如何更新Java swing背景图?
正如您所看到的这些图片,背景网格线和黑色矩形在菜单项关闭后不会更新。我怎样才能更新它?在 C# 中,有一个事件处理程序可以使其自动更新,但我是 Java swing GUI 应用程序的新手。
这是代码:
public void paint(Graphics g) {
super.paintComponents(g);
MainDisplayForm mD = new MainDisplayForm();
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
int gridWidth = 1240;
int gridHeight = 400;
g2.fillRect(20, 50, gridWidth, gridHeight);
g2.setColor(Color.darkGray);
paintGrid(g2,gridWidth, gridHeight);
g2.setColor(Color.red);
Line2D line = new Line2D.Float(20, 50, 250, 260);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g2.draw(line); //pending
g2.dispose();
}
private void paintGrid(Graphics g, int gridWidth, int gridHeight)
{
for(int i=20; i<gridWidth+20; i=i+10)
{
g.drawLine(i, 50, i, gridHeight+49);
}
for(int i=50; i<gridHeight+50; i=i+10)
{
g.drawLine(20, i, 1259, i);
}
}
//感谢您的评论!这是要添加的事件处理程序。 必须将其添加到网格上绘制的每个 memu 项目
private void jMenu2MenuDeselected(javax.swing.event.MenuEvent evt) {
repaint();
}
As you see these picture, the background grid lines and black rectangle are not updated after the menu item is closed. How can I update it? In C# there is an event handler to make it updated automatically, but I am newbie to Java swing GUI application.
Here is the code:
public void paint(Graphics g) {
super.paintComponents(g);
MainDisplayForm mD = new MainDisplayForm();
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
int gridWidth = 1240;
int gridHeight = 400;
g2.fillRect(20, 50, gridWidth, gridHeight);
g2.setColor(Color.darkGray);
paintGrid(g2,gridWidth, gridHeight);
g2.setColor(Color.red);
Line2D line = new Line2D.Float(20, 50, 250, 260);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//g2.draw(line); //pending
g2.dispose();
}
private void paintGrid(Graphics g, int gridWidth, int gridHeight)
{
for(int i=20; i<gridWidth+20; i=i+10)
{
g.drawLine(i, 50, i, gridHeight+49);
}
for(int i=50; i<gridHeight+50; i=i+10)
{
g.drawLine(20, i, 1259, i);
}
}
//Thanks for comments!! Here is the event handler to add.
This must be added to every memu item drawn over the grid
private void jMenu2MenuDeselected(javax.swing.event.MenuEvent evt) {
repaint();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要重写paint()并调用super.paintComponents()。
自定义绘制是通过重写 JPanel(或 JComponent)的 PaintComponent() 方法来完成的,然后您将调用 super.paintComponent()。
如果您需要更多帮助,请发布您的 SSCCE 来演示问题。
Don't override paint() and invoke super.paintComponents().
Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent) and then you would invoke super.paintComponent().
If you need more help then post your SSCCE demonstrating the problem.
使用
repaint()
刷新/更新组件。何时使用
repaint()
?将某些组件添加到面板/框架或操作某些组件后,例如,当您更改 Swing 中组件的位置/属性时动画,调用
repaint()
它会为你完成这项工作。实际上,它触发了对调用组件的update()
方法的调用。Use
repaint()
for refreshing/updating the components.When to use
repaint()
?After you add some components to a panel/frame or manipulate some components, for example when you change the locations/properties of components in a Swing animation, call
repaint()
and it will do the job for you. Actually, it triggers a call forupdate()
method of the calling component.当菜单关闭时,调用重新打印到窗口。
不要调用
paint(getGraphics())
。相反,调用repaint()
因为这会通知超级组件它也需要重绘。When the menu is closed call reprint to the window.
Don't call
paint(getGraphics())
. Instead callrepaint()
because this informs the super component that it needs to redraw as well.