在 JPanel 上绘图
我首先从 NetbeanIDE 的 JPanel
创建扩展自 RubicPanel
类,将其设置为黑色背景,将其放在 JFrame
上,然后开始绘制通过使用另一个这样的类来实现它。
public class Drow {
private final int SquareSize = 99;
public void DrowRubic(RubicEntity GameRubic, RubicPanel rPanel) {
Graphics g = rPanel.getGraphics();
g.setColor(Color.pink);
g.fillRect(0, 0, 301, 301);
int CurrentFace = GameRubic.getDirection(1);
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
DrowSquare(g, (99*i)+1 , (j*99)+1, GameRubic.getSpecificCell(i, j, CurrentFace));
}
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void DrowSquare(Graphics g, int x, int y, Color c) {
g.setColor(c);
g.fillRect(x, y, this.SquareSize-1, this.SquareSize-1);
}
}
结果出现的时间很短,似乎立即被黑色背景取代。
我该如何解决它以及为什么会发生这个问题?
最后为我糟糕的英语感到抱歉。 :)
I'm start with create RubicPanel
class extended from JPanel
from NetbeanIDE set it to black background, put it on a JFrame
and I start to draw on it by using another class like this.
public class Drow {
private final int SquareSize = 99;
public void DrowRubic(RubicEntity GameRubic, RubicPanel rPanel) {
Graphics g = rPanel.getGraphics();
g.setColor(Color.pink);
g.fillRect(0, 0, 301, 301);
int CurrentFace = GameRubic.getDirection(1);
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
DrowSquare(g, (99*i)+1 , (j*99)+1, GameRubic.getSpecificCell(i, j, CurrentFace));
}
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void DrowSquare(Graphics g, int x, int y, Color c) {
g.setColor(c);
g.fillRect(x, y, this.SquareSize-1, this.SquareSize-1);
}
}
and result is appear very short time and seem to be replace with black background immediately.
how can i fix it and why this problem happened?
and the last thing sorry for my bad English. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要执行自定义绘制,请重写
paintComponent
。由于您不想破坏传递的 Graphics 对象,因此最好创建一个副本,以便稍后处理。例如,To perform custom painting, override
paintComponent
. And since you don't want to clobber the passedGraphics
object, it's best that you make a copy that will you later dispose of. For instance,