Paint in a part of JPanel without repaint the rest

谁都会走 提交于 2019-12-06 03:48:53

问题


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:

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, 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 indicate 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.


回答1:


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.




回答2:


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

Create the big oval Shape via BasicStroke with thick line.



来源:https://stackoverflow.com/questions/9668564/paint-in-a-part-of-jpanel-without-repaint-the-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!