1

so I have a Board class that extends JPanel and method write that when called saves some coordinates and calls repaint. However, the effect of repaint is not displayed on screen. When I tried to add JLabel with redbackground to the panel it showed up, so the panel is showing, it's just that repaint does not work.

public int x, y;
public JPanel panel = new JPanel();
private int xx,yy;
private Color c;

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];
    setLayout(new GridLayout());

    add(panel);
}

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

        repaint();
}

@Override 
protected void paintComponent(Graphics g) {
        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);
    super.paintComponent(g);
}   
user3369008
  • 105
  • 11

2 Answers2

3

Call super.paintComponent first

One of the jobs of paintComponet us to fill the Graphics context with the components background color, this is done to ensure what ever was painted to the a Graphics context before is "cleaned" off

Take a look at Painting in AWT and Swingfor more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    1+ It appears that he thinks that he'll save time by not reading the Swing Graphics tutorials. Little does he know that the opposite is true. – Hovercraft Full Of Eels May 12 '14 at 21:50
  • Well there is no difference at all, I tried it already. Also @HovercraftFullOfEels - No, it won't, or atleast not now. I am not native english speaker and understanding even a short tutorial would take me weeks. For example, I have no idea what he talks about in the second senteces and I would have to google a lot to just understand this one thing. – user3369008 May 12 '14 at 21:53
  • 1
    @user3369008 Its possible that the panel is covering the contents of this component, but since you've decided not to provided a runnable example that demonstrates your problem, we're jus making guesses now – MadProgrammer May 12 '14 at 21:56
  • @user3369008 Based on your question and example code provide from your [other question](http://stackoverflow.com/questions/23620780/board-isnt-repainting-properly?noredirect=1#comment36266009_23620780), you don't seem to understand how painting works in Swing. Painting is destructive, that is, each time `paintComponent` is called, you are expected to repaint the entire state the component, not just what you want to update... – MadProgrammer May 13 '14 at 02:42
1

Override paintComponent for instance member panel rather then Board JPanel class itself.

panel = new JPanel() {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        ...// your code here
    }
};

Why Board class is extending JPanel?

Try to use design pattern such as Favor Composition over Inheritance

Braj
  • 46,415
  • 5
  • 60
  • 76
  • Well yes indeed it does, but even if I set them manually to anything it still doesn't matter - no changes. I already fixed that too. – user3369008 May 12 '14 at 22:00
  • It would have worked too but I just removed the panel and it was K aswell. However, do you know if there is a possibility to not lose everything on screen everytime repaint() is called? By repainting I just want to color one rectangle on screen, without losing all the others. – user3369008 May 12 '14 at 22:09
  • Have a look at [how to use clip to reduce paint time?](http://stackoverflow.com/questions/12139461/how-to-use-clip-to-reduce-paint-time). – Braj May 12 '14 at 22:10
  • Uhm, I don't know how can that help me. – user3369008 May 12 '14 at 22:13
  • Read more about **Swing clipping**. A clip is a region of whole window. You can just update a section. – Braj May 12 '14 at 22:15
  • First read and search on Google. Make some programs if there is any doubt then ask a separate question on **StackOverflow**. – Braj May 12 '14 at 22:16