-1

first of all, I'm pretty new to java. I have to make 2 checkerboards but my code doesn't print anything off. I have never seen this error before so I dont know what to do next. could someone please help?

import java.awt.*;
import java.applet.*;

/**
* checkers
* @author /////////
* @version 3.7.2018
*/

public class checkers extends Applet {

  public static void main(String args[]){
  Graphics myG;
  drawCheckerboard(100,100, myG);
  drawCheckerboard(120,500, myG);
}
/**
 * this method makes a checkerboard in the size that you want.
 * @param Graphics thaks the Graphics as a parameter
 */
public void drawCheckerboard(int x, int y, Graphics g) {
  int row;
  int col;

  for ( row = 0; row < 8; row++ ) {

    for ( col = 0; col < 8; col++) {
    x = col * 20;
    y = row * 20;

      if ( (row % 2) == (col % 2) ){
        g.setColor(Color.white);
      }
      else{
        g.setColor(Color.black);
        g.fillRect(x, y, 20, 20);
      }
    }
  }
}
IalexM
  • 1
  • 2
  • Possible duplicate [What is the reason behind “non-static method cannot be referenced from a static context”?](https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static); [Non-static variable cannot be referenced from a static context](https://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – MadProgrammer Mar 17 '18 at 06:53
  • so how can I fix this? – IalexM Mar 17 '18 at 06:54
  • Applets are officially deprecated - it's time to stop using them - besides, `Applet`s don't have a `main` method. Also, you clearly have a lack of understanding into how painting works in AWT/Swing as you're about to run head long into a `NullPointerException`. Stop what you're doing and start by reading through [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) and [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) – MadProgrammer Mar 17 '18 at 06:55
  • Read the duplicate questions - understand the difference between a `static` and non `static` - personally, I'd recommend starting with a good tutorial - [Understanding Class Members](https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) might be a good place to start – MadProgrammer Mar 17 '18 at 06:58

1 Answers1

0

Applets are officially deprecated - it's time to stop using them - besides, Applets don't have a main method.

Clearly have a lack of understanding into how painting works in AWT/Swing as you're about to run head long into a NullPointerException. Stop what you're doing and start by reading through Performing Custom Painting and Painting in AWT and Swing

public static void main is a static context, meaning it can not reference non-static (or instance) fields and methods. Have a look at Understanding Class Members and Static methods vs Instance methods in Java for more details.

Remember, static is not your friend, use it sparingly and wisely.

Instead of trying to fix your code directly, because Applet is dead, I've provided a simple example of how painting should be done...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            drawCheckerboard(100, 100, g2d);
            g2d.dispose();
        }

        protected void drawCheckerboard(int x, int y, Graphics2D g) {
            int row;
            int col;

            for (row = 0; row < 8; row++) {

                for (col = 0; col < 8; col++) {
                    x = col * 20;
                    y = row * 20;

                    if ((row % 2) == (col % 2)) {
                        g.setColor(Color.white);
                    } else {
                        g.setColor(Color.black);
                        g.fillRect(x, y, 20, 20);
                    }
                }
            }
        }
    }

}

Now, there are also two issues with your painting routine. The first, it's not actually using the x/y parameters passed to it, so both boards are getting painted to the same location. Second, it's ignoring the white cell.

You can fix this simply enough...

protected void drawCheckerboard(int x, int y, Graphics2D g) {
    int row;
    int col;

    for (row = 0; row < 8; row++) {

        for (col = 0; col < 8; col++) {
            int xPos = col * 20;
            int yPos = row * 20;

            if ((row % 2) == (col % 2)) {
                g.setColor(Color.white);
            } else {
                g.setColor(Color.black);
            }
            g.fillRect(x + xPos, y + yPos, 20, 20);
        }
    }
}

Also, you'll need to change the getPreferredSize to accomodate the content...

@Override
public Dimension getPreferredSize() {
    return new Dimension(280, 660);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366