CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jan 2013
    Posts
    20

    Beginner coder using GUI JFrame

    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...
    Code:
    //**********************************************************************
    //  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
    Last edited by dstevens; May 3rd, 2013 at 06:44 PM.

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
  •  





Click Here to Expand Forum to Full Width

Featured