-1

私はMouseListener、具体的には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();
  }
 }
}
4

1 に答える 1

2

このサンプルコードを試して、発生する可能性のある質問をしてください。

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);
        }
    }
}
于 2012-04-27T05:23:44.877 に答える