使用paint(g)和run()绘制点 [英] Plot points using paint(g) and run()

查看:114
本文介绍了使用paint(g)和run()绘制点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是一旦按下按钮"GO",它将绘制/绘制具有不同坐标的3个椭圆.我尝试过重新粉刷,但似乎不起作用.它仅显示一个椭圆,即最后一个椭圆.我希望它堆叠起来并附加椭圆形.

What I want to do is once I pressed the button "GO", it will paint/draw 3 ovals with different coordinates. 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.

这是我的代码:

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(); 
}
}

推荐答案

注意-在删除之前的问题之前,我正在研究此答案,因此就您在 this

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.我要做的是创建一个初始化它的方法

  1. Don't initialize everthing in the actionPerformed. You're getting a NullPointerException because paint is called by the frame implicitly before the array is initialized. 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;
}

  • 请勿在诸如JFrame之类的顶级容器上绘画.取而代之的是我们JPanelJCompoent并覆盖paintComponent,并覆盖JPanel中的getPreferredSize(),因此您不必设置JFrame的大小.只是pack()它.

  • Don't paint on top level containers like 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. Just pack() it.

    事件调度线程运行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.

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

    这篇关于使用paint(g)和run()绘制点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