gpt4 book ai didi

java - 矩形绘图java Swing GUI的问题

转载 作者:行者123 更新时间:2023-11-29 10:10:28 27 4
gpt4 key购买 nike

我正在用 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());
}
}

关于java - 矩形绘图java Swing GUI的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38747589/

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