简体   繁体   中英

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.

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.

You can set clip Shape to the thick oval and fill it.

Create the big oval Shape via BasicStroke with thick line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM