Making and Moving Objects
import java.awt.*;
class Dot
{
int left, top;
Color color;
String labl;
Font fnt;
Dot(String lbl, int l, int t, Color c, Font f)
{
labl = lbl;
left = l;
top = t;
color = c;
fnt = f;
}
public void setWidth(int w) {} // Huh??
public int widthOf() { return 0; } // What's this?
public void setHeight(int h) {} // What, again??
public int heightOf() { return 0; } // I give up!!
public void setLeft(int l) { left = l; }
public int leftOf() { return left; }
public void setTop(int t) { top = t; }
public int topOf() { return top; }
public void setColor(Color c) { color = c; }
public Color colorOf() { return color; }
public void setLabel(String s) { labl = s; }
public String labelOf() { return labl; }
public void setFont(Font f) { fnt = f; }
public Font getFont() { return fnt; }
public void draw (Graphics q) {}
public boolean find (int x, int y) { return false; }
}
class RectDot extends Dot
{
int width, height;
RectDot (String s, int l, int t, int w, int h, Color c, Font f)
{
super(s,l,t,c,f);
width = w;
height = h;
}
public void setWidth(int w) { width = w; }
public int widthOf() { return width; }
public void setHeight(int h) { height = h; }
public int heightOf() { return height; }
public boolean find(int x, int y)
{
if (super.leftOf() < x && x < super.leftOf()+width &&
super.topOf() < y && y < super.topOf()+height)
return true;
return false;
}
public void draw(Graphics g)
{
g.setFont(super.getFont());
g.setColor(colorOf());
g.fillRect(leftOf(), topOf()-25, width, height);
g.setColor(Color.black);
g.drawString(labl, leftOf()+width/2, topOf()-25+height/2);
}
}
class OvalDot extends Dot
{
int width, height;
OvalDot (String s, int l, int t, int w, int h, Color c, Font f)
{
super(s,l,t,c,f);
width = w;
height = h;
}
public void setWidth(int w) { width = w; }
public int widthOf() { return width; }
public void setHeight(int h) { height = h; }
public int heightOf() { return height; }
public boolean find(int x, int y)
{
if (super.leftOf() < x && x < super.leftOf()+width &&
super.topOf() < y && y < super.topOf()+height)
return true;
return false;
}
public void draw(Graphics g)
{
g.setFont(super.getFont());
g.setColor(colorOf());
g.fillOval(leftOf(), topOf()-25, width, height);
g.setColor(Color.black);
g.drawString(labl, leftOf()+width/2, topOf()-25+height/2);
}
}
class CircDot extends Dot
{
int diam;
CircDot (String s, int l, int t, int d, Color c, Font f)
{
super(s,l,t,c,f);
diam = d;
}
public void setWidth(int w) { diam = w; }
public int widthOf() { return diam; }
public void setHeight(int h) { diam = h; }
public int heightOf() { return diam; }
public void setDiam(int d) { diam = d; }
public int diameterOf() { return diam; }
public boolean find (int x, int y)
{
if (super.leftOf() < x && x < super.leftOf()+diam &&
super.topOf() < y && y < super.topOf()+diam)
return true;
return false;
}
public void draw(Graphics g)
{
g.setFont(super.getFont());
g.setColor(colorOf());
g.fillOval(leftOf(), topOf()-25, diam, diam);
g.setColor(new Color(0,0,0));
g.drawString(labl, leftOf()+(diam/2), topOf()-25+(diam/2));
}
}
public class Objects extends Frame
{
String fontstylechoice[] = { "Helvetica", "Times Roman", "Courier",
"Dialog", "DialogInput", "Zapf Dingbats" };
String fontstyleselect[] = { "Helvetica", "TimesRoman", "Courier",
"Dialog", "DialogInput", "ZapfDingbats" };
int nfontstyles = 6;
String fontsizechoice[] = { "6", "8", "10", "12", "14", "16", "18" };
int nfontsizes = 7;
Button redbutton, bluebutton, greenbutton, rectbutton, textbutton,
circbutton, ovalbutton, objectbutton, bigbutton, smallbutton,
fontbutton;
TextField namefield;
Panel mainpanel;
Dot dot[] = new Dot[100];
int ndots = 0, current = -1;
Color dotcolor = Color.red;
Dot pick = null;
int fontstyle = 0;
int fontsz = 0;
Font myfont = new Font(fontstyleselect[fontstyle], Font.PLAIN,
Integer.parseInt(fontsizechoice[fontsz]));
int diffx, diffy;
Choice fonttype, fontsize;
public static void main(String arg[])
{
Frame f = new Objects();
f.resize(550,440);
f.show();
}
public Objects()
{
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new GridLayout(18,1));
p.add(new Label(""));
p.add(redbutton = new Button("Red"));
p.add(bluebutton = new Button("Blue"));
p.add(greenbutton = new Button("Green"));
p.add(new Label(""));
p.add(rectbutton = new Button("Rectangle"));
p.add(circbutton = new Button("Circle"));
p.add(ovalbutton = new Button("Oval"));
p.add(new Label(""));
p.add(bigbutton = new Button("Bigger"));
p.add(smallbutton = new Button("Smaller"));
p.add(new Label(""));
p.add(textbutton = new Button("Set Text"));
p.add(new Label(""));
p.add(fonttype = new Choice());
for (int i=0 ; i < nfontstyles ; i++)
fonttype.addItem(fontstylechoice[i]);
p.add(fontsize = new Choice());
for (int i=0 ; i < nfontsizes ; i++)
fontsize.addItem(fontsizechoice[i]);
p.add(fontbutton = new Button("Set Font"));
p.add(new Label(""));
add("East", p);
Panel q = new Panel();
q.setLayout(new GridLayout(1,2));
q.add(objectbutton = new Button("New Object"));
q.add(namefield = new TextField(24));
add("South", q);
mainpanel = new Panel();
add("Center", mainpanel);
}
Image offscreen;
Dimension offscreensize;
Graphics offgraphics;
public void update(Graphics g)
{
Dimension d = size();
if ((offscreen == null) || (d.width != offscreensize.width) ||
(d.height != offscreensize.height))
{
offscreen = createImage(d.width, d.height);
offscreensize = d;
offgraphics = offscreen.getGraphics();
offgraphics.setFont(getFont());
}
offgraphics.setColor(getBackground());
offgraphics.fillRect(0, 0, d.width, d.height);
for (int i=0 ; i < ndots ; i++) dot[i].draw(offgraphics);
g.drawImage(offscreen, 0, 0, null);
}
public boolean mouseDown(Event evt, int x, int y)
{
for (int i=0 ; i < ndots ; i++)
{
if (dot[i].find(x,y))
{
pick = dot[i];
current = i;
diffx = dot[i].leftOf()-x;
diffy = dot[i].topOf()-y;
break;
}
}
return true;
}
public boolean mouseDrag (Event evt, int x, int y)
{
if (pick != null)
{
pick.setLeft(x+diffx);
pick.setTop(y+diffy);
update(mainpanel.getGraphics());
}
return true;
}
public boolean mouseUp (Event evt, int x, int y)
{
pick = null;
return true;
}
public boolean action (Event evt, Object obj)
{
if (evt.target == fonttype) {
fontstyle = fonttype.getSelectedIndex();
myfont = new Font(fontstyleselect[fontstyle], Font.PLAIN,
Integer.parseInt(fontsizechoice[fontsz]));
} else
if (evt.target == fontsize) {
fontsz = fontsize.getSelectedIndex();
myfont = new Font(fontstyleselect[fontstyle], Font.PLAIN,
Integer.parseInt(fontsizechoice[fontsz]));
} else
if (evt.target == redbutton) {
if (current != -1) dot[current].setColor(dotcolor = Color.red);
} else
if (evt.target == greenbutton) {
if (current != -1) dot[current].setColor(dotcolor = Color.green);
} else
if (evt.target == bluebutton) {
if (current != -1) dot[current].setColor(dotcolor = Color.blue);
} else
if (evt.target == rectbutton) {
if (current != -1)
dot[current] = new RectDot(dot[current].labelOf(),
dot[current].leftOf(),
dot[current].topOf(),
dot[current].widthOf(),
dot[current].heightOf(),
dot[current].colorOf(),
dot[current].getFont());
} else
if (evt.target == circbutton) {
if (current != -1)
dot[current] = new CircDot(dot[current].labelOf(),
dot[current].leftOf(),
dot[current].topOf(),
dot[current].widthOf(),
dot[current].colorOf(),
dot[current].getFont());
} else
if (evt.target == ovalbutton) {
if (current != -1)
dot[current] = new OvalDot(dot[current].labelOf(),
dot[current].leftOf(),
dot[current].topOf(),
2*dot[current].widthOf(),
dot[current].heightOf(),
dot[current].colorOf(),
dot[current].getFont());
} else
if (evt.target == objectbutton) {
current = ndots;
dot[ndots++] = new CircDot(namefield.getText(), 100, 100, 30,
dotcolor, myfont);
} else
if (evt.target == smallbutton) {
if (current != -1)
{
dot[current].setWidth(dot[current].widthOf()-1);
dot[current].setHeight(dot[current].heightOf()-1);
}
} else
if (evt.target == bigbutton) {
if (current != -1)
{
dot[current].setWidth(dot[current].widthOf()+1);
dot[current].setHeight(dot[current].heightOf()+1);
}
} else
if (evt.target == textbutton) {
if (current != -1)
{
dot[current].setFont(myfont);
dot[current].setLabel(namefield.getText());
}
} else
if (evt.target == fontbutton) {
if (current != -1) dot[current].setFont(myfont);
}
update(mainpanel.getGraphics());
return super.action(evt, obj);
}
}