Applets and Objects
Draw OneDie (Program): Ass2s1p1.java 
|
Write a program that draws a die on the screen, with the value based on a random number, using the specifications in the diagram:
In this program, the grid is composed of squares of dimensions 10x10, for a total of 100x100 pixels per die. The upper left corner be at (100,100).
Write the method void dot (Graphics gr, int x, int y) where dot (g, 1,1) would correctly draw the upper left circle, and dot(g,4,4) the centre circle.
|
// The "Ass2s1p1" class.
// Select Applet width 300, height 200
import java.applet.*;
import java.awt.*;
public class Ass2s1p1 extends Applet
{
int top = 50, left = 100;
public void init ()
{
} // init method
public void paint (Graphics g)
{
int r = (int) (Math.random () * 6 + 1);
die (g, r);
} // paint method
public void dot (Graphics gr, int x, int y)
{
gr.setColor (Color.white);
gr.fillOval (left + 10 * x, top + 10 * y, 20, 20);
gr.setColor (Color.black);
gr.drawOval (left + 10 * x, top + 10 * y, 20, 20);
} //end dot
public void die (Graphics g, int num)
{
g.setColor (Color.red);
g.fillRect (left, top, 100, 100);
g.setColor (Color.black);
g.drawRect (left, top, 100, 100);
if (num % 2 == 1)
dot (g, 4, 4);
if (num > 1)
{
dot (g, 1, 1);
dot (g, 7, 7);
}
if (num > 3)
{
dot (g, 7, 1);
dot (g, 1, 7);
}
if (num == 6)
{
dot (g, 1, 4);
dot (g, 7, 4);
}
} //end die
} // Ass2s1p1 class
|
The calling HTML file is automatically generated by Ready®:
<html>
<head>
<title>
The Ass2s1p1 applet
</title>
</head>
<body>
<h2>
Here is the Ass2s1p1 applet
</h2>
<hr>
<applet code="Ass2s1p1.class" width="300" height="200">
</applet>
<hr>
</body>
</html>
|
Draw One Die (Method): Ass2s1p2.java 
|
Generalize the above problem by writing a method
void die(Graphics gr, int value, int x, int y, int size, int colour).
It would draw a die with upper left corner at (x,y) and where size represents the size in pixels of the length of one square of the grid.
|
// The "Ass2s1p2" class.
// Select Applet width 300, height 200
import java.applet.*;
import java.awt.*;
public class Ass2s1p2 extends Applet
{
public void init ()
{
} // init method
public void paint (Graphics g)
{
int r = (int) (Math.random () * 6 + 1);
int s = (int) (Math.random () * 6 + 1);
int t = (int) (Math.random () * 6 + 1);
die (g, r, 30, 40, 5, Color.blue);
die (g, s, 160, 5, 12, Color.yellow);
die (g, t, 80, 100, 7, Color.red);
} // paint method
public void dot (Graphics gr, int x, int y, int a, int b, int size)
{
gr.setColor (Color.white);
gr.fillOval (x + size * a, y + size * b, 2 * size, 2 * size);
gr.setColor (Color.black);
gr.drawOval (x + size * a, y + size * b, 2 * size, 2 * size);
} //end dot
public void die (Graphics g, int value, int x, int y, int size, Color colour)
{
g.setColor (colour);
g.fillRect (x, y, 10 * size, 10 * size);
g.setColor (Color.black);
g.drawRect (x, y, 10 * size, 10 * size);
if (value % 2 == 1)
dot (g, x, y, 4, 4, size);
if (value > 1)
{
dot (g, x, y, 1, 1, size);
dot (g, x, y, 7, 7, size);
}
if (value > 3)
{
dot (g, x, y, 7, 1, size);
dot (g, x, y, 1, 7, size);
}
if (value == 6)
{
dot (g, x, y, 1, 4, size);
dot (g, x, y, 7, 4, size);
}
} //end die
} // Ass2s1p2 class
|
Draw One Die (Object): Ass2s1p7.java 
|
Create the class Die with constructor Die()
Create instance variables for the upper left corner if the die, its size, value and colour, with suitable default values.
Create the methods void setColour(Color c), void setSize(int s), void setValue(int v), setPosition(int x, int, y) and draw(Graphics gr);
Write a program to thoroughly test the class.
The Die Class:
|
// The "Die" class.
import java.awt.*;
public class Die
{
protected int posX, posY, size;
protected Color colour;
protected int value;
public Die ()
{
posX = 0;
posY = 0;
size = 10;
value = 1;
colour = Color.red;
} // end constructor
void setColour (Color c)
{
colour = c;
} // end setColour
void setSize (int s)
{
size = s;
} // end setSize
void setValue (int v)
{
value = v;
} // end setValue
void setPosition (int x, int y)
{
posX = x;
posY = y;
} // end setPosition
public void dot (Graphics gr, int x, int y, int a, int b, int size)
{
gr.setColor (Color.white);
gr.fillOval (x + size * a, y + size * b, 2 * size, 2 * size);
gr.setColor (Color.black);
gr.drawOval (x + size * a, y + size * b, 2 * size, 2 * size);
} //end dot
void draw (Graphics g)
{
g.setColor (colour);
g.fillRect (posX, posY, 10 * size, 10 * size);
g.setColor (Color.black);
g.drawRect (posX, posY, 10 * size, 10 * size);
if (value % 2 == 1)
dot (g, posX, posY, 4, 4, size);
if (value > 1)
{
dot (g, posX, posY, 1, 1, size);
dot (g, posX, posY, 7, 7, size);
}
if (value > 3)
{
dot (g, posX, posY, 7, 1, size);
dot (g, posX, posY, 1, 7, size);
}
if (value == 6)
{
dot (g, posX, posY, 1, 4, size);
dot (g, posX, posY, 7, 4, size);
}
} //end draw
} // Die class
|
The Calling Program:
// The "Ass2s1p3" class.
import java.applet.*;
import java.awt.*;
public class Ass2s1p3 extends Applet
{
Die die1, die2;
public void init ()
{
die1 = new Die ();
die2 = new Die ();
die1.setColour (Color.blue);
die1.setSize (7);
die1.setPosition (50, 100);
die2.setColour (Color.red);
die2.setSize (6);
die2.setPosition (150, 105);
} // init method
public void paint (Graphics g)
{
die1.setValue (rand (6));
die2.setValue (rand (6));
die2.draw (g);
die1.draw (g);
} // paint method
public int rand (int max)
{
return (int)( Math.random () * max )+ 1;
} //end rand
} // Ass2s1p3 class
|
Roll two dice: A simple game: Ass2s2p4.java 
|
Write an applet, which lets you roll two dice, repeatedly until you roll a double, and you win, or a sum of 7 and you lose. Use the Die class and one button to cast the two dice.
|
// The "Ass2s1p4" class.
import java.applet.*;
import java.awt.*;
//best viewed with Applet width=300 and height=200
public class Ass2s1p4 extends Applet
{
int value;
Font f;
int a, b;
Die die1 = new Die ();
Die die2 = new Die ();
Button start = new Button ("roll");
public void init ()
{
setBackground (Color.white);
add (start);
die1.setSize (7);
die1.setPosition (50, 50);
die1.setColour (Color.yellow);
die2.setSize (7);
die2.setPosition (150, 50);
die2.setColour (Color.red);
} // end init method
public boolean action (Event evt, Object arg)
{
if (evt.target instanceof Button)
{
a = randDie ();
b = randDie ();
die1.setValue (a);
die2.setValue (b);
}
repaint ();
return true;
}
public void paint (Graphics g)
{
f = new Font ("Times New Roman", Font.BOLD, 12);
g.setFont (f);
g.setColor (Color.blue);
if (a + b == 7)
g.drawString ("Sorry, you lost: You rolled a " + (a + b), 70, 180);
else if (a == b)
g.drawString ("Congratulations: You rolled two " + a + "\'s!!", 70, 180);
else
g.drawString ("Roll again: You rolled a " + (a + b), 70, 180);
die1.draw (g);
die2.draw (g);
} // end paint method
public int randDie ()
{
return (int) (Math.random () * 6 + 1);
}
} // Ass2s1p4 class
|
ThreeDeeDie: Ass2s2p8.java 
|
Create an extension of the Die class called ThreeDeeDie.
(If the variables of the Die class were classified as "private", change them to "protected")
Add a parallelogram as shown to either the left or the right in the draw method and two corresponding methods: drawLeft() and drawRight()
Then modify program 2.1.4 to test the class.
|
The ThreeDeeDie Class
// The "ThreeDeeDie" class.
import java.awt.*;
public class ThreeDeeDie extends Die
{
int sideX [] = {0, 2, 2, 0, 0};
int sideY [] = {0, 1, 9, 10, 0};
int xPt [] = new int [5];
int yPt [] = new int [5];
public ThreeDeeDie ()
{
super ();
}
public ThreeDeeDie (int x, int y, int s)
{
setPosition (x, y);
setSize(s);
}
public void drawLeft (Graphics g)
{
for (int i = 0 ; i < 5 ; i++)
{
xPt [i] = sideX [i] * size + 10 * size + posX;
yPt [i] = sideY [i] * size + posY;
}
g.setColor (colour);
g.fillPolygon (xPt, yPt, 5);
g.setColor (Color.black);
g.drawPolygon (xPt, yPt, 5);
draw (g);
}
public void drawRight (Graphics g)
{
for (int i = 0 ; i < 5 ; i++)
{
|
The Calling Program
// The "Ass2s1p6" class.
import java.applet.*;
import java.awt.*;
public class Ass2s1p6 extends Applet
{
int value;
Font f;
int a = 1, b = 2;
ThreeDeeDie die1 = new ThreeDeeDie (40, 50, 7);
ThreeDeeDie die2 = new ThreeDeeDie (150, 50, 7);
Button start = new Button ("roll");
public void init ()
{
setBackground (Color.white);
add (start);
die1.setColour (Color.yellow);
die2.setColour (Color.red);
die1.setValue (a);
die2.setValue (b);
} // end init method
public boolean action (Event evt, Object arg)
{
if (evt.target instanceof Button)
{
a = randDie ();
b = randDie ();
die1.setValue (a);
die2.setValue (b);
}
repaint ();
return true;
}
public void paint (Graphics g)
{
f = new Font ("Times New Roman", Font.BOLD, 12);
g.setFont (f);
g.setColor (Color.blue);
if (a + b == 7)
g.drawString ("Sorry, you lost: You rolled a " + (a + b), 70, 180);
else if (a == b)
g.drawString ("Congratulations: You rolled two " + a + "\'s!!", 70, 180);
else
g.drawString ("Roll again: You rolled a " + (a + b), 70, 180);
die1.drawLeft (g);
die2.drawRight (g);
} // end paint method
public int randDie ()
{
return (int) (Math.random () * 6 + 1);
}
} // Ass2s1p6 class
|
Bouncing Ball: Ass2s2p8.java 
|
Create the Ball class with constructor Ball( int maxX, int maxY), where the two variables denote the size of the applet, and with methods void move( Graphics g), and various other methods to set the size, colour, position direction and speed of the ball. Each time the move method is invoked, the position variables (x,y) should be updated and the ball painted in its new position.
|
The Ball Class
// Ball class
import java.awt.*;
public class Ball
{
protected Color colour = Color.red;
protected int x = 100, y = 100;
protected int radius = 2;
protected int deltaX = 1, deltaY = 1;
protected int maxX = 300, maxY = 300;
public Ball (int maxX, int maxY)
{
this.maxX = maxX;
this.maxY = maxY;
} //end Ball constructor
public void setSize (int r)
{
radius = r;
}
public void setSpeed (int speed, int direction)
{
deltaX = (int) (-speed * Math.cos (direction * Math.PI / 180));
deltaY = (int) (speed * Math.sin (direction * Math.PI / 180));
}
public void setColor (Color c)
{
colour = c;
} //end setColor
public void setLocation (int x, int y)
{
this.x = x;
this.y = y;
} //end setLocation
public void move (Graphics gr)
{
x += deltaX;
y += deltaY;
if (x < radius * 4 || x > maxX - radius * 4)
deltaX = -deltaX;
if (y < radius * 4 || y > maxY - radius * 4)
deltaY = -deltaY;
gr.setColor (colour);
gr.fillOval (x - 4 * radius, y - 4 * radius, radius * 8, radius * 8);
gr.setColor (Color.black);
gr.drawOval (x - 4 * radius, y - 4 * radius, radius * 8, radius * 8);
} // end move
} //end ball
|
The Calling Program
// Ass2s1p8 class
// make sure the width of the applet is 300x300
import java.applet.*;
import java.awt.*;
public class Ass2s1p8 extends Applet implements Runnable
{
Thread run1;
Ball ball1 = new Ball (300, 300);
Ball ball2 = new Ball (300, 300);
public void init ()
{
ball1.setColor (Color.blue);
// distance 7 pixels further, 20 deg standard angle
ball1.setSpeed (7, 20);
ball1.setLocation (10, 100);
ball1.setSize (2);
ball2.setColor (Color.red);
ball2.setSpeed (5, 110);
ball2.setLocation (100, 200);
ball2.setSize (3);
} //end init
public void start ()
{
if (run1 == null)
{
run1 = new Thread (this);
run1.start ();
}
} //end start
public void stop ()
{
if (run1 != null)
{
run1.stop ();
run1 = null;
}
} //end stop
public void run ()
{
while (true)
{
repaint ();
try
{
Thread.sleep (10);
}
catch (InterruptedException e)
{
}
}
} //end run
public void paint (Graphics g)
{
ball1.move (g);
ball2.move (g);
} // end paint
} //end Ass2s1p8
|
|
Sponsored by ECOO and SIG-Computer Science
.
|
|
|