7 Replies - 15226 Views - Last Post: 12 April 2012 - 08:57 PM Rate Topic: -----

#1 mikeuk20  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 17-April 09

How to create a circle eclipse that will move?

Posted 20 April 2009 - 09:25 AM

Hey guys, I posted earlier asking how to insert an image into this game im creating for the game PONG. Im new to java so was hoping somebody could help me out. Instead of inserting an image I just want to create a simple eclipse shape that will move. Heres my code :

 import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.util.*;
   import java.awt.geom.*;
   import java.applet.AudioClip;
   import java.net.*;

public class BouncingBall extends JPanel implements Runnable, KeyListener {
	// Set up variables
	
	  Image ball;  
	  char key;
	  AudioClip slap;
	  boolean right = false;
	  boolean left = false;
	  int direction = 0;
	  int ballHeight = 30;
	  int ballWidth = 30;
	  int xPosition = 10;
	  int yPosition = 2;
	  int xMove = 1;
	  int yMove = 2;
	  float p1xPosition = 200;
	  float p1xMove = 0;
	  float compxPosition = 400;
	  float compxMove = 1;
	  int height;
	  Thread Ball;
	  boolean lol = false;
  
   //Contructer method, sets up Thread called Ball and image called ball.
  

	/** Creates a new instance of BouncingBall */
	public BouncingBall() {
		
		 Ball = new Thread(this);
		 Ball.start();

	}
	 public void keyTyped (KeyEvent e) {
	  
	  }
	   
	   public void keyPressed (KeyEvent evt) {
		 if (evt.getKeyCode() == KeyEvent.VK_LEFT)
			right = true;
		 if (evt.getKeyCode() == KeyEvent.VK_RIGHT)
			left = true;
	  }
	   
	   public void keyReleased (KeyEvent evt) {
		 if (evt.getKeyCode() == KeyEvent.VK_LEFT)
			left = false;
		 if (evt.getKeyCode() == KeyEvent.VK_RIGHT)
			right = false;
	  
	  }
		  
   // Paints the necessary graphics for the program.
  
	   public void paintComponent(Graphics comp) {
		 Graphics2D comp2D = (Graphics2D) comp;
		 comp2D.setColor(Color.white);
		 comp2D.fillRect(0, 0, getSize().width, getSize().height);
		 height = getSize().height - ballHeight;
		 comp2D.setColor(Color.black);
		 BasicStroke pen = new BasicStroke(12F);
		 comp2D.setStroke(pen);
		 Line2D.Float ln1 = new Line2D.Float(p1xPosition, (getSize().height - 20F), (p1xPosition + (getSize().width / 10F)), (getSize().height - 20F));
		 comp2D.draw(ln1);
		 Line2D.Float ln2 = new Line2D.Float(compxPosition, 20F, (compxPosition + (getSize().width / 10F)), 20F);
		 comp2D.draw(ln2);
		 if (yPosition == -1)
			yPosition = height - 20;
		 if (ball != null) {
			comp2D.drawImage(ball,
			   (int) xPosition,
			   (int) yPosition,
			   this);
		 }
	  
		 if ( right == true )
			direction = 2;
		 else  if ( left == true ) {
			direction = 1;
		 
			}
		  
		 
		 else {
			direction = 0; }
		   
		 switch(direction) {
			case 1: p1xPosition += 1;
			   break;
			case 2: p1xPosition -= 1;
			   break;
		 }
				  
		  
	  }
	   
	   //Thread creation
	   
	   public void run(){
		 Thread thisThread = Thread.currentThread();
		 while (Ball == thisThread) {
		
		 //Ball movement
			xPosition += xMove;
			if (xPosition > (getSize().width - ballWidth)) {
				
			   xMove *= -1;
			}
			if (xPosition < 1) {
				
			   xMove *= -1;
			}
			yPosition += yMove;
			if(yPosition < 1) {
				
			   yMove *= -1;
			}
				 
			if(yPosition > (getSize().height - ballHeight)) {
				
			   yMove *= -1;
			}
				 
			 //Computer Movement	
			compxPosition += compxMove;
			if (compxPosition < xPosition)
			   compxMove = 1;
			if (compxPosition < 1)
			   compxMove = 1;
				 
			if (compxPosition > xPosition)
			   compxMove = -1;
			if ((compxPosition + (getSize().width / 10)) > (getSize().width))
			   compxMove = -1;
				 
			 //Tests to see whether the ball is touching the pads.
			 
			 
				 
			 
			 //Attempted player movement
			 
			 
								
			p1xPosition += p1xMove;
			
			repaint();		//Repaints graphics
				 
			try {
			   Thread.sleep(16);		//frame pause 16 miliseconds
			}
				catch (InterruptedException e) { }
		 }
	  }

}



This where I would like the shape to be however I'm stuck on how to actually create the ball:
	
	public BouncingBall() {
		
		 Ball = new Thread(this);
		 Ball.start();

	}



Thanks Alot :)

This post has been edited by mikeuk20: 20 April 2009 - 09:28 AM


Is This A Good Question/Topic? 1

Replies To: How to create a circle eclipse that will move?

#2 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,450
  • Joined: 24-September 08

Re: How to create a circle eclipse that will move?

Posted 20 April 2009 - 09:40 AM

Wow. That's a lot of unnecessary code. Just make a repaint method? >_>
Was This Post Helpful? 0
  • +
  • -

