我在一个用图形的项目中工作,到目前为止,我有两个不同的类,每个类都有图形。在这两个类中都调用了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();发布于 2011-04-13 23:38:44
答案与Thread无关(更确切地说,抛出线程并不能解决代码已经存在的问题)。这一切都归结为定制绘画,并正确地进行。
有关详细信息,请参阅Java教程的表演定制绘画课程。
一些一般性的建议是:
paint(Graphics)。当您完成时,您会发现自定义呈现可能更好地显示在JDialog、JInternalFrame (等)中。而不是你把它编码进去的东西。JComponent或JPanel之一。第一种是完全自定义绘画,第二种是将定制绘画与其他组件相结合。在这两个类中,都要重写paintComponent(Graphics)而不是paint(Graphics)。EachWordUpperCase,方法&属性firstWordLowerCase,常量ALL_UPPER_CASE。这一点尤其重要的是,如果任何人,除了你将阅读的代码。其他程序员使用名称的情况来提供关于其性质/来源的提示。paint(Graphics) 或 paintComponent(Graphics)内部调用。https://stackoverflow.com/questions/5656669
复制相似问题