Paint on the JPanel part without repainting

I am trying to create Mastermind in Java. The code is not very complicated, but I want to have a very nice interface. I have a JPanel that accepts all my JFrames, and I draw this JPanel using the surchargind method repaint():

public void paint(Graphics g) //méthode permettant de dessiner les éléments sur la carte
   {
   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 that has a panel, I draw a yellow circle as follows:

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 the hole, I want to remove the circle that only indicates where the player can play.

But I don’t know how to delete a circle, or repaint only part of my image, because it is expensive to redraw.

+5
source share
2 answers

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

, (x, y) w h.

+2

.

BasicStroke .

0

All Articles