简体   繁体   English

矩形绘图java Swing GUI的问题

[英]Trouble with rectangle drawing java Swing GUI

I am writing a program with java that draws a rectangle to the screen based on mouse coordinates. 我正在用Java编写一个程序,该程序根据鼠标坐标在屏幕上绘制一个矩形。 However, I am having trouble getting the correct color for this rectangle. 但是,我在获取此矩形的正确颜色时遇到了麻烦。 The goal is to draw a rectangle with correct color after the user has clicked the screen and chosen a color. 目的是在用户单击屏幕并选择一种颜色后绘制具有正确颜色的矩形。 I tried case scenarios but can't get it to work properly. 我尝试了案例方案,但无法使其正常工作。 The not working parts are commented. 不起作用的部分被注释。

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      
    }   
}

You're hard-coding the color of your Graphics Color state: 您正在硬编码“图形颜色”状态的颜色:

g.setColor(Color.BLUE);

So there should be no surprise that it will remain blue no matter what the user chooses. 因此,无论用户选择什么,它都将保持蓝色并不奇怪。

Suggestions: 意见建议:

  • Don't hard code this but instead use a Color variable in this line, and set the state of the variable when the user chooses a color. 不要对此进行硬编码,而应在此行中使用Color变量,并在用户选择颜色时设置变量的状态。
  • So give your class a Color field, say called rectangleColor 因此,为您的班级提供一个Color字段,比如叫rectangleColor
  • In the methods where you get user input, set the value of this field, and call repaint() . 在获取用户输入的方法中,设置此字段的值,然后调用repaint()
  • Don't draw within the paint method but rather a JPanel's paintComponent method. 不要在paint方法中进行绘制,而应在JPanel的paintComponent方法中进行绘制。
  • Don't use getGraphics() to get your Graphics object -- instead do your drawing in your paintComponent method using the Graphics object that the JVM gives you. 不要使用getGraphics()获取Graphics对象-而是使用JVM为您提供的Graphics对象在paintComponent方法中绘制图形。
  • Don't forget to call the super painting method, eg, super.paintComponent(g) in your paintComponent method override. 不要忘记调用超级绘画方法,例如,paintComponent方法重写中的super.paintComponent(g) This will allow the JPanel to do its house-keeping painting. 这将使JPanel可以做其内务绘画。

You can do something like this: 您可以执行以下操作:

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM