2014-03-02 134 views
1

好吧,所以基本上我試圖編寫一個GUI應用程序,在其主窗口繪製2個圓形和2個矩形。我試圖獲得它,以便每當用戶在圓形或矩形內單擊時,該特定的圓形或矩形將更改爲另一種隨機顏色。Java圖形,使用點擊事件改變圖形的顏色

目前,我已經知道MouseClick事件(屏幕上的任何位置)會導致所有圓或矩形將顏色更改爲相同的顏色。 這是我到目前爲止有:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Point; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.util.Random; 

import javax.swing.BorderFactory; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class Question2 { 
    public static void main(String[] args) { 
     SecondFrame f = new SecondFrame("Draw and Fill"); 
     f.init(); 
    } 
} 

class SecondFrame extends JFrame{ 
    SecondFrame(String title) { 
     super(title); 
    } 

    private JPanel mainPanel; 
    private GridBagConstraints gbc = new GridBagConstraints(); 
    private GridBagLayout gbLayout = new GridBagLayout(); 

    void init() { 
     mainPanel = new JPanel(); 
     mainPanel.setLayout(gbLayout); 
     this.setExtendedState(JFrame.MAXIMIZED_BOTH); 
     this.setContentPane(mainPanel); 
     gbc.gridheight = 1; 

     mainPanel.addMouseListener(new MouseListener(){ 

      @Override 
      public void mouseClicked(MouseEvent e) { 
       Point mousePosition; 
       mousePosition = mainPanel.getMousePosition(); 
       repaint(); 
      } 

      @Override 
      public void mouseEntered(MouseEvent e) { 

      } 

      @Override 
      public void mouseExited(MouseEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void mousePressed(MouseEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void mouseReleased(MouseEvent arg0) { 
       // TODO Auto-generated method stub 

      } 

     }); 

     this.pack(); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 
    } 

    public void paint(Graphics g){ 
    super.paint(g); 
    Random ran = new Random(); 
    // Assumes max and min are non-negative. 
    int red = 0 + ran.nextInt(255 - 0 + 1); 
    int green = 0 + ran.nextInt(255 - 0 + 1); 
    int blue = 0 + ran.nextInt(255 - 0 + 1); 

    Color myColor = new Color(red,green,blue); 
    g.setColor(myColor); 
    g.fillOval(50,50,200,200); 
    g.fillOval(50, 255, 200, 200); 
    g.fillRect(255,50,200,200); 
    g.fillRect(255, 255, 200, 200); 
    } 

} 

如果你可以點我在這將是非常讚賞的正確方向。謝謝。

回答

5

您將保留繪製對象的軌跡。然後檢查是否有任何這些對象發生點擊。如果是,請更改其顏色。例如,您可以使用Shape來表示簡單的表單。它的方法可用於確定點擊的點是否在形狀的邊界內。下面是一個例子,介紹ShapeItem,它有兩個屬性ShapeColor。並且,使用列表ShapeItem繪製實際形狀的面板。

也可以考慮一些小的便箋:

  • 不要在頂層容器直接作畫如JFrame。相反,請使用JPanelJComponent的擴展名;
  • 對於噴漆覆蓋paintComponent()而不是paint(),並且不要忘記撥打super.paintComponent(g);
  • 除非添加新功能,否則通常不需要擴展JFrame;

請參閱Performing Custom Painting教程和Closer Look at the Paint Mechanism部分,以獲取更多詳細信息。

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Shape; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.geom.Ellipse2D; 
import java.awt.geom.Rectangle2D; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class DemoShapes { 
    public static final Color DEFAULT_COLOR = Color.BLUE; 

    public DemoShapes() { 
     List<ShapeItem> shapes = new ArrayList<ShapeItem>(); 
     shapes.add(new ShapeItem(new Rectangle2D.Double(110, 1, 100, 100), 
       DEFAULT_COLOR)); 
     shapes.add(new ShapeItem(new Rectangle2D.Double(110, 110, 100, 100), 
       DEFAULT_COLOR)); 
     shapes.add(new ShapeItem(new Ellipse2D.Double(1, 1, 100, 100), 
       DEFAULT_COLOR)); 
     shapes.add(new ShapeItem(new Ellipse2D.Double(1, 110, 100, 100), 
       DEFAULT_COLOR)); 

     JFrame frame = new JFrame("Shapes"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     ShapesPanel panel = new ShapesPanel(shapes); 
     frame.add(panel); 

     frame.setLocationByPlatform(true); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    class ShapeItem { 
     private Shape shape; 
     private Color color; 

     public ShapeItem(Shape shape, Color color) { 
      super(); 
      this.shape = shape; 
      this.color = color; 
     } 

     public Shape getShape() { 
      return shape; 
     } 

     public void setShape(Shape shape) { 
      this.shape = shape; 
     } 

     public Color getColor() { 
      return color; 
     } 

     public void setColor(Color color) { 
      this.color = color; 
     } 
    } 

    class ShapesPanel extends JPanel { 
     private List<ShapeItem> shapes; 
     private Random rand = new Random(); 

     public ShapesPanel(List<ShapeItem> shapesList) { 
      this.shapes = shapesList; 

      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 

        Color color = getRandomColor(); 
        for (ShapeItem item : shapes) { 
         if (item.getShape().contains(e.getPoint())) { 
          item.setColor(color); 
         } 
        } 
        repaint(); 
       } 
      }); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 

      Graphics2D g2 = (Graphics2D) g.create(); 

      for (ShapeItem item : shapes) { 
       g2.setColor(item.getColor()); 
       g2.fill(item.getShape()); 
      } 

      g2.dispose(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(300, 300); 
     } 

     private Color getRandomColor() { 
      return new Color(rand.nextFloat(), rand.nextFloat(), 
       rand.nextFloat()); 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new DemoShapes(); 
      } 
     }); 
    } 
} 

enter image description here

+0

什麼用超'();''在ShapeItem'的構造? – cyberpirate92

+0

@cyberPheonix它調用父類的構造函數。在這種情況下,這是由Java自動完成的,並且不需要顯式添加語句來調用父級的默認構造函數。它可能是由Eclipse的* Generate構造函數使用fields ... *選項自動生成的。 – tenorsax

0

您應該使用mousePosition來確定該點是否在任何橢圓或矩形的範圍內。如果是,請執行g.setColor(myColor)並填寫該形狀。然後將g更改爲正常的非隨機顏色,並填充其餘的形狀。