import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class MyPanel extends JFrame
{
private ObjectInputStream input;
private ObjectOutputStream output;
private JButton choices[];
private String names[]={
"New",
"Line",
"DLine",
"Circle",
"DCircle",
"Rect",
"Fill",
"Move",
"Cutting",
"Coloring",
"Pixels",
};
private String tipText[]={
"刷新",
"画直线",
"画虚线",
"两点画一个圆",
"画一个虚线圆",
"画一个多边形",
"填充一个多边形",
"多边形的平移",
"裁剪图形",
"颜色",
"设置线条粗细",
};
JToolBar buttonPanel ;
private JLabel statusBar;
private DrawPanel drawingArea;
private int width=700,height=500;
drawings[] itemList=new drawings[5000];
private int currentChoice=1;
int index=0,l=0,k=0,t=1;
boolean m=false;
public int x[]=new int[5],y[]=new int[5];
public Color color=Color.black;
int R,G,B;
private float stroke=1.0f;
JComboBox styles;
public MyPanel()
{
super("画图板");
drawingArea=new DrawPanel();
choices=new JButton[names.length];
buttonPanel = new JToolBar( JToolBar.VERTICAL ) ;
buttonPanel = new JToolBar( JToolBar.HORIZONTAL) ;
ButtonHandler handler=new ButtonHandler();
ButtonHandler1 handler1=new ButtonHandler1();
for(int i=0;i<choices.length;i++)
{
choices[i]=new JButton(names[i]);
choices[i].setFont(new Font("宋体",Font.LAYOUT_NO_LIMIT_CONTEXT,12));
choices[i].setMargin( new Insets(0,0,0,0));
choices[i].setFocusable(false);
choices[i].setToolTipText(tipText[i]);
buttonPanel.add(choices[i]);
}
for(int i=1;i<choices.length-2;i++)
{
choices[i].addActionListener(handler);
}
choices[0].addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
newFile();
}
}
);
choices[choices.length-2].addActionListener(handler1);
choices[choices.length-1].addActionListener(handler1);
Container c=getContentPane();
c.add(buttonPanel,BorderLayout.NORTH);
c.add(drawingArea,BorderLayout.CENTER);
statusBar=new JLabel();
c.add(statusBar,BorderLayout.SOUTH);
statusBar.setText(" Welcome To The Little Drawing Pad!!! :)");
createNewItem();
setSize(width,height);
show();
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int j=1;j<choices.length-2;j++)
{
if(e.getSource()==choices[j])
{
currentChoice=j;
createNewItem();
repaint();
}
}
}
}
public class ButtonHandler1 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==choices[choices.length-1])
{
setStroke();
}
if(e.getSource()==choices[choices.length-2])
{
chooseColor();
}
}
}
class mouseA extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
statusBar.setText(" Mouse Pressed @:[" + e.getX() +
", " + e.getY() + "]");
itemList[index].x1=itemList[index].x2=e.getX();
itemList[index].y1=itemList[index].y2=e.getY();
}
public void mouseClicked(MouseEvent e)
{
statusBar.setText(" Mouse Pressed @:[" + e.getX() +
", " + e.getY() + "]");
if(m==true&&t==1)
{
x[l]=e.getX();
y[l]=e.getY();
if(l==4)
{
l=-1;
}
l++;k++;
t=1;
}
if(t==0)
{
itemList[index].X=e.getX();
itemList[index].Y=e.getY();
t=1;
}
}
public void mouseReleased(MouseEvent e)//鼠标松开的时候
{
statusBar.setText(" Mouse Released @:[" + e.getX() +
", " + e.getY() + "]");
itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
repaint();
index++;
createNewItem();
}
public void mouseEntered(MouseEvent e)
{
statusBar.setText(" Mouse Entered @:[" + e.getX() +
", " + e.getY() + "]");
}
public void mouseExited(MouseEvent e)
{
statusBar.setText(" Mouse Exited @:[" + e.getX() +
", " + e.getY() + "]");
}
}
class mouseB extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
statusBar.setText(" Mouse Dragged @:[" + e.getX() +
", " + e.getY() + "]");
itemList[index].x2=e.getX();
itemList[index].y2=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
statusBar.setText(" Mouse Moved @:[" + e.getX() +
", " + e.getY() + "]");
}
}
class DrawPanel extends JPanel
{
public DrawPanel()
{
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
setBackground(Color.white);
addMouseListener(new mouseA());
addMouseMotionListener(new mouseB());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
int j=0;
while (j<=index)
{
draw(g2d,itemList[j]);
j++;
}
}
void draw(Graphics2D g2d,drawings i)
{
i.draw(g2d);
if(k%5==0)
{
i.draw(g2d,x,y);
}
if(t==1)
{
i.draw(g2d,x,y,m);
}
}
}
void createNewItem()
{
if(currentChoice==9)
drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
else
drawingArea.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
switch (currentChoice)
{
case 1:
itemList[index]=new Line();
break;
case 2:
itemList[index]=new DLine();
break;
case 3:
itemList[index]=new Circle();
break;
case 4:
itemList[index]=new DCircle();
break;
case 5:
m=true;
itemList[index]=new Rect();
break;
case 6:
itemList[index]=new Fill();
break;
case 7:
t=0;
itemList[index]=new Move();
break;
case 8:
itemList[index]=new Cutting();
break;
}
//itemList[index].type=currentChoice;
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
itemList[index].stroke=stroke;
}
public void chooseColor()
{
color=JColorChooser.showDialog(MyPanel.this,
"Choose a color",color);
R=color.getRed();
G=color.getGreen();
B=color.getBlue();
itemList[index].R=R;
itemList[index].G=G;
itemList[index].B=B;
}
public void setStroke()
{
String input;
input=JOptionPane.showInputDialog(
"Please input a float stroke value! ( >0 )");
stroke=Float.parseFloat(input);
itemList[index].stroke=stroke;
}
public void newFile()
{
index=0;
currentChoice=1;
color=Color.black;
stroke=1.0f;
createNewItem();