#3 mikeuk20  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 17-April 09

Re: How to create a circle eclipse that will move?

Posted 20 April 2009 - 11:09 AM

Like I said im quite new to java and don't really understand a repaint method
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,450
  • Joined: 24-September 08

Re: How to create a circle eclipse that will move?

Posted 20 April 2009 - 12:04 PM

Oh sorry. I'm working on a graphics tutorial. A paint method is a method you make in your "ball" class or whatever. The application will draw the graphics over and over again automatically. Here is an example of a lab I did. Its kinda basic, but it gives the basics for moving. You can change the speed and whatnot. I don't care if you use it.


package cs241lab5;

/**
 *
 * This is to make a new ball and such.
 * @author Erica and Veejay
 * 
 */

import java.awt.*;
import java.util.*;

public class Ball {

   private int xCoord;
   private int yCoord;
   private int height;
   private int width;
   private int rise;
   private int run;
   private Color color;
   private int panelHeight,panelWidth;
  
   
   public Ball() {
       
       setXCoord();
       setYCoord();
       setHeight();
       setWidth();
       setRise();
       setRun();
       setColor();
      
   }//End constructor
   
   /** This method sets the initial x-coordinate */
   public void setXCoord() {
       
       int max = 51;
       Random rand = new Random();
       int tempX = rand.nextInt(max);
       xCoord = tempX;
       
   }//End setXCoord method
   
   public int getXCoord() {
       
       return(xCoord);
       
   }//End getXCoord method
   
   /** This method sets the initial y-coordinate */
   public void setYCoord() {
       
       int max = 51;
       Random rand = new Random();
       int tempY = rand.nextInt(max);
       yCoord = tempY;
       
   }//End setYCoord method
   
   public int getYCoord() {
       
       return(yCoord);
       
   }//End getYCoord method
   
   /** This method sets the initial height */
   public void setHeight() {
       
       int max = 11;
       Random rand = new Random();
       int tempH = rand.nextInt(max) + 10;
       height = tempH;
       
   }//End setHeight method
   
   public int getHeight() {
       
       return(height);
              
   }//End getHeight method
   
   /** This method sets the initial width */
   public void setWidth() {
       
       int max = 11;
       Random rand = new Random();
       int tempW = rand.nextInt(max) + 10;
       width = tempW;
       
   }//End setWidth method
   
   public int getWidth() {
       
       return(width);
       
   }//End getWidth method
   
   /** This method sets the initial rise */
   public void setRise() {
       
       int max = 11;
       Random rand = new Random();
       int tempR = rand.nextInt(max);
       rise = tempR;
       
   }//End setRise method
   
   public int getRise() {
       
       return(rise);
       
   }//End getRise method
   
   /** This method sets the initial run */
   public void setRun() {
       
       int max = 11;
       Random rand = new Random();
       int tempR = rand.nextInt(max);
       run = tempR;
       
   }//End setRise method
   
   public int getRun() {
       
       return(run);
       
   }//End getRun method
   
   /** This method sets a random color to color */
   public void setColor() {
       
       int red = (int)(Math.random() * 255);
       int green = (int)(Math.random() * 255);
       int blue = (int)(Math.random() * 255);
       Color randomColor = new Color(red, green, blue);
       color = randomColor;
       
   }//End setColor method
   
   public Color getColor() {
       
       return(color);
       
   }//End getColor method
   
   public void paintComponent (Graphics g) {
       
        g.setColor(color);
        g.fillOval(xCoord, yCoord, width, height);

    } // end method paintComponent
   
   public void move() {
       
       
        // If ball is approaching a wall, reverse direction
        if (xCoord < (0 - run) || xCoord > (panelWidth - width)) { 
            run = -run; 
        }
        
	if (yCoord < (0 - rise) || yCoord > (panelHeight - height)) { 
            rise = -rise; 
        }
        
        // "Move" ball according to values in rise and run
        xCoord += run;
        yCoord += rise;
        
        
        
   }//End move method
   
   public void detectCollision(Ball b1, Ball b2){
       
       
       
   }//End detectCollision method

   public void setPanelHeight(int panelHeight) {
        
        this.panelHeight = panelHeight;
        
    }//End setPanelHeight

    public void setPanelWidth(int panelWidth) {
        
        this.panelWidth = panelWidth;
        
    }//End setPanelWidth
    
}//End Ball class



Was This Post Helpful? 1
  • +
  • -

#5 mikeuk20  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 17-April 09

Re: How to create a circle eclipse that will move?

Posted 20 April 2009 - 12:06 PM

Thanks NeoTifa.. much appreciated :)
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,450
  • Joined: 24-September 08

Re: How to create a circle eclipse that will move?

Posted 20 April 2009 - 12:38 PM

Click the thank you button then :P
Was This Post Helpful? 0
  • +
  • -

#7 MisterCasual  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-April 12

Re: How to create a circle eclipse that will move?

Posted 12 April 2012 - 01:33 PM

Sorry i am also new to eclipse and i rewrote that whole chunk of code but now this might sound nooby but how can i make it so i can control the ball we just made with the arrow keys?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to create a circle eclipse that will move?

Posted 12 April 2012 - 08:57 PM

You will need a KeyListener on your JPanel

http://docs.oracle.c...eyListener.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1