Java 从另一个类调用paint() 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18816251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling the paint() method from another class?
提问by romofan23
I am trying to call the paint()
method from another class, but it just doesn't work.
我试图paint()
从另一个类调用该方法,但它不起作用。
Here is the code:
这是代码:
Main.java
:
Main.java
:
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private int WIDTH = 600;
private int HEIGHT = 400;
private String NAME = "Dark Ages";
private String VERSION = "0.0.1 Pre-Alpha";
static boolean running = false;
private Image dbImage;
private Graphics dbg;
public Main() {
//Initialize window
JFrame frame = new JFrame();
frame.setTitle(NAME + " - " + VERSION);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//Running
running = true;
}
public void paint(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g) {
// Draw Images
repaint();
}
public static void main(String args[]) {
new Main();
Player player = new Player();
}
}
Player.java
:
Player.java
:
public class Player {
public void paint(Graphics g) {
g.drawRect(100, 100, 100, 100);
}
}
采纳答案by Hovercraft Full Of Eels
How do I call the paint() method from another class in java?
如何从java中的另一个类调用paint()方法?
In brief, you don't. And in fact, you shouldn't call it directly from the same class either. Instead you should change the state of the class (change its fields), and then call repaint()
on it, which will suggest to the JVM that the component should be painted.
简而言之,你没有。事实上,你也不应该直接从同一个类中调用它。相反,您应该更改类的状态(更改其字段),然后调用repaint()
它,这将向 JVM 建议应该绘制组件。
Other issues and suggestions:
其他问题和建议:
- An obvious issue I see immediately in your code is that your class has a Graphics field, something that is dangerous to do. I suggest that you get rid of that field so that you're not tempted to use it.
- Do not draw directly within a
JFrame
,JDialog
or other top-level window. These windows have too many roles to play behind the scenes that you don't really want to mess with how they render themselves or their children (unless you really know what you're doing and have definite need). - Better to draw in the
paintComponent(...)
method override of aJPanel
or otherJComponent
s. - In your
paintComponent(...)
override, don't forget to call thesuper
's method:super.paintComponent(g);
. This will allow Swing to do housekeeping drawing, such as erasing old out of date or deleted images, before you do your drawing. - Run, don't walk to your nearest Swing painting tutorial.
- Your current
Player
class extends no Swing component and is added to no top level window, so its code will do nothing useful. Again, read the tutorials. - Never call
repaint()
from withinpaint(...)
orpaintComponent(...)
. Please post modified code if possible.
: please don't ask us to create code for you as that's your job. Please understand that the more code you create, the better a coder you'll be, and because of this, most of us don't feel that we should cheat you out of the opportunity to create your own code.
- 我在您的代码中立即看到的一个明显问题是您的类有一个 Graphics 字段,这样做很危险。我建议你摆脱那个领域,这样你就不会被诱惑去使用它。
- 不要直接在
JFrame
、JDialog
或其他顶级窗口中绘制。这些窗口在幕后扮演着太多的角色,你真的不想弄乱它们如何呈现自己或他们的孩子(除非你真的知道自己在做什么并且有明确的需求)。 - 最好在
paintComponent(...)
aJPanel
或其他JComponent
s的方法覆盖中绘制。 - 在您的
paintComponent(...)
覆盖中,不要忘记调用super
的方法:super.paintComponent(g);
。这将允许 Swing 在绘图之前进行内务绘图,例如擦除旧的过时或已删除的图像。 - 快跑,不要走到离你最近的 Swing 绘画教程。
- 您当前的
Player
类没有扩展 Swing 组件,也没有添加到任何顶级窗口,因此它的代码将没有任何用处。再次,阅读教程。 - 永远不要
repaint()
从内部paint(...)
或paintComponent(...)
. Please post modified code if possible.
:请不要要求我们为您创建代码,因为这是您的工作。请理解,您创建的代码越多,您就会成为更好的编码员,因此,我们大多数人不认为我们应该欺骗您创建自己的代码的机会。
Useful Java Tutorials:
有用的 Java 教程:
- The Really Big Index: The main tutorial where you should start.
- Using Swing Components: How to create Swing GUI's
- Lesson: Performing Custom Painting: Introductory tutorial to Swing graphics
- Painting in AWT and Swing: Advanced tutorial on Swing graphics
- 真正的大索引:您应该开始的主要教程。
- 使用 Swing 组件:如何创建 Swing GUI
- 课程:执行自定义绘画:Swing 图形介绍性教程
- 在 AWT 和 Swing 中绘画:Swing 图形高级教程