Domanda

so I have class Board that extends JApplet and in it's constructor I make a JPanel that I'll later draw boxes on, but when I try to do getGraphics it returns null :/

JPanel panel;
public Board(int x, int y, int wolfNumber, int hareNumber){
    this.x=x;
    this.y=y;

    wolvesCoords = new int[wolfNumber][2];
    haresCoords = new int[hareNumber][2];

    panel = new JPanel();
    panel.setVisible(true);

    add(panel);
}


public synchronized void write(int xx, int yy, Color c){
    int width=panel.getWidth()/x;
    int height=panel.getHeight()/y;

    Graphics g = panel.getGraphics();
    System.out.println(g);

    g.setColor(c);

    g.drawRect(xx*width, yy*height, width, height);
    g.fillRect(xx*width, yy*height, width, height);


}

public void paint(Graphics g)
{
        super.paint(g);
}

It gives nullpointerexception at line g.setColor(c) as g is null.

È stato utile?

Soluzione

You are using the Graphics object wrong. Instead of calling write from wherever you call it, instead override paintComponent. You could do something like:

private int xx;
private int yy;
private Color c;

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if(c != null) {
        int width=panel.getWidth()/x;
        int height=panel.getHeight()/y;

        g.setColor(c);

        g.drawRect(xx*width, yy*height, width, height);
        g.fillRect(xx*width, yy*height, width, height);
    }
}

public void write(int xx, int yy, Color c) {
    this.xx = xx;
    this.yy = yy;
    this.c = c;
    repaint();
}

Altri suggerimenti

Yours is a common problem and question and is yet another reason why you shouldn't use a Graphics object obtained by calling getGraphics() on a component. Another reason you shouldn't do this is that if you are successful at getting a non-null Graphics object (which is only available after the component has been rendered), it will not persist, and your image can turn null if any repaints occur.

Instead do what the tutorials advise you to do: Draw with the Graphics object provided to you in the JPanel's paintComponent method. If you want to draw a fixed background, then do so in a BufferedImage, and then draw that BufferedImage in the paintComponent method.


Edit
You ask:

Why would I call drawing code in the paint method? I need to draw only when the method write is called, not when the app starts.

Because that is how Swing graphics is done, because doing it your way is rife with problems (which you're already experiencing). Again, don't guess at this stuff -- read the tutorials where it is all well explained for you.


Edit
You state in comment:

Actually this error shows up when I try to add override - method does not override or implement a method from a supertype. Could it be that I am extending JApplet?

Yes, exactly so.

I have to though

Yes, you have to have a class that extends JApplet in order to produce JApplets, but you don't have to and in fact shouldn't paint directly in them. Instead create a separate class that extends JPanel, and do your graphics inside of that class's paintComponent method. Then display that JPanel in your applet.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top