- Alternating Images on MouseClick
- 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(Stringu005Bu005D 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)
- {
- g.clearRect(0, 0, getWidth(), getHeight());
- g.drawImage(image.getImage(), imageX, imageY, null);
- super.paintComponent(g);
- }
- 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);
- }
- }
- }