java - 使用 Paint(g) 和 run() 绘制点

标签 java user-interface paint runnable

我想要做的是,一旦我按下“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(); 
}
}

最佳答案

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.

  1. 不要初始化 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;
    }
    
  2. 不要在顶级容器上绘画,例如 JFrame 。相反,我们是 JPanelJCompoent并覆盖paintComponent ,并覆盖 getPreferredSize()在你的JPanel所以您不必设置 JFrame 的大小。就pack()它。

  3. Event Dispatch Thread 运行 Swing 应用程序像这样

    public static void main(String[] args) {
        SwingUtilitiies.invokeLater(new Runnable(){
            public void run(){
                new Test();
            }
        });
    }
    
  4. 您永远不会将按钮添加到框架中。

  5. 不要使用空布局。使用Layout Managers .

  6. 添加组件,然后调用 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();
            }
        });
    }
}

关于java - 使用 Paint(g) 和 run() 绘制点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21431122/

相关文章:

java - 我无法恢复到上次 Activity

java - 在连接之前,我如何告诉 SSLSocket 所需的 key 输入的别名?

java - 在 Java 中使用 stream.sorted() 对列表进行排序

qt - 警告 : All changes made in this file will be lost

c++ - 窗外的QPushButton(灵感: CleanMyMac)

java - 使用 paintThumb 我已经让我的箭头在 0 或 100 时从 JSlider 上掉下来,如何解决这个问题?

移动前的 Java 碰撞检测

c# - 规范模式实现帮助

Android UI 编辑器/显示错误

android - 我可以在 View 之间共享 Paint 实例吗?