java多类绘画
我正在从事一个带有图形的项目,到目前为止我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案与 Thread 无关(或者更确切地说,在此抛出线程并不能解决代码已经存在的问题)。这一切都取决于定制绘画,并正确地进行。
有关详细信息,请参阅 Java 教程的执行自定义绘制课程。
一些一般性提示是:
paint(Graphics)
。一旦您这样做,您就会发现自定义渲染可能会在JDialog
、JInternalFrame
(等)中更好地显示,而不是您在其中编码的任何内容。JComponent
或JPanel
。第一个用于完全自定义绘画,第二个用于将自定义绘画与其他组件相结合。在这两个类中,重写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:
paint(Graphics)
in a top level container. The minute you do, you discover that the custom rendering might be better shown in aJDialog
,JInternalFrame
(etc.) rather than whatever you coded it in.JComponent
orJPanel
. The first for entirely custom painting, the second if combining custom painting with other components. In either of those classes, overridepaintComponent(Graphics)
rather thanpaint(Graphics)
.EachWordUpperCase
, methods & attributesfirstWordLowerCase
, constantsALL_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.repaint()
from within eitherpaint(Graphics)
orpaintComponent(Graphics)
.