在JPanel的一部分中绘制而不重新绘制其余部分 [英] Paint in a part of JPanel without repaint the rest
问题描述
我正在尝试用Java创建一个Mastermind。代码并不是很难,但我希望有一个非常好的界面。我有一个JPanel,它占用了我所有的JFrame,我使用surchargind绘制这个JPanel repaint()
方法:
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.
推荐答案
一种非常简单的方法是使用 paintImmediately(x,y ,w,h);
A very simple way is to use paintImmediately(x,y,w,h);
这仅重新绘制以像素(x,y)开头的宽度为w和高度为h的指定区域。
This repaints only the specified area that starts at pixel (x,y) with width w and height h.
这篇关于在JPanel的一部分中绘制而不重新绘制其余部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!