У меня есть задание, которое требует, чтобы мяч перемещался по экрану, на основе кнопки, которую пользователь нажимает, и чтобы мяч чередовался между красным и зеленым щелчком другой кнопки. Все это работает, но цвет меняется. У меня есть слушатель и класс, реагирующий на нажатие кнопки, но я, похоже, не получаю изменения. Есть ли лучший/более простой способ добиться этого?Внедрить изменение цвета с помощью прослушивателя действий
Заранее благодарим за помощь!
код у меня есть:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Lab2b extends JFrame {
Lab2b(){
setTitle("Lab 2");
Lab2Panel p = new Lab2Panel();
add(p);
}
public static void main(String[] args){
Lab2b frame = new Lab2b();
frame.setTitle("Lab 2 - Ball Mover ");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setVisible(true);
}
}
class Lab2Panel extends JPanel{
Lab2Button canvas = new Lab2Button();
JPanel panel = new JPanel();
Lab2Panel() {
setLayout(new BorderLayout());
JButton leftButton = new JButton("left");
JButton rightButton = new JButton("right");
JButton upButton = new JButton("up");
JButton downButton = new JButton("down");
JButton colorButton = new JButton("Change Color");
panel.add(leftButton);
panel.add(rightButton);
panel.add(upButton);
panel.add(downButton);
panel.add(colorButton);
this.add(canvas, BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);
leftButton.addActionListener(new LeftListener(canvas));
rightButton.addActionListener(new RightListener(canvas));
upButton.addActionListener(new UpListener(canvas));
downButton.addActionListener(new DownListener(canvas));
colorButton.addActionListener(new ColorChangeListener(canvas));
}
}
class Lab2Button extends JPanel {
int radius = 5;
int x = -1;
int y = -1;
protected void paintComponent(Graphics g){
if (x<0 || y<0) {
x = getWidth()/2 - radius;
y = getHeight()/2 - radius;
}
super.paintComponent(g);
g.setColor(Color.RED);
g.drawOval(x,y, 2 * radius, 2 * radius);
}
public void moveLeft(){
x -= 5;
this.repaint();
}
public void moveRight(){
x += 5;
this.repaint();
}
public void moveUp(){
y -= 5;
this.repaint();
}
public void moveDown(){
y += 5;
this.repaint();
}
public void colorChange(){
this.repaint();
}
}
class LeftListener implements ActionListener{
private Lab2Button canvas;
LeftListener(Lab2Button canvas) {
this.canvas = canvas;
}
public void actionPerformed(ActionEvent e){
canvas.moveLeft();
}
}
class RightListener implements ActionListener{
private Lab2Button canvas;
RightListener(Lab2Button canvas) {
this.canvas = canvas;
}
public void actionPerformed(ActionEvent e){
canvas.moveRight();
}
}
class UpListener implements ActionListener{
private Lab2Button canvas;
UpListener(Lab2Button canvas) {
this.canvas = canvas;
}
public void actionPerformed(ActionEvent e){
canvas.moveUp();
}
}
class DownListener implements ActionListener{
private Lab2Button canvas;
DownListener(Lab2Button canvas) {
this.canvas = canvas;
}
public void actionPerformed(ActionEvent e){
canvas.moveDown();
}
}
class ColorChangeListener implements ActionListener {
private Lab2Button canvas;
ColorChangeListener(Lab2Button canvas) {
this.canvas = canvas;
}
public void actionPerformed(ActionEvent e){
canvas.colorChange();
}
}
Кнопка движения Слушатель Код класса:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Lab2MoveBallListener extends Lab2Button implements ActionListener {
public void actionPerformed(ActionEvent e){
this.moveLeft();
}
}
Хорошо, изменил этот код:
public void colorChange(){
this.repaint();
}
Для этого и это ошибки на компиляции с: ошибка: не удается найти символ if (g.getColor() = Color.RED) {
public void colorChange(){
if (g.getColor() = Color.RED){
g.setColor(Color.GREEN);
}
else{
g.setColor(Color.RED);
}
this.repaint();
}