import java.applet.Applet;
import java.awt.*;
public class test2 extends Applet
{
Image virtualMem;
Graphics gBuffer;
int oldX, oldY, newX, newY;
int appletWidth;
int appletHeight;
int boxWidth;
int boxHeight;
Rectangle box;
Rectangle color; //UI click box for Color selection)
Rectangle red;
Rectangle yellow;
Rectangle blue;
Rectangle green;
Rectangle black;
Rectangle pen; //UI click box for Pen tool
int numColor, numTool, xClick, yClick;
public void init()
{
setSize(1024,768);
repaint();
boxWidth = 700;
boxHeight = 500;
virtualMem = createImage(boxWidth,boxHeight);
gBuffer = virtualMem.getGraphics();
gBuffer.setColor(Color.white);
gBuffer.fillRect(0,0,boxWidth,boxHeight);
numColor = 1; //sets default color&tool
numTool = 1;
box = new Rectangle(0,0,700,500);
color = new Rectangle(5,5,55,280); //initiate UI click box for Colors
red = new Rectangle(10,10,45,45);
yellow = new Rectangle(10,60,45,45);
blue = new Rectangle(10,110,45,45);
green = new Rectangle(10,160,45,45);
black = new Rectangle(10,210,45,45);
pen = new Rectangle(10,290,45,60);
}
public void paint(Graphics g)
{
gBuffer.setColor(Color.black);
gBuffer.drawLine(oldX,oldY,newX,newY);
g.drawImage(virtualMem,0,0,this);
g.drawRect(0, 0, 700, 500);
g.drawRect(705, 5, 55, 280); //UI text Color
g.drawString("Color", 717, 275);
g.setColor(Color.red); //UI text Red, yellow, ...
g.fillRect(710, 10, 45, 45);
g.setColor(Color.yellow);
g.fillRect(710, 60, 45, 45);
g.setColor(Color.blue);
g.fillRect(710, 110, 45, 45);
g.setColor(Color.green);
g.fillRect(710, 160, 45, 45);
g.setColor(Color.black);
g.fillRect(710, 210, 45, 45);
g.setColor(Color.black);
g.drawRect(710,290,45,60);
switch (numColor) { //Action when clicking each Color
case 1:
g.setColor(Color.red);
break;
case 2:
g.setColor(Color.yellow);
break;
case 3:
g.setColor(Color.blue);
break;
case 4:
g.setColor(Color.green);
break;
case 5:
g.setColor(Color.black);
break;
}
switch (numTool) { // Action when selected Tool (Pen, erase...)
case 1:
g.fillRect(xClick, yClick, 2, 2);
break;
case 2:
g.fillRect(xClick, yClick, 2, 2);
break;
case 3:
g.fillRect(xClick, yClick, 2, 2);
break;
case 4:
g.fillRect(xClick, yClick, 2, 2);
break;
case 5:
g.fillRect(xClick, yClick, 2, 2);
break;
}
}
public boolean mouseDown(Event e, int x, int y)
{
if (color.inside(x, y)){ // If mouse inside color control area, initiate following selections
if (red.inside(x, y)) {
numColor = 1;
}
else if (yellow.inside(x, y)) {
numColor = 2;
}
else if (blue.inside(x, y)) {
numColor = 3;
}
else if (green.inside(x, y)) {
numColor = 4;
}
else if (black.inside(x, y)) {
numColor = 5;
}
}
else if (pen.inside(x, y)){
newX = x;
newY = y;
}
xClick = x;
yClick = y;
repaint();
return true;
}
public boolean mouseDrag(Event e, int x, int y)
{
newX = x;
newY = y;
oldX = newX;
oldY = newY;
repaint();
return true;
}
public void update(Graphics g)
{
paint(g);
}
}