通过ActionListener重绘不执行

时间:2014-07-28 05:48:09

标签: java swing

我曾尝试阅读有关堆栈溢出的类似问题,但我无法得到具体的解决方案。所以,我发回来了。我点击按钮即可重新调用。在绘画功能中,我随机绘制对象。当我点击按钮时,我想他们应该移动。但是,当我点击按钮时没有任何反应。

有谁知道为什么会发生这种行为以及如何解决这个问题?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

@SuppressWarnings("serial")
public class Filter extends JFrame
{
    public static void main(String[] args)
      {
            SwingUtilities.invokeLater(new Runnable()
            {  
                public void run() 
                {
                    Filter mainFrame = new Filter();
                    mainFrame.setVisible(true);
                }
            });
      }

    public Filter()
    {
        //Creating the JFrame main window
        setSize(800, 500);
        setTitle("Particle Filter");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocation(100, 100);
        getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));

        //creates two panels content and sidebar. Sidebar has null layout       
        JPanel content = new JPanel();
        content.setPreferredSize(new Dimension(700,500));
        content.setBackground(Color.LIGHT_GRAY);
        this.getContentPane().add(content);
        JPanel sidebar = new JPanel();
        sidebar.setBackground(Color.LIGHT_GRAY);
        sidebar.setPreferredSize(new Dimension(100,500));
        this.getContentPane().add(sidebar);
        sidebar.setLayout(null);

        //creates three buttons in sidebar
        JButton start_button = new JButton("START");
        start_button.setBounds(10, 75, 77, 23);

        start_button.addActionListener(new MainPanel());       

        sidebar.add(start_button);
        JButton stop_button = new JButton("STOP");
        stop_button.setBounds(10, 109, 77, 23);
        sidebar.add(stop_button);
        JButton reset_button = new JButton("RESET");
        reset_button.setBounds(10, 381, 77, 23);
        sidebar.add(reset_button);

        //calls the content_Walls class and sends the number of ovals to be generated
        content.add( new MainPanel());
    }

}

@SuppressWarnings("serial")
class MainPanel extends JPanel implements ActionListener
{
    public MainPanel()
    {
        setPreferredSize(new Dimension(680,450));
        setBackground(Color.WHITE);
        setBorder(BorderFactory.createLineBorder(Color.black));
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getActionCommand().equals("start_button"))
        repaint();
    }

    @Override
    public void paintComponent(Graphics g)
    {   
        super.paintComponent(g);
        drawParticles(g);
        createObstacles(g,150,225,100,40);
        createObstacles(g,500,300,40,100);
        createRobot(g);        
    }

    private void createRobot(Graphics g)
    {
        int x=0, y=0;     
        int robot_radius=50;
        ArrayList<Integer> robot_list= new ArrayList<Integer>();
        robot_list=positionRobot(x,y);
        drawRobot(g,robot_list.get(0),robot_list.get(1),robot_radius);
    }

    private void drawParticles(Graphics g)
    {
        int n=1000; // n denotes the number of particles    
        ArrayList<Integer> list;        
        list = new ArrayList<Integer>(Collections.nCopies(n, 0));
        for(int i=0;i<list.size();i++)
        {
           generateParticles(g);
        }
    }

    private void generateParticles(Graphics g)
    {
        int x=0;
        int y=0;
        int radius = 4;
        ArrayList<Integer> list= new ArrayList<Integer>();
        list=positionParticles(x,y);
        g.setColor(Color.RED);
        g.fillOval(list.get(0),list.get(1), radius, radius);
    }

    private ArrayList<Integer> positionParticles(int x, int y)
    {
        int radius = 4;
        ArrayList<Integer> list= new ArrayList<Integer>();
        x=randomInteger(2,678); // bounds of x between which the particles should be generated
        y=randomInteger(2,448); // bounds of y between which the particles should be generated
        x=x-(radius/2);
        y=y-(radius/2);
        if((x<251&&x>=150)&&(y<266&&y>=225))
        {
            x=0;
            y=0;
            positionParticles(x,y);
        }
        if((x<541&&x>499)&&(y<401&&y>299))
        {
            x=0;
            y=0;
            positionParticles(x,y);
        }
        list.add(x);
        list.add(y);
        return list;
    }

    private ArrayList<Integer> positionRobot(int x, int y)
    {
        int robot_radius=50;   
        ArrayList<Integer> list= new ArrayList<Integer>();
        x=randomInteger(25,655);//so that it stays inside the content_Walls panel 
        y=randomInteger(25,425); //so that it stays inside the content_Walls panel 
        x=x-(robot_radius/2);
        y=y-(robot_radius/2);
        if((x<250&&x>=150)||(y<=265&&y>=225))
        {
           x=0;
           y=0;
           positionRobot(x,y);
        }
        if((x<=540&&x>=500)||(y<=400&&y>=300))
        {
            x=0;
            y=0;
            positionRobot(x,y);
        }
        list.add(x);
        list.add(y);
        return list;            
    }

    private void createObstacles(Graphics g, int x, int y, int width, int height)
    {
        g.setColor(Color.BLACK);
        g.fillRect(x, y, width, height);
    }

    private void drawRobot(Graphics g, int x, int y, int radius)
    {
        g.setColor(Color.GREEN);
        g.fillOval(x, y, radius, radius);   
    }

    private static int randomInteger(int min, int max)
    {
        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }

}

1 个答案:

答案 0 :(得分:1)

e.getActionCommand()返回&#34; START&#34;所以你必须改变:

if (e.getActionCommand().equals("start_button"))

if (e.getActionCommand().equals("START"))

您的第二个问题是,您使用了两个不同的Mainpanel。创建Mainpanel的实例,并在start_button.addActionListener(mainPanel)content.add(mainPanel);

中使用此实例