画图程序
源代码在线查看: draw.java
/*
* @(#)Draw.java 1.0 05/11/12
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Templatemenu1\ProjectmenuName.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
package LYD.Draw;
import javax.swing.*;
import java.lang.Thread;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.LinkedList;
import java.util.ArrayList;
public class Draw extends JFrame implements ActionListener,ChangeListener{
public static Color foreColor=Color.BLACK;
public static Color backColor=Color.WHITE;
protected TypeDrawController currentController=new TypeDrawController(this);
protected DrawPanel dp=new DrawPanel(currentController);
public static Color c=Color.BLACK;
private JMenuBar menuBar=new JMenuBar();
private JMenu menu=new JMenu("menu");
private JMenuItem menuItem=new JMenuItem("menuItem");
protected JMenu menuEdit=new JMenu("Eiit");
protected JMenu menuShape=new JMenu("Shape");
protected JMenu menucolor=new JMenu("Color");
protected JMenuItem menuRedo=new JMenuItem("Redo");
protected JMenuItem menuUndo=new JMenuItem("Undo");
protected JMenuItem menuPencil=new JMenuItem("Pencil");
protected JMenuItem menuLine=new JMenuItem("Line");
protected JMenuItem menuRectangle=new JMenuItem("Rectangle");
protected JMenuItem menuCircle=new JMenuItem("Circle");
protected JMenuItem menuEllipse=new JMenuItem("Ellipse");
protected JMenuItem menuRubber=new JMenuItem("Rubber");
protected JMenuItem menuBlack=new JMenuItem("Black");
protected JMenuItem menuRed=new JMenuItem("Red");
protected JMenuItem menuColor=new JMenuItem("更多颜色");
protected JPanel toolbar=new JPanel();
protected JPanel menutoolbar=new JPanel();
protected JPanel blackbar=new JPanel();
protected JPanel drawmenubar=new JPanel();
protected JButton btnLine=new JButton("Line");
protected JButton btnPencil=new JButton("Pencil");
protected JButton btnRectangle=new JButton("Rectangle");
protected JButton btnCircle=new JButton("Circle");
protected JButton btnEllipse = new JButton("Ellipse");
protected JButton btnRubber=new JButton("Rubber");
protected JButton btnRed=new JButton("");
protected JButton btnBlack=new JButton("");
protected JLabel labelWidth=new JLabel("画笔宽度:");
protected JSlider sliderWidth=new JSlider(1,30,1);
protected JMenuBar mb=new JMenuBar();
protected JLabel labelText=new JLabel("当前颜色:");
protected JButton btnColor=new JButton(" ");
/*JMenuBar menuBar=new JMenuBar();
JMenu menu=new JMenu("menu");
JMenuItem menuItem=new JMenuItem("item");*/
public Draw() {
menuEdit.add(menuRedo);menuRedo.setEnabled(false);menuRedo.addActionListener(this);
menuEdit.add(menuUndo);menuUndo.setEnabled(false);menuUndo.addActionListener(this);
menuShape.add(menuPencil);menuPencil.addActionListener(this);
menuShape.add(menuLine);menuLine.addActionListener(this);
menuShape.add(menuRectangle);menuRectangle.addActionListener(this);
menuShape.add(menuCircle);menuCircle.addActionListener(this);
menuShape.add(menuEllipse);menuEllipse.addActionListener(this);
menuShape.add(menuRubber);menuRubber.addActionListener(this);
menucolor.add(menuBlack);menuBlack.addActionListener(this);
menucolor.add(menuRed);menuRed.addActionListener(this);
menucolor.add(menuColor);menuColor.addActionListener(this);
mb.add(menuEdit);
mb.add(menuShape);
mb.add(menucolor);
drawmenubar.setBackground(Color.white);
menutoolbar.setLayout(new GridLayout(7,2));
menutoolbar.add(btnLine);btnLine.addActionListener(this);
menutoolbar.add(btnRectangle);btnRectangle.addActionListener(this);
menutoolbar.add(btnCircle);btnCircle.addActionListener(this);
menutoolbar.add(btnEllipse);btnEllipse.addActionListener(this);
menutoolbar.add(btnPencil);btnPencil.addActionListener(this);
menutoolbar.add(btnRubber);btnRubber.addActionListener(this);
menuRed.setBackground(Color.RED);
menutoolbar.add(menuRed);menuRed.addActionListener(this);
menuBlack.setBackground(Color.black);menuBlack.setForeground(Color.RED);
menutoolbar.add(menuBlack);menuBlack.addActionListener(this);
toolbar.add(menutoolbar,"North");
toolbar.add(blackbar,"South");
JPanel statusPanel=new JPanel();
btnColor.setBackground(Color.BLACK);
btnColor.setEnabled(false);
statusPanel.add(labelText);
statusPanel.add(btnColor);
statusPanel.add(labelWidth);//labelWidth.setLabelFor(sliderWidth);
statusPanel.add(sliderWidth);sliderWidth.setSize(20,10);sliderWidth.addChangeListener(this);
setJMenuBar(mb);
dp.setBackground(Color.WHITE);
this.getContentPane().add(toolbar,BorderLayout.WEST);
this.getContentPane().add(dp,BorderLayout.CENTER);
this.getContentPane().add(statusPanel,BorderLayout.SOUTH);
this.setSize(800,600);
//p.setBackground(Color.red);
this.getContentPane().add(menuBar,BorderLayout.NORTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public void setUndoable(boolean b)
{
menuUndo.setEnabled(b);
}
public void setRedoable(boolean b)
{
menuRedo.setEnabled(b);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btnLine)
currentController.SetITypeDrawCommand(TypeDrawController.LineCommand);
else if(e.getSource()==menuLine)
currentController.SetITypeDrawCommand(TypeDrawController.LineCommand);
else if(e.getSource()==btnRectangle)
currentController.SetITypeDrawCommand(TypeDrawController.RectangleCommand);
else if(e.getSource()==menuRectangle)
currentController.SetITypeDrawCommand(TypeDrawController.RectangleCommand);
else if(e.getSource()==btnCircle)
currentController.SetITypeDrawCommand(TypeDrawController.CircleCommand);
else if(e.getSource()==menuCircle)
currentController.SetITypeDrawCommand(TypeDrawController.CircleCommand);
else if(e.getSource()==btnEllipse)
currentController.SetITypeDrawCommand(TypeDrawController.EllipseCommand);
else if(e.getSource()==menuEllipse)
currentController.SetITypeDrawCommand(TypeDrawController.EllipseCommand);
else if(e.getSource()==btnPencil)
currentController.SetITypeDrawCommand(TypeDrawController.PencilCommand);
else if(e.getSource()==menuPencil)
currentController.SetITypeDrawCommand(TypeDrawController.PencilCommand);
else if(e.getSource()==btnRubber)
currentController.SetITypeDrawCommand(TypeDrawController.RubberCommand);
else if(e.getSource()==menuRubber)
currentController.SetITypeDrawCommand(TypeDrawController.RubberCommand);
else if(e.getSource()==btnRed)
setForeColor(Color.RED);
else if(e.getSource()==menuRed)
setForeColor(Color.RED);
else if(e.getSource()==btnBlack)
setForeColor(Color.BLACK);
else if(e.getSource()==menuBlack)
setForeColor(Color.BLACK);
else if(e.getSource()==menuColor)
setForeColor(JColorChooser.showDialog(this,"选择颜色",Color.BLACK));
else if(e.getSource()==menuRedo)
{menuRedo.setEnabled(TypeDrawController.Redo());dp.repaint();}
else if(e.getSource()==menuUndo)
{menuUndo.setEnabled(TypeDrawController.Undo());dp.repaint();}
dp.setCursor(currentController.getCursor());
}
public void stateChanged(ChangeEvent e)
{
if(e.getSource()==sliderWidth)
currentController.setWidth(sliderWidth.getValue());
}
public void setForeColor(Color c)
{
foreColor=c;
btnColor.setBackground(c);
dp.setForeground(foreColor);
currentController.setColor(c);
}
public int getBrushWidth()
{
return sliderWidth.getValue();
}
public static void warning(String str)
{
Object[] options = {"OK"};
JOptionPane.showOptionDialog(null,
str, "Warning",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
}
public static void main(String args[]) {
System.out.println("Starting Draw...");
JFrame.setDefaultLookAndFeelDecorated(true);
Draw mainFrame = new Draw();
mainFrame.setBounds(100,100,800,600);
mainFrame.setResizable(false);
mainFrame.setTitle("Draw");
mainFrame.setVisible(true);
}
}