import java.awt.*;
public class DrawCircle
{
final static int W=300;
final static int H=300;
final static int R=50;
public static void main(String[] args)
{
Frame f=new Frame();
f.setSize(W,H);
f.show();
Graphics g=f.getGraphics();
g.drawOval(W/2-R,H/2-R,2*R,2*R);
}
}
public class DrawCircle
{
final static int W=300;
final static int H=300;
final static int R=50;
public static void main(String[] args)
{
Frame f=new Frame();
f.setSize(W,H);
f.show();
Graphics g=f.getGraphics();
g.drawOval(W/2-R,H/2-R,2*R,2*R);
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Vector;
public class DrawDemo
{
public static void main(String[] args)
{
new Draw().show();
}
}
class Draw extends Frame implements ItemListener
{
Panel radioPanel;
drawPanel dPanel;
public Draw()
{
dPanel=new drawPanel();
radioPanel=new Panel();
CheckboxGroup cbg=new CheckboxGroup();
Checkbox r1=new Checkbox("point",cbg,true);
Checkbox r2=new Checkbox("line",cbg,false);
radioPanel.add(r1);
radioPanel.add(r2);
r1.addItemListener(this);
r2.addItemListener(this);
this.add("North",radioPanel);
this.add("Center",dPanel);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
this.setSize(300,300);
}
public void itemStateChanged(ItemEvent e)
{
if(((String)e.getItem()).equals("point"))
dPanel.setMode(0);
else
dPanel.setMode(1);
}
}
class drawPanel extends Panel
{
int x1,y1;
int x2,y2;
int mode;
Vector lines=new Vector();
public drawPanel()
{
this.setBackground(Color.orange);
this.addMouseListener(new Mou_Lis());
this.addMouseMotionListener(new MouMotion_Lis());
}
public void setMode(int mode)
{
this.mode=mode;
}
public void paint(Graphics g)
{
int np=lines.size();
for(int i=0;i<np;i++)
{
Rectangle p=(Rectangle)lines.elementAt(i);
if(p.width!=-1)
g.drawLine(p.x,p.y,p.width,p.height);
}
if(mode==1)
{
if(x2!=-1)
g.drawLine(x1,y1,x2,y2);
}
}
private class Mou_Lis extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if(mode==0)
{
x1=e.getX();
y1=e.getY();
lines.addElement(new Rectangle(x1,y1,-1,-1));
repaint();
}
else
{
x1=e.getX();
y1=e.getY();
x2=-1;
}
}
public void mouseReleased(MouseEvent e)
{
if(mode==0)
{
repaint();
}
else
{
x2=e.getX();
y2=e.getY();
lines.addElement(new Rectangle(x1,y1,x2,y2));
x2=-1;
repaint();
}
}
}
private class MouMotion_Lis extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
if(mode==0)
{
x2=e.getX();
y2=e.getY();
lines.addElement(new Rectangle(x1,y1,x2,y2));
x1=x2;
y1=y2;
repaint();
}
else
{
x2=e.getX();
y2=e.getY();
repaint();
}
}
}
}