我正在努力解决如何在按钮点击上将圆形图形重新绘制为不同颜色的问题。此外,我还必须对JColorChooser做同样的事情。让选择器对话框显示是相当简单的,但我需要添加到动作侦听器以更改圆的颜色?
我有点不知所措,虽然我知道方法repaint()涉及到了。请看看我的代码并告诉我哪里出错了或者把自己“画”成一个角落。 谢谢!
package P3;
// ******************************************************************
// CirclePanel.java
//
// A panel with a circle drawn in the center and buttons on the
// bottom that move the circle.
// ******************************************************************
import java.awt.*;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.event.*;
public class CirclePanel extends JPanel {
public JButton btnChoice;
private JButton btnRed;
private JButton btnYellow;
private JButton btnBlue;
private final int CIRCLE_SIZE = 50;
private int x, y;
private Color c;
//---------------------------------------------------------------
// Set up circle and buttons to move it.
//---------------------------------------------------------------
public CirclePanel(int width, int height) {
// Set coordinates so circle starts in middle
x = (width / 2) - (CIRCLE_SIZE / 2);
y = (height / 2) - (CIRCLE_SIZE / 2);
//set recentering coordinates - need to find a position relative to
//center of panel and not location of circle (for position reset)
c = Color.green;
// Need a border layout to get the buttons on the bottom
this.setLayout(new BorderLayout());
// Create buttons to move the circle
JButton left = new JButton("Left");
JButton right = new JButton("Right");
JButton up = new JButton("Up");
JButton down = new JButton("Down");
JButton center = new JButton("+");
// Add listeners to the buttons
left.addActionListener(new MoveListener(-20, 0));
right.addActionListener(new MoveListener(20, 0));
up.addActionListener(new MoveListener(0, -20));
down.addActionListener(new MoveListener(0, 20));
//reset circle position to start position
//center.addActionListener(new MoveListener());
// Need a panel to put the buttons on or they'll be on top of each other.
GridBagLayout layout = new GridBagLayout();
// create GridBagConstraints object "gbc" to position the buttons we want to use
GridBagConstraints gbc = null;
//position of up button
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(0, 0, 3, 0);
layout.setConstraints(up, gbc);
//position of left button
gbc = new GridBagConstraints();
gbc.gridy = 1;
gbc.insets = new Insets(0, 0, 0, 3);
layout.setConstraints(left, gbc);
//position of center button
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
layout.setConstraints(center, gbc);
//position of right button
gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 1;
gbc.insets = new Insets(0, 3, 0, 0);
layout.setConstraints(right, gbc);
//position of down button
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 2;
gbc.insets = new Insets(3, 0, 0, 0);
layout.setConstraints(down, gbc);
// apply GridBagLayout to a JPanel
JPanel btnPanel = new JPanel(layout);
//add the buttons to the panel
btnPanel.add(left);
btnPanel.add(right);
btnPanel.add(up);
btnPanel.add(down);
btnPanel.add(center);
//new panel with buttons to choose colors
JPanel chooserBtnPnl = new JPanel();
chooserBtnPnl.setLayout(new GridLayout(1, 4, 3, 3));
//red button
btnRed = new JButton("Red");
btnRed.setBackground(c.RED);
btnRed.addActionListener(new ColorListener(Color.RED));
//yellow button
btnYellow = new JButton("Yellow");
btnYellow.setBackground(c.YELLOW);
btnYellow.addActionListener(new ColorListener(Color.YELLOW));
//blue button
btnBlue = new JButton("Blue");
btnBlue.setBackground(c.BLUE);
btnBlue.addActionListener(new ColorListener(Color.BLUE));
//button used for choosing a color with JColorChooser
btnChoice = new JButton("Choose Color");
btnChoice.addActionListener(new ColorListener());
chooserBtnPnl.add(btnRed);
chooserBtnPnl.add(btnYellow);
chooserBtnPnl.add(btnBlue);
chooserBtnPnl.add(btnChoice);
// Add the chooser panel to the top of the main panel
this.add(chooserBtnPnl, "North");
// Add the button panel to the bottom of the main panel
this.add(btnPanel, "South");
}
//---------------------------------------------------------------
// Draw circle on CirclePanel
//---------------------------------------------------------------
public void paintComponent(Graphics page) {
super.paintComponent(page);
page.setColor(c);
page.fillOval(x, y, CIRCLE_SIZE, CIRCLE_SIZE);
}
//---------------------------------------------------------------
// Class to listen for button clicks that move circle.
//---------------------------------------------------------------
private class MoveListener implements ActionListener {
private int dx;
private int dy;
//---------------------------------------------------------------
// Parameters tell how to move circle at click.
//---------------------------------------------------------------
public MoveListener(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
//---------------------------------------------------------------
// Change x and y coordinates and repaint.
//---------------------------------------------------------------
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
repaint();
}
}
private class ColorListener implements ActionListener {
private Color color;
ColorListener(){}
//tells what color to change the circle to
public ColorListener(Color c) {
this.color = c;
}
//change color and repaint
public void actionPerformed(ActionEvent e) {
JColorChooser jcc = new JColorChooser();
if (e.getSource() == btnChoice) {
jcc.showDialog(btnChoice, "Choose Circle Color", Color.BLUE);
} else {
if (e.getSource() == btnRed) {
repaint();
} else if (e.getSource() == btnYellow) {
repaint();
} else if (e.getSource() == btnBlue) {
repaint();
}
}
}
}
}
和文件MoveCircle.java
package P3;
// ******************************************************************
// MoveCircle.java
//
// Uses CirclePanel to display a GUI that lets the user move
// a circle by pressing buttons.
// ******************************************************************
import java.awt.*;
import javax.swing.*;
public class MoveCircle {
public static void main(String[] args) {
JFrame frame = new JFrame("MoveCircle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.getContentPane().add(new CirclePanel(400,300));
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
}
答案 0 :(得分:1)
实际上,您忘记在ColorListener中将新颜色设置为您的字段c
(圆圈的颜色)。以下代码应该可以解决问题。
// change color and repaint
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnChoice) {
JColorChooser jcc = new JColorChooser();
c = jcc.showDialog(btnChoice, "Choose Circle Color", Color.BLUE);
} else {
c = this.color;
}
repaint();
}