我正在尝试为我的作业创建一个简单的绘图应用程序,用户可以选择三种颜色中的一种来绘制。要激活"笔",用户需要单击窗口,然后拖动鼠标(未按下)在屏幕上绘制线条。我有一些事情很难。其中之一是在更改颜色时,我想改变该点的颜色(在用户选择不同颜色之后),而不是用户制作的所有先前点(绘图)。我也很难正确实现鼠标监听器。我不确定如何在用户点击屏幕(mousePressed?)后激活mouseMoved函数,并在用户第二次单击时停用mouseMoved。 以下是我到目前为止的情况:
package project.pkg6;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class DrawingTester extends JPanel{
//linked list holds points
private LinkedList<Point> pointList;
//default color
private Color selectedColor = Color.red;
//colors
private final Color red;
private final Color blue;
private final Color yellow;
//eraser
private final Color white;
//radio buttons
private final JRadioButton redButton;
private final JRadioButton blueButton;
private final JRadioButton yellowButton;
private final JRadioButton eraserButton;
//group radio buttons
private final ButtonGroup colorRadioGroup;
//size of dot
private final static int DIAMETER = 10;
public DrawingTester(){
setLayout(new FlowLayout());
setBackground(Color.WHITE);
pointList = new LinkedList<Point>();
this.addMouseMotionListener(new MouseDrawListener());
this.addMouseListener(new MouseDrawListener());
//create radio buttons
redButton = new JRadioButton("Red", true);
blueButton = new JRadioButton("Blue", false);
yellowButton = new JRadioButton("Yellow", false);
eraserButton = new JRadioButton("Eraser", false);
add(redButton);
add(blueButton);
add(yellowButton);
add(eraserButton);
//add radio buttons to group
colorRadioGroup = new ButtonGroup();
colorRadioGroup.add(redButton);
colorRadioGroup.add(blueButton);
colorRadioGroup.add(yellowButton);
colorRadioGroup.add(eraserButton);
//create color objects
red = Color.RED;
blue = Color.BLUE;
yellow = Color.YELLOW;
white = Color.WHITE;
//register events
redButton.addItemListener(new RadioButtonHandler(red));
blueButton.addItemListener(new RadioButtonHandler(blue));
yellowButton.addItemListener(new RadioButtonHandler(yellow));
eraserButton.addItemListener(new RadioButtonHandler(white));
}
@Override
public void paintComponent(Graphics pen){
super.paintComponent(pen);
//draw points
for(Point point : pointList){
int x = (int) point.getX();
int y = (int) point.getY();
pen.setColor(selectedColor);
pen.fillOval(x, y, DIAMETER, DIAMETER);
}
}
//event handlers
private class RadioButtonHandler implements ItemListener{
private Color color;
public RadioButtonHandler(Color c){
color = c;
}
@Override
public void itemStateChanged(ItemEvent e){
if (e.getSource() == redButton){
selectedColor = Color.RED;
}
else if(e.getSource() == blueButton){
selectedColor = Color.BLUE;
}
else if(e.getSource() == yellowButton){
selectedColor = Color.YELLOW;
}
else if (e.getSource() == eraserButton){
selectedColor = Color.WHITE;
}
}
}
private class MouseDrawListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e){
//repaint();
draw(e);
}
@Override
public void mouseMoved(MouseEvent e){
/*Point point = e.getPoint();
pointList.add(point);
repaint();
draw(e);*/
}
void draw(MouseEvent e){
Point point = e.getPoint();
pointList.add(point);
repaint();
}
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
JFrame drawPanel = new JFrame("Drawing");
drawPanel.setSize(400,400);
DrawingTester drawing = new DrawingTester();
drawPanel.getContentPane().add(drawing);
drawPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawPanel.setVisible(true);
}
});
}
}