Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 6.22 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Alternating Images on MouseClick
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5.  
  6. public class Rebound
  7. {
  8.  
  9. public static void main (String[] args)
  10. {
  11.    JFrame frame = new JFrame ("Rebound");
  12.    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  13.  
  14.   frame.getContentPane().add(new ReboundPanel());
  15.   frame.pack();
  16.   frame.setVisible(true);
  17. }
  18. }
  19.        
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import javax.swing.*;
  23.  
  24.  public class ReboundPanel extends JPanel
  25.  {
  26.    private final int WIDTH = 300, HEIGHT = 100;
  27.    private final int DELAY = 20, IMAGE_SIZE = 35;
  28.  
  29.    private ImageIcon image;
  30.    private Timer timer;
  31.    private int x, y, moveX, moveY;
  32.    private ImageIcon image2;
  33.  
  34.  
  35.  
  36. public ReboundPanel()
  37. {
  38.   timer = new Timer(DELAY, new ReboundListener());
  39.  
  40.   image = new ImageIcon("C:\happyFace.gif");
  41.   image2 = new ImageIcon("C:\red smiley.gif");
  42.  
  43.  
  44.   x = 0;
  45.   y = 40;
  46.   moveX = moveY = 3;      
  47.  
  48.   setPreferredSize (new Dimension(WIDTH, HEIGHT));
  49.   setBackground (Color.black);
  50.   addMouseListener(new MousePressListener());
  51.   timer.start();
  52. }
  53.  
  54. public void paintComponent (Graphics page)
  55. {
  56.    super.paintComponent (page);
  57.    image.paintIcon (this, page, x, y);
  58. }
  59.  
  60.    class MousePressListener implements MouseListener
  61.    {
  62.       public void mousePressed(MouseEvent event) {}
  63.       public void mouseReleased(MouseEvent event) {}
  64.       public void mouseClicked(MouseEvent event) {
  65.  
  66.           image = image2;
  67.  
  68.   }
  69.   public void mouseEntered(MouseEvent event) {}
  70.   public void mouseExited(MouseEvent event) {}
  71.  }
  72.  
  73.  private class ReboundListener implements ActionListener
  74.  {
  75.   //--------------------------------------------------------------
  76.   //  Updates the position of the image and possibly the direction
  77.   //  of movement whenever the timer fires an action event.
  78.   //--------------------------------------------------------------
  79.   public void actionPerformed (ActionEvent event)
  80.   {
  81.      x += moveX;
  82.      y += moveY;
  83.  
  84.      if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
  85.         moveX = moveX * -1;
  86.  
  87.      if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
  88.         moveY = moveY * -1;
  89.  
  90.      repaint();
  91.   }
  92.  }
  93. }
  94.        
  95. import java.awt.*;
  96. import java.awt.event.*;
  97. import java.awt.image.BufferedImage;
  98. import javax.swing.*;
  99.  
  100. public class LocateMouseExample
  101. {  
  102.     /*
  103.      * This is just JFrame, that we be
  104.      * using as the Base for our Application.
  105.      * Though here we are calling our
  106.      * JPanel (CustomPanel), whose
  107.      * paintComponent(...) method, we had
  108.      * override.
  109.      */
  110.     private void createAndDisplayGUI()
  111.     {
  112.         JFrame frame = new JFrame("Locate Mouse Position");
  113.         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  114.  
  115.         CustomPanel contentPane = new CustomPanel();
  116.         frame.setContentPane(contentPane);  
  117.         frame.pack();
  118.         frame.setLocationByPlatform(true);
  119.         frame.setVisible(true);
  120.     }
  121.  
  122.     public static void main(Stringu005Bu005D args)
  123.     {
  124.         SwingUtilities.invokeLater(new Runnable()
  125.         {
  126.             public void run()
  127.             {
  128.                 new LocateMouseExample().createAndDisplayGUI();
  129.             }
  130.         });
  131.     }
  132. }
  133.  
  134. class CustomPanel extends JComponent
  135. {
  136.     private final int SIZE = 50;
  137.     private int imageX = 100;
  138.     private int imageY = 100;
  139.     private int imageIndex;
  140.  
  141.     private ImageIcon image;
  142.     private ImageIcon firstImage;
  143.     private ImageIcon secondImage;
  144.     private java.net.URL url;
  145.  
  146.     private Rectangle boundsForMouse;
  147.  
  148.     public CustomPanel()
  149.     {
  150.         image = new ImageIcon();
  151.         try
  152.         {
  153.             url = new java.net.URL("http://gagandeepbali.uk.to/gaganisonline/images/eclipse/caIcon.png");
  154.             firstImage = new ImageIcon(url);
  155.             url = new java.net.URL("http://gagandeepbali.uk.to/gaganisonline/images/swing/share/Keyboard.png");
  156.             secondImage = new ImageIcon(url);
  157.         }
  158.         catch(Exception e)
  159.         {
  160.             e.printStackTrace();
  161.         }
  162.         imageIndex = 1;
  163.         image.setImage(firstImage.getImage());
  164.  
  165.         boundsForMouse = new Rectangle(imageX - 30,
  166.                                         imageY - 30,
  167.                                         firstImage.getIconWidth() + 60,
  168.                                         firstImage.getIconHeight() + 60);      
  169.         setOpaque(true);        
  170.         addMouseListener(new MouseController());        
  171.     }
  172.  
  173.     private int setImage(int counter)
  174.     {
  175.         System.out.println("Image Index : " + counter);
  176.         if (counter == 1)
  177.         {
  178.             image = new ImageIcon();
  179.             image.setImage(secondImage.getImage());
  180.             boundsForMouse = new Rectangle(imageX - 30,
  181.                                         imageY - 30,
  182.                                         secondImage.getIconWidth() + 60,
  183.                                         secondImage.getIconHeight() + 60);
  184.             repaint();
  185.             counter++;
  186.             return (counter);
  187.         }  
  188.         else if (counter == 2)
  189.         {  
  190.             image = new ImageIcon();    
  191.             image.setImage(firstImage.getImage());
  192.             boundsForMouse = new Rectangle(imageX - 30,
  193.                                         imageY - 30,
  194.                                         firstImage.getIconWidth() + 60,
  195.                                         firstImage.getIconHeight() + 60);
  196.             repaint();
  197.             return (--counter);
  198.         }
  199.         return 1;
  200.     }
  201.  
  202.     @Override
  203.     public Dimension getPreferredSize()
  204.     {
  205.         return (new Dimension(500, 500));
  206.     }
  207.  
  208.     @Override
  209.     public void paintComponent(Graphics g)
  210.     {
  211.         g.clearRect(0, 0, getWidth(), getHeight());
  212.         g.drawImage(image.getImage(), imageX, imageY, null);
  213.         super.paintComponent(g);        
  214.     }
  215.  
  216.     private class MouseController extends MouseAdapter
  217.     {
  218.         public void mouseClicked(MouseEvent me)
  219.         {          
  220.             int xHitOnPanel = me.getX();
  221.             int yHitOnPanel = me.getY();
  222.             System.out.println("X HIT : " + xHitOnPanel);
  223.             System.out.println("Y HIT : " + yHitOnPanel);
  224.             System.out.println("RECTANGLE BOUNDS X : " + boundsForMouse.x);
  225.             System.out.println("RECTANGLE BOUNDS Y : " + boundsForMouse.y);
  226.  
  227.             if (boundsForMouse.contains(xHitOnPanel, yHitOnPanel))
  228.                 imageIndex = setImage(imageIndex);
  229.         }
  230.     }
  231. }