//Keith Adams, September 20, 2000
//This program is an applet that will display my
//first and last name, draw three colored concentric
//rings, and display the scoring values for each ring.
//It will also draw a 5x5 checkerboard
//You can also adjust the position and size of the targets
//and throw darts at the target and see what your score is.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Decisions extends Applet implements ActionListener
{
Button target_choice1=new Button("Target");//declare target button
Button target_choice2=new Button("Checkerboard");//checkerboard button
Button clear=new Button("Clear");
Button throw_dart=new Button("Throw Dart");
boolean clearcheck;
boolean circletarget;
boolean squaretarget;
boolean dart;
int y_center;
int x_center;
int circle_radius;
private TextField target_centerx = new TextField (10);
private TextField target_centery = new TextField (10);
private TextField target_size = new TextField (10);
int size;
public void init()
{
y_center=400;
x_center=400;
size=200;
clearcheck=false;
circletarget=false;
squaretarget=false;
add(target_choice1);
add(target_choice2);
add(clear);
add(throw_dart);
target_choice1.addActionListener(this);
target_choice2.addActionListener(this);
clear.addActionListener(this);
throw_dart.addActionListener(this);
add(new Label("X:"));
add (target_centerx);
target_centerx.addActionListener(this);
add(new Label("Y:"));
add (target_centery);
target_centery.addActionListener(this);
target_centerx.setText (""+x_center);
target_centery.setText (""+y_center);
add(new Label("Size:"));
add (target_size);
target_size.addActionListener(this);
target_size.setText(""+size);
}
public void paint (Graphics window)
{
//write my name, draw target with scores, draw checkerboard
window.drawString("Keith Adams",200,40);
if (!clearcheck)
{
if (squaretarget)
draw_board(window,x_center/2,y_center/4,size/2);
if (circletarget)
draw_target(window);
if(dart)
draw_dart(window);
}
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == clear)
clearcheck=true;
if (event.getSource() == target_choice1)
{
clearcheck=false;
circletarget=true;
squaretarget=false;
dart=false;
}
if (event.getSource() == target_choice2)
{
clearcheck=false;
squaretarget=true;
circletarget=false;
dart=false;
}
if (event.getSource() == throw_dart)
{ clearcheck=false;
dart=true;
}
x_center=Integer.parseInt(target_centerx.getText());
y_center=Integer.parseInt(target_centery.getText());
size=Integer.parseInt(target_size.getText());
circle_radius=size/2;
repaint();
}
private void draw_dart(Graphics window)
{
int x_dart, y_dart;
x_dart=(int)(Math.random()*2*circle_radius*3+x_center-circle_radius*3)+1;
y_dart=(int)(Math.random()*2*circle_radius*3+y_center-circle_radius*3)+1;
window.setColor(Color.white);
window.drawString("X", x_dart, y_dart);
int score;
if ((x_dart<=circle_radius/3)&&(y_dart<=circle_radius/3))
{score=200;
window.drawString(score+" points. Wow! You must be a P.E. Major.",10,400);
}
else if (((x_dart>circle_radius/3)||(x_dart<=circle_radius*2/3))&&((y_dart>circle_radius/3)||(y_dart<=circle_radius*2/3)))
{ score=100;
window.drawString(score+" points. Good Job.",10,400);
}
else if (((x_dart>circle_radius*2/3)||(x_dart<=circle_radius))&&((y_dart>circle_radius*2/3)||(y_dart<=circle_radius)))
{score=50;
window.drawString(score+" points. Not Bad.",10,400);
}
else
{score=0;
window.drawString(score+ " points. You throw like a computer programmer.",10,400);
}
}
private void draw_target (Graphics window)
{
//draw a target with three circles
draw_circles(window, x_center, y_center, circle_radius*3,Color.blue,50);
draw_circles(window, x_center, y_center, circle_radius*2,Color.green,100);
draw_circles(window, x_center,y_center,circle_radius,Color.red,200);
}
private void draw_circles (Graphics window, int centerX,
int centerY,int radius,
Color circle_color,int score)
{
//draw a circle with a point value
window.setColor(circle_color);
window.fillOval(centerX-radius,centerY-radius,2*radius,2*radius);
window.setColor(Color.white);
window.drawString(score+" points",centerX+radius-80,centerY+10);
}
//add 5x5 checker board
private void draw_square (Graphics window, Color square_color,
int left, int top, int size)
{
//set color and draw square
window.setColor (square_color);
window.fillRect(left,top,size,size);
}
private void draw_row1 (Graphics window,int topX,int topY,int size)
{
//draw a row of five squares
draw_square(window,Color.black,topX,topY,size);
draw_square(window,Color.red,topX+size,topY,size);
draw_square(window,Color.black,topX+2*size,topY,size);
draw_square(window,Color.red, topX+3*size,topY,size);
draw_square(window,Color.black,topX+4*size,topY,size);
}
private void draw_row2 (Graphics window, int topX,int topY,int size)
{
//draw a row of five squares (of opposite color alternation)
draw_square(window,Color.red,topX,topY,size);
draw_square(window,Color.black,topX+size,topY,size);
draw_square(window,Color.red,topX+2*size,topY,size);
draw_square(window,Color.black, topX+3*size,topY,size);
draw_square(window,Color.red,topX+4*size,topY,size);
}
private void draw_board (Graphics window,int startX,int startY,int length)
{
//draw 5 rows (alternating between the two color alternations)
draw_row1(window,startX,startY,length);
draw_row2(window,startX,startY+length,length);
draw_row1(window,startX,startY+2*length,length);
draw_row2(window,startX,startY+3*length,length);
draw_row1(window,startX,startY+4*length,length);
}
}