- /**
* Class for creating a graphic display
*
* @author T. K. Rogers
* @version 1-22-05
*/
import java.awt.* ;
import javax.swing.* ;
import java.util.* ;
- class GraphicsDisplay
extends JPanel {
private static int
y_velocity , // Dot's y
component of velocity
x_velocity , // Dot's x
component of velocity
x_coordinate , // Dot's x coordinate
y_coordinate , // Dot's y coordinate
y_velocity1 ,
x_velocity1 ,
x_coordinate1 ,
y_coordinate1 ;
-
public static boolean stop =
true ;
// A value of true stops the dot's motion
private static long
delay = 3000000 ; // Slows down the dot's motion
-
//Constructer///////////////////////////////////////////////////////////////
public GraphicsDisplay ( ) {
y_velocity = 2 ;
x_velocity = 3 ;
x_coordinate = 80 ;
y_coordinate = 30 ;
y_velocity1 = 5 ;
x_velocity1 = 6 ;
x_coordinate1 = 120 ;
y_coordinate1 = 120 ;
}
-
//////////////////////////////////////////////////////////////////////////////
public void paint ( Graphics g2
) {
int
a ; // Loop counter
-
// Sets and fills background color in applet, Color (
red, green, blue ).
// Values range from 0 to 255
g2.setColor(new Color ( 240,
240, 230 ) ) ;
g2.fillRect (0, 0, getSize (
).width, getSize ( ).height ) ;
-
// Draws a retangular boundary
g2.setColor (
new Color ( 0, 0, 0 ) ) ;
g2.drawRect ( 30, 30, getSize (
).width-60, getSize ( ).height-60 ) ;
-
// Sets color for the dot and draws it.
g2.setColor(new Color ( 240, 0,
0 ) ) ;
g2.fillOval ( x_coordinate1,
y_coordinate, 6, 6 ) ;
-
g2.setColor ( new Color ( 1,5,155 ) ) ;
g2.fillOval ( x_coordinate1 +
1, y_coordinate1 - 1,6, 6 ) ;
- if (
GuiMenu.option.isSelected ( ) ) {
setDelay ( "slow" ) ;
} else {
GraphicsDisplay.setDelay ( "fast" ) ;
}
-
// code for making the dot bounce back and forth.
if ( !stop ) {
for ( a=0;
a<delay; a++ ) ; //This code slows down the loop
moveDot ( ) ;
}
-
// Sets up Legend.
g2.setColor (
new Color ( 0, 0, 0 ) ) ;
g2.drawString (
"Bouncing Dot", getSize ( ).width/2-36, 25 ) ;
- repaint (
) ;
}
-
///////////////////////////////////////////////////////////////////////////
/**
* Moves the dot.
*
* @param y none
* @return none
*/
- // The following moveDot
( ) code works but is inefficient. Why?
public void moveDot ( ) {
// Code for
advancing the dot in each time increment.
x_coordinate += x_velocity ;
y_coordinate += y_velocity ;
x_coordinate1 += x_velocity1 ;
y_coordinate1 += y_velocity1 ;
-
// Code for making the dot bounce when it hits the
wall. The dot
-
// has an elastic collision, hence kinetic energy is conserved.
-
if ( x_coordinate >= ( getSize ( ).width-30 ) ) {
-
x_coordinate -= x_coordinate - ( getSize ( ).width - 30 ) ;
-
x_velocity *= -1; // When hitting a vertical wall the
x-velocity reverses
- }
-
-
if ( x_coordinate <= 30 ) {
-
x_coordinate -= x_coordinate - 30 ;
-
x_velocity *= -1;
- }
-
-
if ( x_coordinate1 >= ( getSize ( ).width - 30 ) ) {
-
x_coordinate1 -= x_coordinate1 - ( getSize ( ).width - 30 ) ;
-
x_velocity1 *= -1;
- }
-
-
if ( x_coordinate1 <= 30 ) {
-
x_coordinate1 -= x_coordinate1 - 30 ;
-
x_velocity1 *= -1;
- }
-
-
if ( y_coordinate >= ( getSize ( ).height - 30 ) ) {
-
y_coordinate -= y_coordinate - ( getSize ( ).height - 30 ) ;
-
y_velocity *= -1;
- }
-
-
if ( y_coordinate <= 30 ) {
-
y_coordinate -= y_coordinate - 30 ;
-
y_velocity *= -1 ;
- }
-
-
if ( y_coordinate1 >= ( getSize ( ).height - 30 ) ) {
-
y_coordinate1 -= y_coordinate1 - (getSize().height-30);
-
y_velocity1 *= -1 ;
- }
-
-
if ( y_coordinate1 <= 30 ) {
-
y_coordinate1 -= y_coordinate1 - 30 ;
-
y_velocity1 *= -1;
- }
- }
-
////////////////////////////////////////////////////////////////////////
public static void stopDot ( ) {
// Stops the
dot
stop = true ;
}
-
////////////////////////////////////////////////////////////////////////
public static void startDot ( )
{
// Starts the
dot
stop = false ;
}
////////////////////////////////////////////////////////////////////////
public static void resetDot ( )
{
// Resets the
dot
stopDot ( ) ;
y_velocity = 2 ;
x_velocity = 3 ;
x_coordinate = 80 ;
y_coordinate = 30 ;
y_velocity1 = 5 ;
x_velocity1 = 6 ;
x_coordinate1 = 90 ;
y_coordinate1 = 50 ;
}
////////////////////////////////////////////////////////////////////////
public static void setDelay (
String setting ) {
if ( setting ==
"slow" ) {
delay =
90000000 ;
} else {
delay = 3000000 ;
}
}
}