command.java
/**
* @author aassd
*
* to change this generated comment edit the template variable "typecomment":
* window>preferences>java>templates.
* to enable and disable the creation of type comments go to
* window>preferences>java>code generation.
*/
public interface command {
public final static int line = 2;
public final static int circle = 4;
public final static int rectangle = 8;
}
icircle.java
import java.awt.*;
/**
* @author aassd
*
* to change this generated comment edit the template variable "typecomment":
* window>preferences>java>templates.
* to enable and disable the creation of type comments go to
* window>preferences>java>code generation.
*/
public class icircle implements shape {
private color color;
private int x;
private int y;
private int width;
private int height;
public icircle(int x, int y, int width, int height, color color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public void draw(graphics g) {
g.setcolor(color);
g.drawarc(x, y, width, height, 0, 360);
}
}
iline.java
import java.awt.*;
/**
* @author aassd
*
* to change this generated comment edit the template variable "typecomment":
* window>preferences>java>templates.
* to enable and disable the creation of type comments go to
* window>preferences>java>code generation.
*/
public class iline implements shape {
private int x1;
private int y1;
private int x2;
private int y2;
private color color;
public iline(int x1, int y1, int x2, int y2, color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
}
public void draw(graphics g) {
g.setcolor(color);
g.drawline(x1, y1, x2, y2);
}
}
irectangle.java
import java.awt.*;
import java.awt.event.*;
import java.util.vector;
/**
* @author aassd
*
* to change this generated comment edit the template variable "typecomment":
* window>preferences>java>templates.
* to enable and disable the creation of type comments go to
* window>preferences>java>code generation.
*/
public class irectangle implements shape {
private int x;
private int y;
private int width;
private int height;
private color color;
public irectangle(int x, int y, int width, int height, color color) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public void draw(graphics g) {
g.setcolor(color);
g.drawrect(x, y, width, height);
}
}
paintapp.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* @author aassd
*
* to change this generated comment edit the template variable "typecomment":
* window>preferences>java>templates.
* to enable and disable the creation of type comments go to
* window>preferences>java>code generation.
*/
public class paintapp extends frame {
panel commandpanel = new panel();
panel colorpanel = new panel();
panel shapepanel = new panel();
button btnred = new button("red");
button btngreen = new button("green");
button btnblue = new button("blue");
button btnline = new button("line");
button btnrectangle = new button("rectangle");
button btncircle = new button("circle");
button btnundo = new button("undo");
button btnredo = new button("redo");
button btnexit = new button("exit");
paintboard paintboard = new paintboard();
public paintapp() {
this.setlayout(new borderlayout());
btnline.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
btnline_actionperformed(e);
}
});
btnrectangle.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
btnrectangle_actionperformed(e);
}
});
btncircle.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
btncircle_actionperformed(e);
}
});
btnexit.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
btnexit_actionperformed(e);
}
});
btnundo.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
btnundo_actionperformed(e);
}
});
btnredo.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
btnredo_actionperformed(e);
}
});
shapepanel.setlayout(new flowlayout());
shapepanel.add(btnline, null);
shapepanel.add(btnrectangle, null);
shapepanel.add(btncircle, null);
shapepanel.add(btnundo, null);
shapepanel.add(btnredo, null);
shapepanel.add(btnexit,null);
btnred.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
btnred_actionperformed(e);
}
});
btngreen.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
btngreen_actionperformed(e);
}
});
btnblue.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
btnblue_actionperformed(e);
}
});
colorpanel.setlayout(new flowlayout());
colorpanel.add(btnred, null);
colorpanel.add(btngreen, null);
colorpanel.add(btnblue, null);
commandpanel.setlayout(new borderlayout());
commandpanel.add(shapepanel, borderlayout.north);
commandpanel.add(colorpanel, borderlayout.center);
this.add(commandpanel, borderlayout.south);
this.add(paintboard, borderlayout.center);
///////////////////////////////////////////////////////////////////////////
btnline.setforeground(color.red);
paintboard.setcommand(command.line);
btnred.setforeground(color.red);
paintboard.setcolor(color.red);
///////////////////////////////////////////////////////////////////////////
}
void btnexit_actionperformed(actionevent e) {
system.exit(0);
}
void btnundo_actionperformed(actionevent e) {
paintboard.undo();
}
void btnredo_actionperformed(actionevent e) {
paintboard.redo();
}
void btnline_actionperformed(actionevent e) {
btnline.setforeground(color.red);
btnrectangle.setforeground(color.black);
btncircle.setforeground(color.black);
paintboard.setcommand(command.line);
}
void btnrectangle_actionperformed(actionevent e) {
btnline.setforeground(color.black);
btnrectangle.setforeground(color.red);
btncircle.setforeground(color.black);
paintboard.setcommand(command.rectangle);
}
void btncircle_actionperformed(actionevent e) {
btnline.setforeground(color.black);
btnrectangle.setforeground(color.black);
btncircle.setforeground(color.red);
paintboard.setcommand(command.circle);
}
void btnred_actionperformed(actionevent e) {
btnred.setforeground(color.red);
btngreen.setforeground(color.black);
btnblue.setforeground(color.black);
paintboard.setcolor(color.red);
}
void btngreen_actionperformed(actionevent e) {
btnred.setforeground(color.black);
btngreen.setforeground(color.red);
btnblue.setforeground(color.black);
paintboard.setcolor(color.green);
}
void btnblue_actionperformed(actionevent e) {
btnred.setforeground(color.black);
btngreen.setforeground(color.black);
btnblue.setforeground(color.red);
paintboard.setcolor(color.blue);
}
public static void main(string[] args) {
paintapp paintapp = new paintapp();
paintapp.setsize(600, 500);
paintapp.setvisible(true);
}
}
paintboard.java
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* @author aassd
*
* to change this generated comment edit the template variable "typecomment":
* window>preferences>java>templates.
* to enable and disable the creation of type comments go to
* window>preferences>java>code generation.
*/
public class paintboard extends canvas implements java.awt.event.mouselistener, java.awt.event.mousemotionlistener {
int srcx = 0;
int srcy = 0;
int linetox = 0;
int linetoy = 0;
boolean bmousepressing = false;
java.util.stack vshapes = new java.util.stack();
java.util.stack vdelshapes = new java.util.stack();
private int command = 0;
private color color;
public void undo() {
if (vshapes.size() > 0) {
object shape = vshapes.pop();
vdelshapes.push(shape);
repaint();
}
}
public void redo() {
if (vdelshapes.size() > 0) {
object shape = vdelshapes.pop();
vshapes.push(shape);
repaint();
}
}
public void setcommand(int command) {
this.command = command;
}
public void setcolor(color color) {
this.color = color;
}
public paintboard() {
this.addmouselistener(this);
this.addmousemotionlistener(this);
}
public void paint(graphics g) {
dimension size = size();
int width = size.width ;
int height = size.height;
g.setcolor(color.white);
g.fillrect(0,0, width,height);
shape shape = null;
java.util.enumeration enum = vshapes.elements();
g.setcolor(color.blue);
while (enum.hasmoreelements()) {
shape = (shape) enum.nextelement();
shape.draw(g);
}
if (bmousepressing) {
g.setcolor(color);
switch (command) {
case command.line:
g.drawline(srcx, srcy, linetox, linetoy);
break;
case command.rectangle:
if (linetox < srcx) {
if (linetoy < srcy) {
g.drawrect(linetox, linetoy, srcx – linetox , srcy – linetoy);
}
else {
g.drawrect(linetox, srcy, srcx – linetox, linetoy – srcy);
}
}
else {
if (linetoy < srcy) {
g.drawrect(srcx, linetoy, linetox – srcx, srcy – linetoy);
}
else {
g.drawrect(srcx, srcy, linetox – srcx, linetoy – srcy);
}
}
break;
case command.circle:
int w = (int)java.lang.math.sqrt((srcx – linetox) * (srcx – linetox) + (srcy – linetoy) * (srcy – linetoy));
g.drawarc(srcx – w/2, srcy – w/2, w, w, 0, 360);
break;
}//end switch(command)
}
}
public void mouseclicked(mouseevent e) {
}
public void mousepressed(mouseevent e) {
srcx = e.getx();
srcy = e.gety();
bmousepressing = true;
}
public void mousereleased(mouseevent e) {
linetox = e.getx();
linetoy = e.gety();
bmousepressing = false;
switch (command) {
case command.line:
iline line = new iline(srcx, srcy, linetox, linetoy, color);
vshapes.push(line);
break;
case command.rectangle:
irectangle rectangle = null;
if (linetox < srcx) {
if (linetoy < srcy) {
rectangle = new irectangle(linetox, linetoy, srcx – linetox , srcy – linetoy, color);
}
else {
rectangle = new irectangle(linetox, srcy, srcx – linetox, linetoy – srcy, color);
}
}
else {
if (linetoy < srcy) {
rectangle = new irectangle(srcx, linetoy, linetox – srcx, srcy – linetoy, color);
}
else {
rectangle = new irectangle(srcx, srcy, linetox – srcx, linetoy – srcy, color);
}
}
vshapes.push(rectangle);
break;
case command.circle:
int w = (int)java.lang.math.sqrt((srcx – linetox) * (srcx – linetox) + (srcy – linetoy) * (srcy – linetoy));
icircle circle = new icircle(srcx – w/2, srcy – w/2, w, w, color);
vshapes.push(circle);
break;
} //end switch(command)
repaint();
}
public void mouseentered(mouseevent e) {
}
public void mouseexited(mouseevent e) {
}
public void mousedragged(mouseevent e) {
linetox = e.getx();
linetoy = e.gety();
repaint();
}
public void mousemoved(mouseevent e) {
}
}
shape.java
import java.awt.*;
import java.awt.event.*;
/**
* @author aassd
*
* to change this generated comment edit the template variable "typecomment":
* window>preferences>java>templates.
* to enable and disable the creation of type comments go to
* window>preferences>java>code generation.
*/
public interface shape {
public void draw(graphics g);
}