2016-05-24 5 views
0

Aus irgendeinem Grund kann ich nicht herausfinden, warum ich nicht Rechteck zum Zeichnen bekommen kann, sobald der Benutzer die Maus loslassen. Das sollte die draw() Methode aktivieren und ein Rechteckobjekt erstellen, um das Objekt auf die drawPanel zu zeichnen.kann nicht scheinen, um Form zu zeichnen, nachdem die Maus freigegeben wird

ShapeGui.java Klasse:

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

Rectangle-Klasse:

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

Antwort

0

Ich schlage vor, die folgende Lösung - den Prozess der neu zu streichen und Malerei kompliziert ist vor allem, wenn Sie Panels hinzufügen auf Panels und verlässt sich auf das System - es bedeutet nicht, dass, weil Sie "draw" sagten, es sofort zeichnet:

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

Der Rest bleibt gleich.

+0

Vielen Dank! Ich verstehe meist, was Sie getan haben, das war sehr hilfreich .. jedoch scheint ich "Ausnahme im Thread" AWT-EventQueue-0 "java.lang.NullPointerException" bei der PaintComponent-Methode zu bekommen. Aus irgendeinem Grund führte das Programm die Methode weiter. aber es läuft weiter, nachdem ich meine Maus gezogen und losgelassen habe. –

Verwandte Themen