[英]Plot points using paint(g) and run()
我要做的是一旦按下按鈕“ GO”,它將繪制/繪制具有不同坐標的3個橢圓。 我試過重新粉刷,但似乎不起作用。 它僅顯示一個橢圓,即最后一個橢圓。 我希望它堆疊起來並附加橢圓形。
這是我的代碼:
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();
}
}
注意 -在刪除之前的問題之前,我正在研究此答案,因此就您在此問題中發布的新代碼而言,答案可能會有所不同,但這可以使您朝着相同的目標邁進。
不要在actionPerformed
初始化所有內容。 您將收到NullPointerException
因為在初始化數組之前,框架隱式調用了paint
。 我所做的是創建一個方法來初始化它
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; }
不要在諸如JFrame
頂級容器上繪畫。 相反,我們可以使用JPanel
或JCompoent
並覆蓋paintComponent
,並在JPanel
覆蓋getPreferredSize()
,因此您不必設置JFrame
的大小。 只需pack()
它。
像這樣從事件調度線程運行Swing應用
public static void main(String[] args) { SwingUtilitiies.invokeLater(new Runnable(){ public void run(){ new Test(); } }); }
您永遠不會將按鈕添加到框架。
不要使用空布局。 使用布局管理器 。
添加您的組件, 然后調用setVisible
這是正在運行的重構代碼
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.