[英]Plot points using paint(g) and run()
What I want to do is once I pressed the button "GO", it will paint/draw 3 ovals with different coordinates. 我要做的是一旦按下按钮“ GO”,它将绘制/绘制具有不同坐标的3个椭圆。 I've tried repainting but seems it doesn't work. 我试过重新粉刷,但似乎不起作用。 It only shows one oval which is the last oval. 它仅显示一个椭圆,即最后一个椭圆。 I want it to stack up and append the ovals. 我希望它堆叠起来并附加椭圆形。
Here's my code: 这是我的代码:
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test extends JFrame implements ActionListener{
JButton button;
int[] itoken;
int x,y;
public Test() {
super("Test");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,500);
this.setVisible(true);
this.setResizable(true);
this.setLayout(null);
button= new JButton("GO");
button.setBounds(500, 100, 50,50);
this.add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button){
String text= "200 300 250 150 400 100";
String[] token= text.split("\\W");
itoken= new int[token.length];
int i=0;
for (String str : token){
itoken[i++] = Integer.parseInt(str);
}
for(i=0; i<itoken.length; i++)
System.out.println(itoken[i]);
run();
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawOval(x - 5, y - 5, 10, 10);
}
public void run(){
int i=0;
while(i<itoken.length-1){
repaint();
x=itoken[i];
y=itoken[i+1];
i++;
}
}
public static void main(String[] args) {
Test test = new Test();
}
}
Note - I was working on this answer right before you deleted your previous question, so the answer may see a little off in terms of the new code you posted in this question, but it gets you towards the same goal. 注意 -在删除之前的问题之前,我正在研究此答案,因此就您在此问题中发布的新代码而言,答案可能会有所不同,但这可以使您朝着相同的目标迈进。
Don't initialize everthing in the actionPerformed
. 不要在actionPerformed
初始化所有内容。 You're getting a NullPointerException
because paint
is called by the frame implicitly before the array is initialized. 您将收到NullPointerException
因为在初始化数组之前,框架隐式调用了paint
。 What I did was create a method to initialize it 我所做的是创建一个方法来初始化它
int[] iToken = initArray(); ... private int[] initArray() { String text = "200 300 250 150 400 100"; String[] token = text.split("\\\\W"); int[] itoken = new int[token.length]; int i = 0; for (String str : token) { itoken[i++] = Integer.parseInt(str); } return itoken; }
Don't paint on top level containers like JFrame
. 不要在诸如JFrame
顶级容器上绘画。 Instead us a JPanel
or JCompoent
and override paintComponent
, and override getPreferredSize()
in your JPanel
so you won't have to set the size of your JFrame
. 相反,我们可以使用JPanel
或JCompoent
并覆盖paintComponent
,并在JPanel
覆盖getPreferredSize()
,因此您不必设置JFrame
的大小。 Just pack()
it. 只需pack()
它。
Run Swing apps from the Event Dispatch Thread like this 像这样从事件调度线程运行Swing应用
public static void main(String[] args) { SwingUtilitiies.invokeLater(new Runnable(){ public void run(){ new Test(); } }); }
You never add your button to the frame. 您永远不会将按钮添加到框架。
Don't use a null layout. 不要使用空布局。 Use Layout Managers . 使用布局管理器 。
Add you components, then call setVisible
添加您的组件, 然后调用setVisible
Here's the running refactored code 这是正在运行的重构代码
import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test extends JFrame implements ActionListener {
JButton button;
boolean paint = false;
int x, y;
int[] iToken = initArray();
public Test() {
super("Test");
button = new JButton("GO");
button.setBounds(500, 100, 50, 50);
button.addActionListener(this);
add(button, BorderLayout.SOUTH);
add(new DrawPanel());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
this.setResizable(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
paint = true;
repaint();
}
}
private int[] initArray() {
String text = "200 300 250 150 400 100";
String[] token = text.split("\\W");
int[] itoken = new int[token.length];
int i = 0;
for (String str : token) {
itoken[i++] = Integer.parseInt(str);
}
return itoken;
}
public class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (paint) {
for (int i = 0; i < iToken.length; i += 2) {
x = iToken[i];
y = iToken[i + 1];
g.drawOval(x - 5, y - 5, 10, 10);
}
}
}
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.