鼠标释放后似乎无法画出形状

时间:2016-05-24 02:53:58

标签: java user-interface

出于某种原因,我似乎无法弄清楚为什么一旦用户释放鼠标就无法绘制矩形。应该激活draw()方法并创建一个矩形对象以将对象绘制到drawPanel上。

ShapeGui.java类:

    public class ShapeGui extends JFrame{
    private JButton[] buttons;
    private final String[] butLabel =    {"Red","Green","Blue","Rectangle","Circle","Exit"};
    private Container pane;
    private JLabel dateLabel;
    private int x1,x2,y1,y2;
    private String color;
    JTextArea textArea;
    ButtonHandler butHandle = new ButtonHandler();
    private boolean isRect=true;  //set default draw shape when program run to rectangel
    JPanel drawPanel= new JPanel();


    public ShapeGui(){
        super("Draw Shape GUI");
        pane = getContentPane();
        pane.setLayout(new BorderLayout());
        pane.add(createButton(), BorderLayout.CENTER);
        pane.add(textArea(), BorderLayout.EAST);
        pane.add(drawSpace(), BorderLayout.WEST);
    }

    public JPanel createButton(){
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(2,3));
        buttons = new JButton[butLabel.length];

        for(int i=0; i<butLabel.length; i++){
            buttons[i]= new JButton(butLabel[i]);
            buttons[i].addActionListener(butHandle);
            buttonPanel.add(buttons[i]);
        }
        return buttonPanel;
    }

    public JPanel drawSpace(){
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new BorderLayout());
        leftPanel.setPreferredSize(new Dimension(200,450));

        //create panel for mouse click event
        drawPanel= new JPanel();
        leftPanel.add(drawPanel, BorderLayout.CENTER);
        MouseHandler mouseHandle = new MouseHandler();
        drawPanel.addMouseListener(mouseHandle);
        drawPanel.addMouseMotionListener(mouseHandle);
        //create date label and format to show only date
        LocalDate today = LocalDate.now(ZoneId.of("America/Montreal"));  
        dateLabel= new JLabel(today.toString());
        leftPanel.add(dateLabel,BorderLayout.SOUTH); //add date to panel

        return leftPanel;

    }
    public JPanel textArea(){
        JPanel textPanel= new JPanel();
        textArea= new JTextArea(15,15);
        //add textarea inside scroll pane 
        JScrollPane scroll = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        textPanel.add(scroll);

        return textPanel;
    }

    //method to draw shape after button is pressed
    public void drawShape(){
        if(isRect){
            Rectangle rect = new Rectangle(x1,y1,x2,y2,color);
            drawPanel.add(rect);
            rect.draw();
        }
    }

    private class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            Object obj = e.getSource();
            if(obj == buttons[0]){
                color="RED";
                System.out.println("Color: "+color);
            }
            if(obj == buttons[1]){
                color="GREEN";
                System.out.println("Color: "+color);
            }
            if(obj == buttons[2]){
                color="BLUE";
                System.out.println("Color: "+color);
            }
            if(obj == buttons[3]){
                isRect=true;
                System.out.println("Boolean Rectangle: "+isRect);
            }
            if(obj == buttons[4]){  //change to false once button circle is clicked
                isRect=false;
                System.out.println("Boolean Rectangle: "+isRect);
            }
            if(obj == buttons[5]){
                //serialization 
                System.exit(0);
            }
        }
    }  //end of buttonHandler
    private class MouseHandler extends MouseAdapter{
        @Override
        public void mousePressed(MouseEvent e){
            textArea.append(String.format("Pressed at [%d, %d]\n", e.getX(),e.getY()));
            x1=e.getX();
            y1=e.getY();
        }
        @Override
        public void mouseReleased(MouseEvent e){
            textArea.append(String.format("Released at [%d, %d]\n", e.getX(),e.getY()));
            x2=e.getX();
            y2=e.getY();
            drawShape();
        }
    }
}

矩形类:

    public class Rectangle extends JPanel{
    private int x1,x2,y1,y2;
    String color;

    public Rectangle(int x1, int y1, int x2, int y2,String color) {
        this.x1=x1;
        this.y1=y1;
        this.x2=x2;
        this.y2=y2;
        this.color=color;
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        //change color
        if(color.equalsIgnoreCase("Red"))
            g.setColor(Color.RED);
        else if(color.equalsIgnoreCase("Green")){
            g.setColor(Color.GREEN);
        }
        else
            g.setColor(Color.BLUE);
        g.fillRect(x1, y1, (x2-x1), (y2-y1));
    }
    public void draw(){
        repaint();
    }
}

1 个答案:

答案 0 :(得分:0)

我建议采用以下解决方案 - 重新绘制和绘画的过程很复杂,特别是如果你在面板上添加面板并依赖于系统 - 它并不意味着因为你说“绘制”它会立即绘制它:

class ShapeGui extends JFrame{
private JButton[] buttons;
private final String[] butLabel =    {"Red","Green","Blue","Rectangle","Circle","Exit"};
private Container pane;
private JLabel dateLabel;
private int x1,x2,y1,y2;
private Color color;
JTextArea textArea;
ButtonHandler butHandle = new ButtonHandler();
private boolean isRect=true;  //set default draw shape when program run to rectangel
DrawPanel drawPanel= new DrawPanel();

public JPanel drawSpace(){
    JPanel leftPanel = new JPanel();
    leftPanel.setLayout(new BorderLayout());
    leftPanel.setPreferredSize(new Dimension(200,450));

    //create panel for mouse click event
    leftPanel.add(drawPanel, BorderLayout.CENTER);
    MouseHandler mouseHandle = new MouseHandler();
    drawPanel.addMouseListener(mouseHandle);
    drawPanel.addMouseMotionListener(mouseHandle);
    //create date label and format to show only date
    Date today = new Date();  
    dateLabel= new JLabel(today.toString());
    leftPanel.add(dateLabel,BorderLayout.SOUTH); //add date to panel

    return leftPanel;

}


public void drawShape(){
    if(isRect){
    Rectangle rect = new Rectangle(x1,y1,x2,y2,color);
        drawPanel.add(rect);
        drawPanel.repaint();
    }
}

private class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        Object obj = e.getSource();
        if(obj == buttons[0]){
            color=Color.red;
            System.out.println("Color: "+color);
        }
        if(obj == buttons[1]){
            color=Color.green;
            System.out.println("Color: "+color);
        }
        if(obj == buttons[2]){
            color=Color.blue;
            System.out.println("Color: "+color);
        }
        if(obj == buttons[3]){
            isRect=true;
            System.out.println("Boolean Rectangle: "+isRect);
        }
        if(obj == buttons[4]){  //change to false once button circle is clicked
            isRect=false;
            System.out.println("Boolean Rectangle: "+isRect);
        }
        if(obj == buttons[5]){
            //serialization 
            System.exit(0);
        }
    }
}  //end of buttonHandler



class DrawPanel extends JPanel {
Rectangle rect;
private int x1,x2,y1,y2;
Color color;

public DrawPanel() {
}

public void add(Rectangle rect) {
  this.rect=rect;
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(rect.color);
    g.fillRect(rect.x1, rect.y1, rect.x2-rect.x1, rect.y2-rect.y1);
}
}

class Rectangle {
int x1,x2,y1,y2;
Color color;

public Rectangle(int x1, int y1, int x2, int y2, Color color) {
    this.x1=x1;
    this.y1=y1;
    this.x2=x2;
    this.y2=y2;
    this.color=color;
}
}

其余的保持不变。