为什么赢了';JAVA图形绘制
我是一名JAVA初学者,我对以下问题感到头疼: 为什么此代码不绘制为什么赢了';JAVA图形绘制,java,swing,graphics,jpanel,2d,Java,Swing,Graphics,Jpanel,2d,我是一名JAVA初学者,我对以下问题感到头疼: 为什么此代码不绘制 import ... public class tekening extends JFrame{ private JPanel p; private Graphics g; tekening(){ setLayout(new FlowLayout()); p = new JPanel(); p.setPreferredSize(new Dimens
import ...
public class tekening extends JFrame{
private JPanel p;
private Graphics g;
tekening(){
setLayout(new FlowLayout());
p = new JPanel();
p.setPreferredSize(new Dimension(350, 350));
p.setBackground(Color.WHITE);
add(p);
setLocationByPlatform(true);
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
g = p.getGraphics();
g.setColor(Color.BLACK);
g.drawRect(30, 30, 80, 40);
g.drawLine(10, 10, 40, 50);
}
}
import ...
public class tekenclasse extends JFrame implements ActionListener{
private JPanel p;
private Graphics g;
private JButton button1;
tekenclasse(){
setLayout(new FlowLayout());
button1 = new JButton("Knop 1");
button1.addActionListener(this);
add(button1);
p = new JPanel();
p.setPreferredSize(new Dimension(350, 350));
p.setBackground(Color.WHITE);
add(p);
setLocationByPlatform(true);
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
g = p.getGraphics();
g.setColor(Color.BLACK);
g.drawRect(30, 30, 80, 40);
g.drawLine(10, 10, 40, 50);
}
}
这段代码为什么会绘制
import ...
public class tekening extends JFrame{
private JPanel p;
private Graphics g;
tekening(){
setLayout(new FlowLayout());
p = new JPanel();
p.setPreferredSize(new Dimension(350, 350));
p.setBackground(Color.WHITE);
add(p);
setLocationByPlatform(true);
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
g = p.getGraphics();
g.setColor(Color.BLACK);
g.drawRect(30, 30, 80, 40);
g.drawLine(10, 10, 40, 50);
}
}
import ...
public class tekenclasse extends JFrame implements ActionListener{
private JPanel p;
private Graphics g;
private JButton button1;
tekenclasse(){
setLayout(new FlowLayout());
button1 = new JButton("Knop 1");
button1.addActionListener(this);
add(button1);
p = new JPanel();
p.setPreferredSize(new Dimension(350, 350));
p.setBackground(Color.WHITE);
add(p);
setLocationByPlatform(true);
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
g = p.getGraphics();
g.setColor(Color.BLACK);
g.drawRect(30, 30, 80, 40);
g.drawLine(10, 10, 40, 50);
}
}
对我来说,这是完全奇怪的。为什么我不能在构造函数中使用图形。为什么我可以在活动结束后使用它。这太愚蠢了,我想马上画,我不想按按钮
getGraphics()
进行绘制JFrame
JPanel
(或JComponent
)并覆盖其受保护的组件(图形g)
方法。使用该图形上下文进行绘制。无论是直接在paintComponent
方法中编写代码,还是调用将graphics
对象作为参数传递给的方法,所有绘制都应在图形上下文提供的范围内完成JPanel/JComponent
中的public维度getPreferredSize()
,为绘制曲面提供首选尺寸。将pabel放到相框上,然后pack()
public class DrawPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.draw... // do all your drawing here
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
重要提示:在发布另一个关于绘画的问题之前,你必须先阅读关于定制绘画的链接,否则你会被打屁股:-)不要这样画
JFrame
。替代paint
方法。出了什么问题,我已经测试了你的代码,我得到了一个输出中带有白色面板的框架。这是您的输出吗?您可以先使用调试,然后查看图形是否真实绘制。我不知道为什么会发生这种情况,但我认为在那之后图像会被重新绘制,这就是为什么会出现空白帧。请纠正我(我认为@Donvino是正确的。由于您只有构造函数内部的图形,它将redrawn@Donvino我想你是对的,但是我如何解决这个问题呢?你能解释一下为什么我不能使用getGraphics()
,为什么我必须重写paintComponent()
。getGraphics()只存在一个调用,因为它不会持久化任何内容。2.它是绘制组件的绘制链的一部分。这些方法会被调用,因此您不必自己调用它们。阅读链接,他们给出了很好的解释