Mã:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Mypaint extends JFrame implements ActionListener, MouseListener
{
private JPanel MainPanel,p1,p2,p1a,p1b,p1a1,p1a2,p1b1,p1b2;
private JLabel lmp,llc,lfc;
private JButton b_Rect,b_Circle,b_Polygon,b_Ellipse;
private JComboBox cbo_LineColor,cbo_FillColor;
private Object source;
int x1, x2, y1, y2;
int shape;
public Mypaint()
{
super("My Paint Application");
MainPanel=new JPanel();
p1=new JPanel();
p2=new JPanel();
p1a=new JPanel();
p1b=new JPanel();
p1a1=new JPanel();
p1a2=new JPanel();
p1b1=new JPanel();
p1b2=new JPanel();
getContentPane().add(MainPanel);
MainPanel.setLayout(new GridLayout(2,1,5,5));
MainPanel.add(p1);
MainPanel.add(p2);
p1.setLayout(new GridLayout(2,1,5,5));
p1.add(p1a);
p1.add(p1b);
p1a.setLayout(new GridLayout(2,1,5,5));
p1a.add(p1a1);
p1a.add(p1a2);
p1b.setLayout(new GridLayout(1,2,5,5));
p1b.add(p1b1);
p1b.add(p1b2);
lmp=new JLabel("My Paint Application");
p1a1.add(lmp);
b_Rect=new JButton("Rectangle");
b_Circle=new JButton("Circle");
b_Polygon=new JButton("Polygon");
b_Ellipse=new JButton("Ellipse");
p1a2.setLayout(new FlowLayout());
p1a2.add(b_Rect);
p1a2.add(b_Circle);
p1a2.add(b_Polygon);
p1a2.add(b_Ellipse);
llc=new JLabel("Select Line Color");
lfc=new JLabel("Select Fill Color");
cbo_LineColor=new JComboBox();
cbo_FillColor=new JComboBox();
cbo_LineColor.addItem("Red");
cbo_LineColor.addItem("Magenta");
cbo_LineColor.addItem("Blue");
cbo_LineColor.addItem("Cyan");
cbo_LineColor.addItem("Green");
cbo_LineColor.addItem("Yellow");
cbo_LineColor.addItem("White");
cbo_LineColor.addItem("Gray");
cbo_LineColor.addItem("Black");
cbo_FillColor.addItem("Red");
cbo_FillColor.addItem("Magenta");
cbo_FillColor.addItem("Blue");
cbo_FillColor.addItem("Cyan");
cbo_FillColor.addItem("Green");
cbo_FillColor.addItem("Yellow");
cbo_FillColor.addItem("White");
cbo_FillColor.addItem("Gray");
cbo_FillColor.addItem("Black");
p1b1.setLayout(new GridLayout(2,1,5,5));
p1b1.add(llc);
p1b1.add(cbo_LineColor);
p1b2.setLayout(new GridLayout(2,1,5,5));
p1b2.add(lfc);
p1b2.add(cbo_FillColor);
p2.addMouseListener(this);
b_Rect.addActionListener(this);
b_Circle.addActionListener(this);
b_Polygon.addActionListener(this);
b_Ellipse.addActionListener(this);
}
public void drawShape(int shape) {
Graphics g = getGraphics();
switch (shape) {
case 1:
if (x1<x2 && y1<y2);{
g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
else if (x1<x2 && y1>y2);{
g.drawRect(x1, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
break;
case 2:
if (x1<x2 && y1<y2) {
g.drawPolygon(p);
}
else if (x1>x2 && y1<y2) {
g.drawEl(x2, y1, Math.abs(x2-x1), Math.abs(y2-y1));
}
break;
case 3:
g.drawRect(x1, y1, x2-x1, y2-y1);
break;
}
}
public void actionPerformed(ActionEvent ae)
{
source=ae.getSource();
if (source==b_Rect)
{
}
if (source==b_Circle)
{
}
if (source==b_Polygon)
{
}
if (source==b_Ellipse)
{
}
}
public static void main(String args[])
{
Mypaint pa1=new Mypaint();
pa1.setSize(600,600);
pa1.setVisible(true);
}
}