Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paint in a part of JPanel without repaint the rest

I'm trying to make a Mastermind in Java. The code isn't really difficult, but I want to have a very good interface. I have a JPanel which take all my JFrame, and I paint this JPanel with surchargind repaint() method:

//method to draw elements on the map
public void paint(Graphics g) {
   super.paintComponents(g);
   Graphics gr;
   gr = MasterMindPane.getGraphics();

   img = MasterMindPane.getToolkit().getImage("images/plateau4-8.jpg");
   gr.drawImage(img, 0, 0, 600, 720, this);

   gr = bouleRougePane.getGraphics();
   img = bouleRougePane.getToolkit().getImage("images/bouleRouge.png");
   //gr.drawImage(img, 535, 303, 45, 45, this);
   gr.drawImage(img, 0, 0, 45, 45, this);
   gr = bouleOrangePane.getGraphics();
   img = bouleOrangePane.getToolkit().getImage("images/bouleOrange.png");
   //gr.drawImage(img, 535, 303, 45, 45, this);
   gr.drawImage(img, 0, 0, 45, 45, this);
}

When I click on one image, which have a Panel, I draw a yellow circle like that :

private void bouleRougePaneMouseClicked(java.awt.event.MouseEvent evt) {                                            
   Graphics2D g2d = (Graphics2D) MasterMindPane.getGraphics();

   for(int i = 0; i<4; i++)
   {
      g2d.setColor(Color.ORANGE);
      g2d.setStroke(new BasicStroke(3));
      g2d.drawOval(78+i*70, 106+etape*50, 35, 35);
   }
}      

And when I select a hole, I want to delete the circle, which only indicates where the gamer can play.

But I don't know how to delete the circle or repaint just a part of my Image because it costs a lot to repaint all.

like image 496
bbusseuil Avatar asked Nov 05 '22 03:11

bbusseuil


1 Answers

A very simple way is to use paintImmediately(x,y,w,h);

This repaints only the specified area that starts at pixel (x,y) with width w and height h.

like image 78
Bruno Avatar answered Nov 09 '22 13:11

Bruno