import java.awt.*;
/* Class Painter is the main class of this program ...
* This program will create a simple interface that will allow
* a user to paint on the provided canvas with several colors, using
* several geometric shapes and print the result
* As such, it is my first Java applet to be written independ-
* ently.
*
* Current Version: 1.1
*
* Painter class ===============
*/
public class Painter extends java.applet.Applet {
// variables ====================
private BorderLayout main_layout = new BorderLayout();
private GridLayout west_layout = new GridLayout(10, 1, 2, 2);
private FlowLayout north_layout = new FlowLayout(FlowLayout.LEFT);
private FlowLayout southern_layout = new FlowLayout(FlowLayout.LEFT);
private Font logo_font = new Font("Dialog", Font.BOLD, 20);
private Color button_color = new Color(00,80,00);
private Panel north = new Panel();
private Panel south = new Panel();
private Panel east = new Panel();
private Panel west = new Panel();
public MyCanvas center = new MyCanvas(this);
// left-hand-side components
Label choose_color = new Label("Pick color: ");
Choice choice_north_1 = new Choice();
String[] choice_1_options = {"color green","color
yellow","color red","color blue","color
cyan","color magenta","color pink","color
orange","color white"};
Color[] choice_1_colors =
{Color.green,Color.yellow,Color.red,Color.blue,Color.cyan,Color.magenta,Color.pink,Color.orange,
Color.white};
Label choose_shape = new Label("Choose shape: ");
Choice choice_of_shapes = new Choice();
String[] choice_of_shapes_list = {"free line","straight
line","rectangle","square","ellipse","circle","sun
rays","landscape"};
// define top components
Label program_name = new Label("Java Painter v1.1");
// declare clear button for top
Button erase = new Button("Clear");
// define south component
MyCanvas southern = new MyCanvas(this);
// ============
// init() method ================
public void init() { // initialize graphics and menus
setLayout(main_layout); // set main layout: BorderLayout
setBackground(Color.lightGray);
setForeground(Color.black);
// initialize left-hand-side components
west.setLayout(west_layout);
west.setBackground(Color.black);
choice_north_1.setBackground(Color.black);
choice_north_1.setForeground(Color.white);
choose_color.setForeground(Color.lightGray);
choose_shape.setForeground(Color.lightGray);
choice_of_shapes.setBackground(Color.black);
choice_of_shapes.setForeground(Color.white);
for (int
general_count=0;general_count<choice_1_options.length;general_count++) {
choice_north_1.addItem(choice_1_options[general_count]);
}
for (int
general_count=0;general_count<choice_of_shapes_list.length;general_count++) {
choice_of_shapes.addItem(choice_of_shapes_list[general_count]);
}
west.add(choose_color);
west.add(choice_north_1);
west.add(choose_shape);
west.add(choice_of_shapes);
// initialize top components
north.setBackground(Color.gray);
north.setForeground(Color.white);
north.setLayout(north_layout);
program_name.setFont(logo_font);
north.add(program_name);
erase.setForeground(button_color);
north.add(erase);
// initialize south component
south.setLayout(southern_layout);
south.setBackground(Color.blue);
south.add(southern);
// initialize east component
east.setLayout(new FlowLayout(FlowLayout.RIGHT));
east.setBackground(Color.blue);
// add all components to the screen
add("North", north);
add("South", south);
add("East", east);
add("West", west);
add("Center", center);
} // end init()
// ============
// action() method ==============
public boolean action(Event evt, Object arg) { // react to buttons
if (evt.target == erase) {
center.clearCanvas();
}
else {
return false;
}
return true;
}
// ============
} // end Painter class
// ====================
// ====================
// ====================
/* MyCanvas extends Canvas class.
* It also handles all painting done on the canvas component of the
* program.
*
*
*
* MyCanvas class ============
*/
class MyCanvas extends Canvas {
// declaring variables ==========
Painter applet; // reference-with-Painter-class variables
String color_selected, shape_selected; // selected color and shape
int last_x, last_y, first_x, first_y; // coordinates
int temp_x, temp_y; // temporary coordinates
Color canvas_color = Color.lightGray; // main
public int general_count = 0;
// ============
// MyCanvas class constructor ===
MyCanvas(Painter parent) { // MyCanvas constructor
applet = parent;
}
// ============
// determineColor method (returns Color object) ===
public Color determineColor() { // find out what color is selected
color_selected = applet.choice_north_1.getSelectedItem().toString();
for (general_count=0;general_count<applet.choice_1_colors.length;general_count++)
{
if (color_selected == applet.choice_1_options[general_count]) {
return applet.choice_1_colors[general_count];
}
}
return Color.black;
}
// determineShape method (returns String object) ==
public int determineShape() { // find out what shape is selected
shape_selected = applet.choice_of_shapes.getSelectedItem().toString();
for
(general_count=0;general_count<applet.choice_of_shapes_list.length;general_count++) {
if (shape_selected == applet.choice_of_shapes_list[general_count]) {
return general_count;
}
}
return 0;
}
public void paint(Graphics g) {
// nothing here for now
}
// mouseup, mousedown, mousedrag methods ==========
public boolean mouseDown(Event e, int x, int y) { // on mousedown...
last_x = x; last_y = y;
first_x = x; first_y = y;
return true;
}
public boolean mouseDrag(Event e, int x, int y) { // on mousedrag...
Graphics g = this.getGraphics();
g.setColor(determineColor());
switch(determineShape()) {
case 0:
g.drawLine(last_x,last_y,x,y);
last_x=x; last_y=y;
break;
case 6:
g.drawLine(first_x,first_y,x,y);
last_x=x; last_y=y;
break;
case 7:
g.drawLine(first_x,first_y,x,y);
g.drawLine(last_x,last_y,x,y);
last_x=x; last_y=y;
break;
default:
break;
}
return true;
}
public boolean mouseUp(Event e, int x, int y) { // on mouseup...
last_x=x; last_y=y;
Graphics g = this.getGraphics();
g.setColor(determineColor());
switch(determineShape()) {
case 1:
g.drawLine(first_x,first_y,x,y);
last_x=x; last_y=y;
break;
case 2:
g.drawRect(first_x,first_y,(x - first_x),(y - first_y));
last_x=x; last_y=y;
break;
case 3:
g.drawRect(first_x,first_y,(x - first_x),(x - first_x));
last_x=x; last_y=y;
break;
case 4:
g.drawOval(first_x,first_y,(x - first_x),(y - first_y));
last_x=x; last_y=y;
break;
case 5:
g.drawOval(first_x,first_y,(x - first_x),(x - first_x));
last_x=x; last_y=y;
break;
default:
break;
}
return true;
}
public void clearCanvas() {
Graphics g = this.getGraphics();
g.setColor(this.getBackground());
g.fillRect(0,0,size().width,size().height);
}
// ============
} // end MyCanvas class
|