Results 1 to 6 of 6
Thread: Button Within Animation
- 03-19-2010, 03:15 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
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); } }
- 03-19-2010, 04:06 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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".
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.Unfortunately, I can't just turn an image into 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.Or the entire screen could be a button,
- 03-19-2010, 07:05 PM #3
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
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.
- 03-21-2010, 03:32 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
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"The coding practices presented here are old techniques used for AWT applications and should NOT be used with Swing.
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.
- 03-21-2010, 03:42 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
-
[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.
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?Java Code:import java.awt.*; import java.awt.event.*; //...
If so, why?
to the OP, go with camickr's recommendation
Similar Threads
-
Fish animation
By SwEeTAcTioN in forum AWT / SwingReplies: 3Last Post: 11-30-2009, 10:56 AM -
need help about animation ?
By h9h in forum Java 2DReplies: 1Last Post: 10-30-2009, 11:41 AM -
Animation failure...
By lordbob75 in forum New To JavaReplies: 10Last Post: 05-11-2009, 02:06 AM -
Text animation
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:44 PM -
GUI Animation
By serfster in forum New To JavaReplies: 2Last Post: 06-11-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks