Professor Kit Romano's Web Site

 



Site Index

About Me

Welcome
Definition of Program
Variable Types
Understanding Memory
Declaring Variables
Simple Commands
Concatenation and Strings
Sleep-Threads-Try Catch
Campus Scenes
Pictures
Java simplified
Application Shell Styles
Reading Data From File
Overview of Applets
Excellent Graphics Applet
Online Reference
Nested if statements
Arrays
Sorting Arrays
Using Strings
Functions
Top 20 Replies 
First program 211
Important Class Example
JAVA in SDK Environment
Understanding OOP
OOP simple example
Recursion example
Example of Inheritance
     and use of JTextArea



*** Web Related **********
Build Student Web Page ?
What is FTP
Download WS ftp95
Download Java Plug In
Applets from jbuilder to sdk
Practice Problems

 

Programming Examples

 

 

                                                

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

public class ball_bounce extends JApplet
{
private ball b1=new ball(150,250,2,4,50,Color.BLUE);
private ball b2=new ball(350,250,-3,2,50,Color.RED);
private ball b3=new ball(150,350,4,6,50,Color.MAGENTA);
private ball b4=new ball(350,350,-5,3,50,Color.YELLOW); 
private ball b5=new ball(350,350,-5,3,100,Color.GREEN);
private Image a;

public ball_bounce() { }

public void init()
{
setBackground(Color.cyan);
}
public void start() { } 

public void paint(Graphics g)
{
Dimension d = getSize();
checkOffscreenImage();
Graphics offG = a.getGraphics();
offG.setColor(getBackground()); //clear screen
offG.fillRect(0, 0, 500, 500); //clear screen
// Draw into the offscreen image.
paintOffscreen(a.getGraphics());
// Put the offscreen image on the screen.
g.drawImage(a, 0, 0, null);

try{Thread.sleep(10);}
catch(Exception e){}
repaint();
}
private void checkOffscreenImage() 
{
Dimension d = getSize();
if (a == null ||
a.getWidth(null) != d.width ||
a.getHeight(null) != d.height) {
a = createImage(d.width, d.height);
}
}
public void paintOffscreen(Graphics g) {
ball[] B={b1,b2,b3,b4};
for(int a=0;a<4;a++)
B[a].moveBall(g);
b5.moveBall(g);
for(int a=0;a<4;a++) 
for(int b=0;b<4;b++)
{
if(b!=a)
B[a].ballCollision(B[b]);
}
for(int a=0;a<4;a++) {
int b=b5.x_speed;
b5.ballCollision(B[a]);
if(b!=b5.x_speed) b5.ball_size-=5;
if(0>=b5.ball_size) b5.ball_size=100;
}
}
}

*************************************************

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

public class ball extends JApplet
{
public int x_loc,y_loc,x_speed,y_speed,ball_size;
private Color ball_color;
public ball(int x, int y, int xs, 
int ys, int s, Color c)
{
x_loc=x;
y_loc=y;
x_speed=xs;
y_speed=ys;
ball_size=s;
ball_color=c;
}
private void drawBall(Graphics g)
{
g.setColor(ball_color);
g.fillOval(x_loc,y_loc,ball_size,ball_size);
}
public void moveBall(Graphics g)
{
x_loc+=x_speed;
y_loc+=y_speed;
drawBall(g);
wallCollision();
}
private void wallCollision()
{
if(x_loc<=0 || x_loc>=500-ball_size) x_speed*=-1;
if(y_loc<=0 || y_loc>=500-ball_size) y_speed*=-1;
}
public void ballCollision(ball q)
{
int xd=(this.x_loc+this.ball_size)-(q.x_loc+q.ball_size);
int yd=(this.y_loc+this.ball_size)-(q.y_loc+q.ball_size);
double xs=xd*xd;
double ys=yd*yd;
int h=(int)(Math.sqrt(xs+ys)); 
if(h<=this.ball_size/2+q.ball_size/2) 
{
x_speed*=-1;
y_speed*=-1;
}
}
}