我在鼠标点击图片时无法将图片换出。它应该切换到" redSmiley.gif"当从任何方向在30像素范围内点击图像并切换回" happyFace.gif"当它再次被点击时。这是我的主要方法。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Rebound
{
//-----------------------------------------------------------------
// Displays the main frame of the program.
//-----------------------------------------------------------------
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);
}
}
这是我的Panel Class。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ReboundPanel extends JPanel
{
private static final long serialVersionUID = 1L;
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;
//-----------------------------------------------------------------
// Sets up the panel, including the timer for the animation.
//-----------------------------------------------------------------
public ReboundPanel()
{
timer = new Timer(DELAY, new ReboundListener());
image = new ImageIcon (getClass().getResource("happyFace.gif"));
x = 0;
y = 40;
moveX = moveY = 3;
setPreferredSize (new Dimension(WIDTH, HEIGHT));
setBackground (Color.black);
timer.start();
}
//-----------------------------------------------------------------
// Draws the image in the current location.
//-----------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent (page);
image.paintIcon (this, page, x, y);
}
//*****************************************************************
// Represents the action listener for the timer.
//*****************************************************************
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();
}
}
class MousePressListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
if(event.getX() <= 30 && event.getY() <= 30)
{
if(event.getX() <= 30 && event.getY() <= 30)
{
image = new ImageIcon(getClass().getResource("redSmiley.gif"));
}
else
image = new ImageIcon(getClass().getResource("happyFace.gif"));
}
}
public void mouseClicked(MouseEvent event)
{
if(event.getX() <= 30 && event.getY() <= 30)
{
image = new ImageIcon(getClass().getResource("redSmiley.gif"));
}
else
image = new ImageIcon(getClass().getResource("happyFace.gif"));
}
//Empty methods
public void mouseEntered(MouseEvent arg0)
{
}
public void mouseExited(MouseEvent arg0)
{
}
public void mouseReleased(MouseEvent arg0)
{
}
}
}
如何在30像素内单击鼠标按钮时交换图像?
答案 0 :(得分:0)
您需要知道图像的渲染位置,并且需要将鼠标点与此进行比较...
一种方法是创建一个包含缓冲区的java.awt.Rectangle
...
Rectangle bounds = new Rectangle();
bounds.x = x - 30;
bounds.y = y - 30;
bounds.width = image.getWidth(this) + 60;
bounds.height = image.getHeight(this) + 60;
然后检查鼠标点是否在其边界内......
if (bounds.contains(event.getPoint()) {
// Did click image...