📄 drawfigure.java
字号:
package drawfigure; import java.awt.*; import java.awt.event.*; import javax.swing.*;import java.util.*; class DrawFigure extends JFrame implements MouseListener,MouseMotionListener,AdjustmentListener,ItemListener{ Stack<Shape> stack=new Stack<Shape>(); //暂存所画图形的栈Stack<Shape> delStack=new Stack<Shape>(); //删除图形的栈,用于恢复boolean saved=true; //判断是否已经保存过JPanel p1=new JPanel();JPanel p2=new JPanel();JPanel top=new JPanel();JPanel center=new JPanel();JPanel bottom=new JPanel();ButtonGroup g=new ButtonGroup();JRadioButton op1=new JRadioButton("画",true);JRadioButton op3=new JRadioButton("圆",false);JRadioButton op2=new JRadioButton("直线",false);JRadioButton op4=new JRadioButton("矩形",false);JCheckBox c1=new JCheckBox("填充");Color c=Color.black;JTextArea tt=new JTextArea(2,3);int f=1; //绘图类型,1-任意画,2-画直线,3-画圆,4-画矩形 int sx,sy,x,y; //用于保存坐标boolean fill=false; //是否填充boolean flag=false; //用于判断是否是某个绘图的开始JScrollBar r1 = new JScrollBar(JScrollBar.HORIZONTAL,0,4,0,255); //颜色滚动条JScrollBar g1 = new JScrollBar(JScrollBar.HORIZONTAL,0,4,0,255);JScrollBar b1 = new JScrollBar(JScrollBar.HORIZONTAL,0,4,0,255);public DrawFigure() {getContentPane().setLayout(new BorderLayout());setDefaultCloseOperation(EXIT_ON_CLOSE); JMenuBar mb = new JMenuBar(); //设置菜单文件 JMenu file1 = new JMenu("文件"); mb.add(file1); JMenuItem save = new JMenuItem("保存"); file1.add(save); JMenuItem open = new JMenuItem("导入"); file1.add(open); JMenuItem clear = new JMenuItem("重画"); file1.add(clear); file1.addSeparator(); JMenuItem exit = new JMenuItem("退出"); file1.add(exit); JMenu file2 = new JMenu("编辑"); mb.add(file2); JMenuItem delet = new JMenuItem("清除图形"); file2.add(delet); JMenuItem reverse = new JMenuItem("恢复清除"); file2.add(reverse); top.setLayout(new BorderLayout()); top.add(mb,"West"); center.setOpaque(true); center.setBackground(Color.white);tt.setBackground(Color.black);p1.setLayout(new FlowLayout()); //设置流动布局p1.setBackground(Color.lightGray); //设置背景色p2.setLayout(new GridLayout(3,1)) ; //设置网格布局p2.setPreferredSize(new Dimension(150,50));p2.add(r1);p2.add(g1);p2.add(b1);tt.setEditable(false); //文本不能编辑,用于显示色彩g.add(op1);g.add(op2);g.add(op3);g.add(op4);p1.add(op1);p1.add(op2);p1.add(op3);p1.add(op4);p1.add(c1);p1.add(tt);bottom.add(p1);bottom.add(p2);c1.addItemListener(this);r1.addAdjustmentListener(this);g1.addAdjustmentListener(this);b1.addAdjustmentListener(this);op1.addItemListener(this);op2.addItemListener(this);op3.addItemListener(this);op4.addItemListener(this);center.addMouseListener(this); center.addMouseMotionListener(this);getContentPane().add(top,"North");getContentPane().add(bottom,"South");getContentPane().add(center,"Center");this.setTitle("绘图程序");setSize(500,300);setVisible(true); save.addActionListener(new ActionListener(){ //保存文件 public void actionPerformed(ActionEvent event) { if(!saved) save(); } }); open.addActionListener(new ActionListener(){ //打开存储的图形文件 public void actionPerformed(ActionEvent event) { if(saved) open(); else{ int judge=JOptionPane.showConfirmDialog(null,"修改尚未保存,是否保存画图?", "警告",JOptionPane.INFORMATION_MESSAGE); //0-是,1-否,2-取消。 if(judge==0){ save(); } else if(judge==1){ open(); } else{repaint();} } } }); clear.addActionListener(new ActionListener(){ //清空面板,重画; public void actionPerformed(ActionEvent event) { if(!saved) { int judge=JOptionPane.showConfirmDialog(null,"修改尚未保存,是否保存画图?", "警告",JOptionPane.INFORMATION_MESSAGE); //0-是,1-否,2-取消。 if(judge==0){ save(); } else if(judge==1){ stack.clear();saved=true; repaint(); } else{repaint();} } else { stack.clear(); repaint(); } } }); exit.addActionListener(new ActionListener(){ //退出绘图程序。 public void actionPerformed(ActionEvent event) { if(!saved) { int judge=JOptionPane.showConfirmDialog(null,"修改尚未保存,是否保存画图?", "警告",JOptionPane.INFORMATION_MESSAGE); //0-是,1-否,2-取消。 if(judge==0){ save(); } else if(judge==1){ setVisible(false);System.exit(0); } else{repaint();} } else { setVisible(false); System.exit(0); } } }); delet.addActionListener(new ActionListener(){ //删除最新画的图形 public void actionPerformed(ActionEvent event) { Shape s = new Shape(); if(stack.size()>0){ s=stack.pop(); delStack.push(s); repaint(); } else JOptionPane.showMessageDialog(null,"已经没有图形可以撤销","错误",JOptionPane.INFORMATION_MESSAGE); } }); reverse.addActionListener(new ActionListener(){ //撤销删除 public void actionPerformed(ActionEvent event) { Shape s = new Shape(); if(delStack.size()>0){ s=delStack.pop(); stack.push(s); s.g=center.getGraphics(); s.draw(); } /* else{ repaint(); JOptionPane.showMessageDialog(null,"已经没有图形可以恢复","错误",JOptionPane.INFORMATION_MESSAGE);}*/ } }); /*getContentPane().addFocusListener(new FocusListener(){ @Override public void focusGained(FocusEvent event){ repaint(); } @Override public void focusLost(FocusEvent event){ } });*/}public void save() { Save s = new Save(this); }public void open() { Open o = new Open(this);} @Overridepublic void paint(Graphics g) //覆盖paint方法 { super.paint(g); g=center.getGraphics(); for(int i=0;i<stack.size();i++) { stack.get(i).g=g; stack.get(i).draw(); } }public void mousePressed(MouseEvent e){flag=false;sx=e.getX();sy=e.getY();//绘图开始点 } public void mouseReleased(MouseEvent e) { int x0, y0; Graphics g456 = center.getGraphics(); switch (f) { case 2: //画直线 g456.setXORMode(Color.white); //设置异或画图模式 g456.drawLine(sx, sy, x, y); //清上一次移动时画的线 x = e.getX();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -