我正在用java编写一个程序,该程序根据鼠标坐标在屏幕上绘制一个矩形。但是,我很难为这个矩形找到正确的颜色。目标是在用户点击屏幕并选择颜色后,绘制一个具有正确颜色的矩形。我尝试过案例场景,但无法正常工作。对不工作的零件进行了注释。
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import java.awt.event.*;
public class test extends JFrame implements ActionListener, MouseListener, KeyListener {
Shape box = new Rectangle2D.Float(10, 10, 10, 10);
public test () {
setSize(250,150);
addMouseListener(this);
addKeyListener(this);
Color bgColor = new Color(125,125,125);
setBackground(bgColor);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
test frame = new test();
frame.setVisible(true);
}
});
}
public void actionPerformed(ActionEvent ae) {
}
public void drawRectangle(int x, int y) {
Graphics g = this.getGraphics();
// KeyEvent e = this.getKeyChar();
// switch (test.keyTyped()) {
// case b:
g.drawRect(x, y, x, y);
g.setColor(Color.BLUE);
g.fillRect(x, y, 2, 2);
// case r:
// g.drawRect(x, y, x, y);
// g.setColor(Color.RED);
// g.fillRect(x, y, 2, 2);
// case y:
// g.drawRect(x, y, x, y);
// g.setColor(Color.Yellow);
// g.fillRect(x, y, 2, 2);
// case g:
// g.drawRect(x, y, x, y);
// g.setColor(Color.GREEN);
// g.fillRect(x, y, 2, 2);
//}
}
int x, y;
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
c = Character.toLowerCase(c);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.white);
g.drawString("Click anywhere to draw a rectangle", 50, 250);
g.drawString("Choose color by pressing the corresponding key on your keyboard: ", 50, 270);
g.setColor(Color.blue);
g.drawString("B: Blue ", 50, 285);
g.setColor(Color.red);
g.drawString("R: Red ", 95, 285);
g.setColor(Color.yellow);
g.drawString("Y: Yellow ", 140, 285);
g.setColor(Color.green);
g.drawString("G: Green ", 195, 285);
drawRectangle(x, y);
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
您可以这样做:
HashMap<Integer, Color> colorsMap = new HashMap<>();
int selectedColor = Color.BLUE;
public test() {
....
colorsMap.put(KeyEvent.VK_B, Color.BLUE);
colorsMap.put(KeyEvent.VK_R, Color.RED);
colorsMap.put(KeyEvent.VK_Y, Color.YELLOW);
colorsMap.put(KeyEvent.VK_G, Color.GREEN);
....
}
public void drawRectangle(Graphics g, int x, int y) {
g.setColor(selectedColor);
g.fillRect(x, y, 2, 2);
}
@Override
public void paint(Graphics g) {
....
drawRectangle(g, x, y);
....
}
public void keyPressed(KeyEvent e) {
if(colorsMap.containsKey(e.getKeyCode())){
selectedColor = colorsMap.get(e.getKeyCode());
}
}
您正在对图形颜色状态的颜色进行硬编码:
g.setColor(Color.BLUE);
因此,无论用户选择什么,它都将保持蓝色,这一点也不奇怪。
建议:
- 不要硬编码,而是在这一行中使用Color变量,并在用户选择颜色时设置变量的状态
- 所以给你的类一个颜色字段,比如说
rectangleColor
- 在获得用户输入的方法中,设置该字段的值,然后调用
repaint()
- 不要在paint方法中绘制,而是在JPanel的paintComponent方法中绘制
- 不要使用
getGraphics()
来获取Graphics对象,而是使用JVM提供的Graphics对象在paintComponent方法中进行绘制 - 不要忘记调用超级绘制方法,例如paintComponent方法覆盖中的
super.paintComponent(g)
。这将允许JPanel进行房屋维护绘画