Java 鼠标单击上的交替图像

Java 鼠标单击上的交替图像,java,image,swing,mouselistener,Java,Image,Swing,Mouselistener,我正在和MouseStener合作,特别是MouseClick,还有一张图片。图像在面板上移动 如何使其在距当前位置30像素范围内单击时,图像交替显示 这是我的相框 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Rebound { public static void main (String[] args) { JFrame frame = new JFrame ("Reb

我正在和MouseStener合作,特别是MouseClick,还有一张图片。图像在面板上移动

如何使其在距当前位置30像素范围内单击时,图像交替显示

这是我的相框

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Rebound
{

public static void main (String[] args)
{
   JFrame frame = new JFrame ("Rebound");
   frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

  frame.getContentPane().add(new ReboundPanel());
  frame.pack();
  frame.setVisible(true);
}
}
这是我的面板

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

 public class ReboundPanel extends JPanel
 {
   private final int WIDTH = 300, HEIGHT = 100;
   private final int DELAY = 20, IMAGE_SIZE = 35;

   private ImageIcon image;
   private Timer timer;
   private int x, y, moveX, moveY;
   private ImageIcon image2;



public ReboundPanel()
{
  timer = new Timer(DELAY, new ReboundListener());

  image = new ImageIcon("C:\\happyFace.gif");
  image2 = new ImageIcon("C:\\red smiley.gif");


  x = 0;
  y = 40;
  moveX = moveY = 3;      

  setPreferredSize (new Dimension(WIDTH, HEIGHT));
  setBackground (Color.black);
  addMouseListener(new MousePressListener());
  timer.start();
}

public void paintComponent (Graphics page)
{
   super.paintComponent (page);
   image.paintIcon (this, page, x, y);
}

   class MousePressListener implements MouseListener 
   { 
      public void mousePressed(MouseEvent event) {}
      public void mouseReleased(MouseEvent event) {} 
      public void mouseClicked(MouseEvent event) {

          image = image2;

  } 
  public void mouseEntered(MouseEvent event) {} 
  public void mouseExited(MouseEvent event) {} 
 } 

 private class ReboundListener implements ActionListener
 {
  //--------------------------------------------------------------
  //  Updates the position of the image and possibly the direction
  //  of movement whenever the timer fires an action event.
  //--------------------------------------------------------------
  public void actionPerformed (ActionEvent event)
  {
     x += moveX;
     y += moveY;

     if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
        moveX = moveX * -1;

     if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
        moveY = moveY * -1;

     repaint();
  }
 }
}

尝试此示例代码并询问可能出现的任何问题:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class LocateMouseExample {   
    /*
     * This is just JFrame, that we be 
     * using as the Base for our Application.
     * Though here we are calling our
     * JPanel (CustomPanel), whose
     * paintComponent(...) method, we had
     * override.
     */
    private void createAndDisplayGUI() {
        JFrame frame = new JFrame("Locate Mouse Position");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        CustomPanel contentPane = new CustomPanel();
        frame.setContentPane(contentPane);  
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String\u005B\u005D args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new LocateMouseExample().createAndDisplayGUI();
            }
        });
    }
}

class CustomPanel extends JComponent {

    private final int SIZE = 50;
    private int imageX = 100;
    private int imageY = 100;
    private int imageIndex;

    private ImageIcon image;
    private ImageIcon firstImage;
    private ImageIcon secondImage;
    private java.net.URL url;

    private Rectangle boundsForMouse;

    public CustomPanel() {
        image = new ImageIcon();
        try {
            url = new java.net.URL("http://gagandeepbali.uk.to/gaganisonline/images/eclipse/caIcon.png");
            firstImage = new ImageIcon(url);
            url = new java.net.URL("http://gagandeepbali.uk.to/gaganisonline/images/swing/share/Keyboard.png");
            secondImage = new ImageIcon(url);
        } catch(Exception e) {
            e.printStackTrace();
        }
        imageIndex = 1;
        image.setImage(firstImage.getImage());

        boundsForMouse = new Rectangle(imageX - 30,
                                        imageY - 30,
                                        firstImage.getIconWidth() + 60,
                                        firstImage.getIconHeight() + 60);       
        setOpaque(true);        
        addMouseListener(new MouseController());         
    }

    private int setImage(int counter) {
        System.out.println("Image Index : " + counter);
        if (counter == 1) {
            image = new ImageIcon();
            image.setImage(secondImage.getImage());
            boundsForMouse = new Rectangle(imageX - 30,
                                        imageY - 30,
                                        secondImage.getIconWidth() + 60,
                                        secondImage.getIconHeight() + 60);
            repaint();
            counter++;
            return (counter);
        } else if (counter == 2) {   
            image = new ImageIcon();    
            image.setImage(firstImage.getImage());
            boundsForMouse = new Rectangle(imageX - 30,
                                        imageY - 30,
                                        firstImage.getIconWidth() + 60,
                                        firstImage.getIconHeight() + 60);
            repaint();
            return (--counter);
        }
        return 1;
    }

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

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.clearRect(0, 0, getWidth(), getHeight());
        g.drawImage(image.getImage(), imageX, imageY, null);
    }

    private class MouseController extends MouseAdapter {
        public void mouseClicked(MouseEvent me) {           
            int xHitOnPanel = me.getX();
            int yHitOnPanel = me.getY();
            System.out.println("X HIT : " + xHitOnPanel);
            System.out.println("Y HIT : " + yHitOnPanel);
            System.out.println("RECTANGLE BOUNDS X : " + boundsForMouse.x);
            System.out.println("RECTANGLE BOUNDS Y : " + boundsForMouse.y);

            if (boundsForMouse.contains(xHitOnPanel, yHitOnPanel))
                imageIndex = setImage(imageIndex);
        }
    }
}

我试图让它在距离当前位置30像素的范围内点击时,图像会交替出现。因此,我找到了一种在图像之间交替出现的方法。我想还有其他人。我补充说:私人布尔颜色;我将我的mouseClickedMouseEvent事件更改为:public void mouseClickedMouseEvent事件{ifcolor==true{image=new ImageIconC:\\red smiley.gif;color=false;}否则{image=new ImageIconC:\\happyFace.gif;color=true;}@user1349999:看看这个。一定要阅读这个线程,然后看看下面的答案