gpt4 book ai didi

java - 通过 ActionListener 重绘未执行

转载 作者:行者123 更新时间:2023-12-01 12:42:16 25 4
gpt4 key购买 nike

我尝试阅读有关堆栈溢出的类似问题,但无法得到具体的解决方案。所以,我把它发回来。我通过单击按钮来调用重绘。与绘画功能一样,我随机绘制对象。我想当我点击按钮时它们应该移动。但是,当我单击按钮时什么也没有发生。

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

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

}

最佳答案

e.getActionCommand() 返回“START”,因此您必须更改:

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

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

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

中使用它

关于java - 通过 ActionListener 重绘未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24989103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com