Results 1 to 6 of 6
  1. #1
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Question Button Within Animation

    While learning how to make animation I created this applet that shows a ball bouncing.
    The ball looses 10% of it's bounce strength every bounce untill it is just moving back and forth at the bottom of the screen. (I think I am going to create a strength for Y movement eventually.)
    What I am trying to do is turn the ball into a button so that the strength is reset to 1 when it is clicked. (Or the entire screen could be a button, It doesn't matter as long as a click brings the strength back.)
    Unfortunately, I can't just turn an image into a button.
    How can this be done.
    Thank you for your help.

    Java Code:
    import java.awt.*;
    
    public class Bounce extends javax.swing.JApplet
    	implements Runnable, {
    
    	Image ball;
    	float current = (float) 0;
    	float strength = (float) 1;
    	Thread runner;
    	int xPosition = 10;
    	int xMove = 4;
    	int yPosition = -1;
    	int ballHeight = 102;
    	int ballWidth = 111;
    	int height;
    	
    	Image workspace;
    	Graphics offscreen;
    
    	public void init() {
    		workspace = createImage(getSize().width + 1000, 1000);
    		offscreen = workspace.getGraphics();
    		setBackground(Color.white);
    		ball = getImage(getCodeBase(), "tennis.jpg");
    
    	}
    
    	public void paint(Graphics screen) {
    		Graphics2D screen2D = (Graphics2D) screen;
    		height = getSize().height - ballHeight;
    		if (yPosition == -1)
    			yPosition = height;
    		offscreen.setColor(Color.white);
    		offscreen.fillRect(0,0,getSize().width,getSize().height);
    		offscreen.drawImage(ball, (int) xPosition, (int) yPosition, this);
    		screen2D.drawImage(workspace, 0, 0, this);
    	}
    
    	public void start() {
    		if (runner == null) {
    			runner = new Thread(this);
    			runner.start();
    		}
    	}
    
    	public void run() {
    		Thread thisThread = Thread.currentThread();
    		while (runner == thisThread) {
    			repaint();
    			current += (float) 0.1;
    			if (current > 3) {
    				current = (float) 0;
    				strength = (float) (strength * 0.9);
    			}
    			xPosition += xMove;
    			if (xPosition > (getSize().width - 111))
    				xMove *= -1;
    			if (xPosition < 1)
    				xMove *= -1;
    			double bounce = Math.sin(current) * height * strength;
    			yPosition = (int) (height - bounce);
    			try {
    				Thread.sleep(25);
    			} catch (InterruptedException e) { }
    		}
    	}
    
    	public void update(Graphics screen) {
    		paint(screen);
    	}
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Before answering the question you need to completely restructure your code. The coding practices presented here are old techniques used for AWT applications and should NOT be used with Swing.

    When using Swing you should never override the paint() or update() methods.
    Custom painting is done by extending JPanel and adding custom painting code in the paintComponent() method. Read the section from the Swing tutorial on Custom Painting. Also, to control animation you should be using a Swing Timer. There is also a section in the tutorial on "How to Use Timers".

    Unfortunately, I can't just turn an image into a button.
    Add an ImageIcon to the button, then add an ActionListener to the button. If you use this approach, there is no need for custom painting. All you do is add the button to the panel and then move the button by using the setLocation(...) method.

    Or the entire screen could be a button,
    Add a MouseListener to the panel. This is closer to the code you have since the painting code will need to be moved to the paintComponent() method of your panel that you add to the applet.

  3. #3
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default MouseListeners are the easiest.

    Adding listerners to a project can get tricky if you are a beginner.
    A MouseListener is one of the easiest listeners to add to a component.
    Do not assume they are all as simple as this one.

    I have added a MouseListener to your code.
    I have not implemented the function that you wanted. I leave that to you.


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class Bounce extends javax.swing.JApplet
      implements Runnable, MouseListener {
    
      Image ball;
      float current = (float) 0;
      float strength = (float) 1;
      Thread runner;
      int xPosition = 10;
      int xMove = 4;
      int yPosition = -1;
      int ballHeight = 102;
      int ballWidth = 111;
      int height;
      
      Image workspace;
      Graphics offscreen;
    
      public void init() {
        workspace = createImage(getSize().width + 1000, 1000);
        offscreen = workspace.getGraphics();
        setBackground(Color.white);
        ball = getImage(getCodeBase(), "tennis.jpg");
        addMouseListener(this);
      }
    
      public void paint(Graphics screen) {
        Graphics2D screen2D = (Graphics2D) screen;
        height = getSize().height - ballHeight;
        if (yPosition == -1)
          yPosition = height;
        offscreen.setColor(Color.white);
        offscreen.fillRect(0,0,getSize().width,getSize().height);
        offscreen.drawImage(ball, (int) xPosition, (int) yPosition, this);
        screen2D.drawImage(workspace, 0, 0, this);
      }
    
      public void start() {
        if (runner == null) {
          runner = new Thread(this);
          runner.start();
        }
      }
    
      public void run() {
        Thread thisThread = Thread.currentThread();
        while (runner == thisThread) {
          repaint();
          current += (float) 0.1;
          if (current > 3) {
            current = (float) 0;
            strength = (float) (strength * 0.9);
          }
          xPosition += xMove;
          if (xPosition > (getSize().width - 111))
            xMove *= -1;
          if (xPosition < 1)
            xMove *= -1;
          double bounce = Math.sin(current) * height * strength;
          yPosition = (int) (height - bounce);
          try {
            Thread.sleep(25);
          } catch (InterruptedException e) { }
        }
      }
      
      public void mouseClicked(MouseEvent e){
        int x = e.getX();  int y = e.getY();
        if(xPosition < x && x < xPosition + ballWidth &&
           yPosition < y && y < yPosition + ballHeight){
             System.out.println("You're really on the ball!!");
        }
      }
      public void mouseEntered(MouseEvent e){;}
      public void mouseExited(MouseEvent e){;}
      public void mousePressed(MouseEvent e){;}
      public void mouseReleased(MouseEvent e){;}
    
      public void update(Graphics screen) {
        paint(screen);
      }
    }
    Last edited by paul pasciak; 03-19-2010 at 07:35 PM.

  4. #4
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default

    The coding practices presented here are old techniques used for AWT applications and should NOT be used with Swing.
    They are old techniques because I'm using a really outdated book that a friend lent me, the book is called "Sams, Teach Yourself Java 2 in 24 Hours"
    And Because we are not using java 2 anymore some of the stuff will be outdated. Ouch.

    Thank you for revising my code with the mouse listener for me. I have been playing around with MouseListener for a while but couldn't get it to work in this instance.

    I'll try the code and post back here if I have any more problems. Thank you for your detailed responses.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Download the Sun Tutorials. I would start by reading the Creating a GUI With Swing tutorial, which has sections on custom painting and on creating applets.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    [QUOTE=paul pasciak;110142]Adding listerners to a project can get tricky if you are a beginner.
    A MouseListener is one of the easiest listeners to add to a component.
    Do not assume they are all as simple as this one.

    I have added a MouseListener to your code.
    I have not implemented the function that you wanted. I leave that to you.


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    //...
    Are you really recommending that the OP draw directly in the JApplet itself and not in a JPanel, that they override paint and implement double buffering by hand instead of using Swing automatic double buffering?

    If so, why?

    to the OP, go with camickr's recommendation

Similar Threads

  1. Fish animation
    By SwEeTAcTioN in forum AWT / Swing
    Replies: 3
    Last Post: 11-30-2009, 10:56 AM
  2. need help about animation ?
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-30-2009, 11:41 AM
  3. Animation failure...
    By lordbob75 in forum New To Java
    Replies: 10
    Last Post: 05-11-2009, 02:06 AM
  4. Text animation
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-21-2008, 08:44 PM
  5. GUI Animation
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-11-2008, 03:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •