使用 getGraphics() 绘制对象而不扩展 JFrame [英] Drawing an object using getGraphics() without extending JFrame
问题描述
如何在没有类(扩展 JFrame
)的情况下绘制对象?我找到了 getGraphics
方法,但它没有绘制对象.
How can I draw an object without a class (which extends JFrame
)? I found getGraphics
method but it doesnt draw the object.
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(600, 400);
JPanel panel = new JPanel();
frame.add(panel);
Graphics g = panel.getGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, 100, 100);
}
}
推荐答案
如果你想改变你的组件被绘制的方式(你正在添加矩形),你需要重新定义paintComponent()
在那个组件中.在您的代码中,您使用的是 getGraphics()
.
If you want to change the way your component is being drawn (you are adding rectangles), you need to redefine paintComponent()
in that component. In your code, you are using getGraphics()
.
您不应在组件上调用 getGraphics()
.您所做的任何绘制(对返回的 Graphics
)都将是临时的,并且会在下次 Swing 确定需要重新绘制组件时丢失.
You shouldn't call getGraphics()
on a component. Any painting you do (to the Graphics
returned) will be temporary and will be lost the next time Swing determines a component needs to be repainted.
相反,您应该覆盖paintComponent(Graphics)
方法(JComponent
或JPanel
),并在此方法中进行绘制, 使用作为参数接收的 Graphics
对象.
Instead, you should override the paintComponent(Graphics)
method (of the JComponent
or JPanel
), and do the painting in this method, using the Graphics
object received as argument.
查看此链接以进一步阅读.
以下是您的代码,已更正.
Below is your code, corrected.
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(600, 400);
JPanel panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 100, 100);
}
};
frame.add(panel);
// Graphics g = panel.getGraphics();
// g.setColor(Color.BLUE);
// g.fillRect(0, 0, 100, 100);
frame.validate(); // because you added panel after setVisible was called
frame.repaint(); // because you added panel after setVisible was called
}
}
另一个版本,执行完全相同的事情,但您可能更清楚:
Another version, does the exact same thing, but may be clearer to you:
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(600, 400);
JPanel panel = new MyRectangleJPanel(); // changed this line
frame.add(panel);
// Graphics g = panel.getGraphics();
// g.setColor(Color.BLUE);
// g.fillRect(0, 0, 100, 100);
frame.validate(); // because you added panel after setVisible was called
frame.repaint(); // because you added panel after setVisible was called
}
}
/* A JPanel that overrides the paintComponent() method and draws a rectangle */
class MyRectangleJPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 100, 100);
}
}
这篇关于使用 getGraphics() 绘制对象而不扩展 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!