5 Replies - 617 Views - Last Post: 04 May 2013 - 06:04 PM Rate Topic: -----

#1 dstevens  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 02-February 13

Animation in GUI Jframe

Posted 03 May 2013 - 04:58 PM

Please Help! Cant figure this out.
I am trying to rewrite a program given to me from my professor. I have a few days to figure this out. Time is running out. A program out of our books called REBOUND.java and our objective is to have the happyface to start and stop onclick. I rewrote the program and is as follows...
//**********************************************************************
//  ReboundPanel.java               Java Foundations
//
//  Represents the primary panel for the rebound program.
//**********************************************************************

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;
	
	//-------------------------------------------------------------------
	//  Sets up the panel, including the timer for the animation
	//-------------------------------------------------------------------
	
	public ReboundPanel()
	{
		timer = new Timer(DELAY, new ReboundListener());
		
		image = new ImageIcon ("happyface.gif");
		
		x = 0;
		y = 40;
		moveX = moveY = 3;
		
		setPreferredSize (new Dimension(WIDTH, HEIGHT));
		setBackground (Color.black);
		timer.start();
	}
	
	//--------------------------------------------------------------------
	//  Draw the image in the current location
	//--------------------------------------------------------------------
	
	public void paintComponent(Graphics page)
	{
		super.paintComponent(page);
		image.paintIcon(this,  page, x, y);
	}
	
	//*********************************************************************
	//  Represents the action Listener for the timer
	//*********************************************************************
	
	private class ReboundListener implements ActionListener
	{
		//******************************************************************
		//Updates the position of the image and the possibly the direction
		//of the 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();
		}
	}
}




I cant seem to see the happyface it is suppose to have in the GUI. can some one please help me get this figured out.
Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Animation in GUI Jframe

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 849
  • View blog
  • Posts: 2,585
  • Joined: 29-July 11

Re: Animation in GUI Jframe

Posted 03 May 2013 - 06:21 PM

Image image = new ImageIcon("file").getImage();

page.drawImage(image, x, y, null);


Was This Post Helpful? 1
  • +
  • -

#3 dstevens  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 02-February 13

Re: Animation in GUI Jframe

Posted 04 May 2013 - 03:50 PM

View Postfarrell2k, on 03 May 2013 - 06:21 PM, said:

Image image = new ImageIcon("file").getImage();

page.drawImage(image, x, y, null);



Where does the code on your line 3 go? is it suppose to go in the PaintComponent class or reboundPanel Class?
Please advise?
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 849
  • View blog
  • Posts: 2,585
  • Joined: 29-July 11

Re: Animation in GUI Jframe

Posted 04 May 2013 - 05:18 PM

the drawImage() call goes in paintComponent. The Image assignment is an instance declaration at the top of the class.
Was This Post Helpful? 0
  • +
  • -

#5 dstevens  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 02-February 13

Re: Animation in GUI Jframe

Posted 04 May 2013 - 05:59 PM

I tried to add a timer with a start stop when mouse is clicked, but it is not working at all. Could someone please help me. it is the If statements for the timer. any help is much appreciate. thank you

//**********************************************************************
//  ReboundPanel.java               Java Foundations
//
//  Represents the primary panel for the rebound program.
//**********************************************************************

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;
	boolean b;
	
	//-------------------------------------------------------------------
	//  Sets up the panel, including the timer for the animation
	//-------------------------------------------------------------------
	
	public ReboundPanel()
	{
		timer = new Timer(DELAY, new ReboundListener(){
			 @Override
			    public void actionPerformed(ActionEvent ae) {
			        // change polygon data
			        // ...

			        repaint();
			    }
		});
		
		image = new ImageIcon ("C:/Users/dstevens/Desktop/happyFace.gif");//.getImage();
		
		
		x = 0;
		y = 40;
		moveX = moveY = 3;
		
		
		setPreferredSize (new Dimension(WIDTH, HEIGHT));
		setBackground (Color.black);
		timer.start();
		
	
	}
	
	
	//--------------------------------------------------------------------
	//  Draw the image in the current location
	//--------------------------------------------------------------------
	
	 @Override
	public void paintComponent(Graphics page)
	{
		super.paintComponent(page);
		image.paintIcon(this,  page, x, y);
	}
	
	//*********************************************************************
	//  Represents the action Listener for the timer
	//*********************************************************************
	
	private class ReboundListener implements ActionListener
	{
		//******************************************************************
		//Updates the position of the image and the possibly the direction
		//of the movement whenever the timer fires an action event.
		//******************************************************************
		public void actionPerformed (ActionEvent event)
		{
			x += moveX;
			y += moveY;
			
			
		if (B)/> {
			 //timer.start();
					if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
				moveX = moveX * -1;
			
			if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
				moveY = moveY * -1;
			repaint();
		} else {
			   timer.stop();
						}
						b = !b;
		}
		}
	}


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: Animation in GUI Jframe

Posted 04 May 2013 - 06:04 PM

Related threads merged.

You didn't use a MouseListener, or really make any attempt at dealing with the mouse clicks.

Also, why do you override the actionPerformed() method in ReboundListener when you add an instance of it to your Timer? Referring to this snippet:
timer = new Timer(DELAY, new ReboundListener(){
29	             @Override
30	                public void actionPerformed(ActionEvent ae) {
31	                    // change polygon data
32	                    // ...
33	 
34	                    repaint();
35	                }
36	        });


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1