Total members 11894 |It is currently Sat Nov 23, 2024 1:25 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






Project Name: Bouncing Coloring Ball game using java
 Programmer:   Casper
 Type:   Game
 Technology:  Java
IDE: NetBeans

 Description:   this is a standalone application, where two colored balls which they are moving inside a frame, when they touch one of the 4 walls sides, their color change to another color, and so on, you can stop and play the game with two buttons

code samples :
java code
package javaapplication4;

import java.awt.*;

////////// BallModel
public class Ball {
//... Constants


final static int DIAMETER = 21;
public Image buffer; // The off-screen image for double-buffering
public Graphics bufferGraphics; // A Graphics object for the buffer


//... Instance variables
private int m_x; // x and y coordinates upper left
private int m_y;
private int typeColor;

private int m_velocityX; // Pixels to move each time move() is called.
private int m_velocityY;

private int m_rightBound; // Maximum permissible x, y values.
private int m_bottomBound;

//======================================================== constructor
public Ball(int x, int y, int velocityX, int velocityY) {
m_x = x;
m_y = y;
m_velocityX = velocityX;
m_velocityY = velocityY;

}

//======================================================== setBounds
public void setBounds(int width, int height) {
m_rightBound = width - DIAMETER;
m_bottomBound = height - DIAMETER;
}

//============================================================== move
public void move() {
//... Move the ball at the give velocity.
m_x += m_velocityX;
m_y += m_velocityY;

//... Bounce the ball off the walls if necessary.
if (m_x < 0) { // If at or beyond left side
typeColor=1;
m_x = 0; // Place against edge and
m_velocityX = -m_velocityX; // reverse direction.

} else if (m_x > m_rightBound) { // If at or beyond right side
m_x = m_rightBound; // Place against right edge.
typeColor=2;
m_velocityX = -m_velocityX; // Reverse direction.
}

if (m_y < 0) { // if we're at top
m_y = 0;
typeColor=3;
m_velocityY = -m_velocityY;

} else if (m_y > m_bottomBound) { // if we're at bottom
m_y = m_bottomBound;
typeColor=4;
m_velocityY = -m_velocityY;
}
}

//============================================================== draw
public void draw(Graphics g) {
if(typeColor==1){
g.setColor(Color.BLUE);
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
if(typeColor==2){
g.setColor(Color.GREEN);
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
if(typeColor==3){
g.setColor(Color.YELLOW);
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
if(typeColor==4){
g.setColor(Color.RED);
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}

}

//==== getDiameter, getX, getY
public int getDiameter() { return DIAMETER;}
public int getX() { return m_x;}
public int getY() { return m_y;}

//===-======= setPosition
public void setPosition(int x, int y) {
m_x = x;
m_y = y;
}
}


java code
/*
* BallInBox.java

*/

package javaapplication4;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

//////BouncingBall
public class BallInBox extends JPanel {
//============================================== fields
//... Instance variables representing the ball.
private Ball m_ball = new Ball(0, 0, 2, 3);
private Ball mm_ball = new Ball(200, 80,2,3);

//... Instance variables for the animiation
private int m_interval = 35; // Milliseconds between updates.
private Timer m_timer; // Timer fires to anmimate one step.

//======= constructor
/** Set panel size and creates timer. */
public BallInBox() {
setPreferredSize(new Dimension(200, 80));
setBorder(BorderFactory.createLineBorder(Color.BLACK));
m_timer = new Timer(m_interval, new TimerAction());
}

//========setAnimation
/** Turn animation on or off.
*@param turnOnOff Specifies state of animation.
*/
public void setAnimation(boolean turnOnOff) {
if (turnOnOff) {
m_timer.start(); // start animation by starting the timer.
} else {
m_timer.stop(); // stop timer
}
}


//===== paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background, border
m_ball.draw(g);
mm_ball.draw(g);// Draw the ball.
}

/////// inner listener class ActionListener
class TimerAction implements ActionListener {
//========== actionPerformed
/** ActionListener of the timer. Each time this is called,
* the ball's position is updated, creating the appearance of
* movement.
*@param e This ActionEvent parameter is unused.
*/
public void actionPerformed(ActionEvent e) {
m_ball.setBounds(getWidth(), getHeight());
m_ball.move(); // Move the ball.
mm_ball.setBounds(getWidth(),getHeight());
mm_ball.move();
repaint(); // Repaint indirectly calls paintComponent.
}
}
}//endclass





Attachments:
Bouncing Coloring Ball.rar [20.77 KiB]
Downloaded 1686 times

_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .
Author:
Expert
User avatar Posts: 838
Have thanks: 2 time

updated.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : Bouncing Coloring Ball Game(V1.0)
 bouncing ball in flash     -  
 ping pong game - java-Sticker-ball game (modified v1.1)     -  
 Applet Game To Shot a Random Ball     -  
 Ball Color Match 2D Java Game Version 1.0     -  
 ping pong java code-Sticker-ball game     -  
 fill all Squares of game board - 2d Java Game Example     -  
 Sticker Ball     -  
 Ball Sticker{Applet}     -  
 Ball sticker applet     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  



Topic Tags

Java Projects







Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com