NullPointerException on Java graphics

后端 未结 2 1582
难免孤独
难免孤独 2021-01-26 19:30

I\'m trying to implement a method to paint a specific portion of a grid. To do so I overrided the paintComponent method:

public class Frame extends JPanel {
...
         


        
2条回答
  •  执笔经年
    2021-01-26 20:02

    Change

    public void specificPaint(int coordinateX, int coordinateY, Color color)
    

    to

    public void specificPaint(int coordinateX, int coordinateY, Color color, Graphics g)
    

    Then call specificPaint inside your paintComponent method with the Graphics object you are drawing with

    See Hovercraft's answer for more details as to why using this.getGraphics() doesn't work

提交回复
热议问题