java多类绘画

发布于 2024-11-01 03:55:03 字数 779 浏览 0 评论 0原文

我正在从事一个带有图形的项目,到目前为止我有 2 个不同的类,每个类都有图形。在这两个类中,都会调用 paint(Graphics g) 方法,但是当我执行它时,两个 JFrames 都会闪烁。

我的问题:正确的方法是在一个类中调用项目的所有图形还是每个类都需要新线程?

先感谢您。


代码片段

public void paint(Graphics g) 
{ 
    repaint(); 
    mapLimits();
    moveEnemy();
    g.drawImage(background, 0,0, null); // draw background 
    drawImage(g, myHero, heroXposition, heroYposition, "name"); // draw hero 
    repaint(); 
}

和库存类的绘制方法如下所示,

public void paint(Graphics g) 
{ 
    g.drawImage(background, 0,0,null); //background 
    repaint(); 
} 

并且它们都在主类中调用

Hero hero = new Hero(); 
hero.setVisible(true); 
Inventory inv = new Inventory(); 
inv.setVisible();

I am working in a project with graphics where so far I have 2 different classes with graphics in each one. In both classes the paint(Graphics g) method is called but when I execute it, both JFrames are flickering.

My question: is that the correct way is to call all the graphics of the project in one class or new threads for each class are required?

Thank you in advance.


Code snippets

public void paint(Graphics g) 
{ 
    repaint(); 
    mapLimits();
    moveEnemy();
    g.drawImage(background, 0,0, null); // draw background 
    drawImage(g, myHero, heroXposition, heroYposition, "name"); // draw hero 
    repaint(); 
}

and for the inventory class the paint method goes like this

public void paint(Graphics g) 
{ 
    g.drawImage(background, 0,0,null); //background 
    repaint(); 
} 

and both of them are called in the main class

Hero hero = new Hero(); 
hero.setVisible(true); 
Inventory inv = new Inventory(); 
inv.setVisible();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2024-11-08 03:55:03

答案与 Thread 无关(或者更确切地说,在此抛出线程并不能解决代码已经存在的问题)。这一切都取决于定制绘画,并正确地进行。

有关详细信息,请参阅 Java 教程的执行自定义绘制课程。

一些一般性提示是:

  • 切勿在顶级容器中覆盖 paint(Graphics)。一旦您这样做,您就会发现自定义渲染可能会在 JDialogJInternalFrame (等)中更好地显示,而不是您在其中编码的任何内容。
  • 而是扩展其中之一JComponentJPanel。第一个用于完全自定义绘画,第二个用于将自定义绘画与其他组件相结合。在这两个类中,重写 paintComponent(Graphics) 而不是 paint(Graphics)
  • 使用类和类的通用术语方法名称。类 EachWordUpperCase、方法和方法属性firstWordLowerCase,常量ALL_UPPER_CASE。如果除了您之外的任何人都会阅读代码,这一点尤其重要。其他程序员使用名称的大小写来提供有关其性质/来源的提示。
  • 不要从 paint(Graphics) paintComponent(Graphics) 中调用 repaint()

The answer has nothing to do with Thread (or rather, throwing threads at this will not solve the problems the code already has). It all comes down to custom painting, and doing it correctly.

See the Performing Custom Painting lesson of the Java Tutorial for details.

Some general tips are:

  • Never override paint(Graphics) in a top level container. The minute you do, you discover that the custom rendering might be better shown in a JDialog, JInternalFrame (etc.) rather than whatever you coded it in.
  • Instead extend one of JComponent or JPanel. The first for entirely custom painting, the second if combining custom painting with other components. In either of those classes, override paintComponent(Graphics) rather than paint(Graphics).
  • Use the common nomenclature for class & method names. Classes EachWordUpperCase, methods & attributes firstWordLowerCase, constants ALL_UPPER_CASE. This is especially important if anyone besides you will ever read the code. Other programmers use the case of names to provide hints as to their nature/source.
  • Don't call repaint() from within either paint(Graphics) or paintComponent(Graphics).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文