2016-02-13 41 views
0

我创建了一个有2个圆圈和2个方块的简单GUI。点击鼠标时,形状将变为随机颜色。我希望我的形状只在鼠标在形状内部被点击时才改变颜色。此外,目前所有4个形状都会改变颜色,但我希望每个形状根据点击的形状而独立改变颜色。如何使它成为我的MousePressed侦听器仅在某个区域中单击时才响应的?

package weekThree; 

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.Random; 

import javax.swing.*; 

@SuppressWarnings("serial") 
public class taskTwo2 extends JPanel { 
    private static final int PREF_W = 200; 
    private static final int PREF_H = PREF_W; 
    private Color circleColor = Color.RED; //starting colour 
    private Color circleColor2 = Color.BLUE; 
    private Color squareColor = Color.GREEN; 
    private Color squareColor2 = Color.YELLOW; 
    private int circX = 10; 
    private int circY = circX; 
    private int circW = PREF_W - 2 * circX; 
    private int circH = PREF_H - 2 * circY; 

    public taskTwo2() { 
     addMouseListener(new MyMouse()); //mouse click listener 
    } 

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

     // to smooth out graphics 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //smoothes out edges 

     g2.setColor(circleColor); 
     g2.fillOval(circX, circY, circW, circH); 
     g2.setColor(Color.BLACK); 
     g2.drawOval(circX, circY, circW, circH); 


     g2.setColor(circleColor2); 
     g2.fillOval(25*(circX), 25*(circY), circW, circH); 
     g2.setColor(Color.BLACK); 
     g2.drawOval(25*(circX), 25*(circY), circW, circH); 


     g2.setColor(squareColor); 
     g2.fillRect(circX, 25*(circY), circW, circH); 
     g2.setColor(Color.BLACK); 
     g2.drawRect(circX, 25*(circY), circW, circH); 


     g2.setColor(squareColor2); 
     g2.fillRect(25*(circX), circY, circW, circH); 
     g2.setColor(Color.BLACK); 
     g2.drawRect(25*(circX), circY, circW, circH); 
    } 

    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
      return super.getPreferredSize(); 
     } 
     return new Dimension(PREF_W, PREF_H); 
    } 

    private class MyMouse extends MouseAdapter { 
     Random rand = new Random(); 

     @Override 
     public void mousePressed(MouseEvent e) { 
      float red = rand.nextFloat();  //generates a random value for the red tint value 
      float green = rand.nextFloat(); 
      float blue = rand.nextFloat(); 
      circleColor = new Color(red, green, blue); 

      float red2 = rand.nextFloat(); 
      float green2 = rand.nextFloat(); 
      float blue2 = rand.nextFloat(); 
      circleColor2 = new Color(red2, green2, blue2); 

      float red3 = rand.nextFloat(); 
      float green3 = rand.nextFloat(); 
      float blue3 = rand.nextFloat(); 
      squareColor = new Color(red3, green3, blue3); 

      float red4 = rand.nextFloat(); 
      float green4 = rand.nextFloat(); 
      float blue4 = rand.nextFloat(); 
      squareColor2 = new Color(red4, green4, blue4); 

      repaint(); //repaint components 
     } 
    } 

    private static void createAndShowGui() { //code for GUI visuals 
     JFrame frame = new JFrame("MyTaskToo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new taskTwo2()); 
     frame.setSize(500, 500); 
     frame.setVisible(true); 
    } 

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

感谢

回答

0

您可以通过使用Point mouseLocation = e.getPoint();然后,你必须要检查如果鼠标是圆的范围内得到您的mousePressed()方法鼠标位置。这个公式是简单地从圆心的Euclidean distance

/** 
* Checks if the mouse location is within the circle 
* @param mouseLocation the location of the mouse 
* @param cX circle center X location 
* @param cY circle center Y location 
* @param cR circle radius 
* @return true if the mouse is no more than cR units away from the center of the circle 
*/ 
private static boolean isInCircle(Point mouseLocation, double cX, double cY, double cR){ 
    final double dX = mouseLocation.getX()-cX; 
    final double dY = mouseLocation.getY()-cY; 
    return Math.sqrt(dX*dX+dY*dY)<=cR; 
} 

然后你只需要检查每一个圆圈。下面是第一个代码:

if(isInCircle(mouseLocation,circX+circW/2,circY+circH/2,circW/2)){ 
      float red = rand.nextFloat();  //generates a random value for the red tint value 
      float green = rand.nextFloat(); 
      float blue = rand.nextFloat(); 
      circleColor = new Color(red, green, blue); 
     } 

代码为正方形是相似的,但它使用的Taxicab distance

相关问题