/*** DrawFrame 类文件内容: Email:[email protected] *****/ import java.awt.BorderLayout; import java.awt.Button; import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Label; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; import javax.swing.JOptionPane; public class DrawFrame extends Frame { CheckboxGroup cbg = new CheckboxGroup(); CheckboxGroup shapecbg = new CheckboxGroup(); CheckboxGroup shapeFillcbg = new CheckboxGroup(); Button btn_String =new Button("插入文字"); DrawPanel drawPanel = new DrawPanel(this); public void setFrame() { Button btn_clear = new Button("清屏"); btn_clear.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { drawPanel.shapes.removeAll(drawPanel.shapes); drawPanel.repaint(); drawPanel.shape = null; } }); Label lab1 = new Label("ControlGroup"); btn_clear.setBackground(Color.LIGHT_GRAY); Panel paramPanel = new Panel(); paramPanel.setLayout(new GridLayout(3, 3)); Checkbox cb1 = new Checkbox("Black", cbg, true); Checkbox cb2 = new Checkbox("Red", cbg, false); Checkbox cb3 = new Checkbox("Blue", cbg, false); Checkbox shapecb1 = new Checkbox("线", shapecbg, true); Checkbox shapecb2 = new Checkbox("圆", shapecbg, false); Checkbox shapecb3 = new Checkbox("方", shapecbg, false); Checkbox shapeFillcb1 = new Checkbox("实心", shapeFillcbg, false); Checkbox shapeFillcb2 = new Checkbox("空心", shapeFillcbg, true); paramPanel.add(cb1); paramPanel.add(cb2); paramPanel.add(cb3); paramPanel.add(lab1); paramPanel.add(shapecb1); paramPanel.add(shapecb2); paramPanel.add(shapecb3); //paramPanel.add(new Label()); paramPanel.add(btn_clear); paramPanel.add(shapeFillcb1); paramPanel.add(shapeFillcb2); btn_String.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String str=JOptionPane.showInputDialog("输入字符 "); drawPanel.str=str; } }); paramPanel.add(btn_String); paramPanel.setBackground(Color.LIGHT_GRAY); this.add(paramPanel, BorderLayout.NORTH); this.add(drawPanel,BorderLayout.CENTER); this.setSize(400, 400); this.setLocation(200, 200); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); this.setVisible(true); } public static void main(String[] args) { DrawFrame dp=new DrawFrame(); dp.setFrame(); } } DrawPanel 文件代码: import java.awt.Color; import java.awt.Graphics; import java.awt.Panel; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; import java.awt.event.MouseWheelListener; import java.util.ArrayList; public class DrawPanel extends Panel { DrawFrame df; ArrayList<Shape> shapes =new ArrayList<Shape>(); //Graphics g = getGraphics(); Shape shape = null; String str = null; public DrawPanel(DrawFrame df){ this.df=df; this.setBackground(Color.yellow); this.addMouseListener(new MousePaint1()); this.addMouseMotionListener(new MousePaint2()); } public void paint(Graphics g) { if(shape!= null)shape.draw(g); this.drawShape(g); } private Color getColor() { if(df.cbg.getSelectedCheckbox().getLabel()=="Blue") return Color.blue; if(df.cbg.getSelectedCheckbox().getLabel()=="Red") return Color.red; if(df.cbg.getSelectedCheckbox().getLabel()=="Black") return Color.black; return Color.black; } private Shape getShape() { //System.out.println("return share"); if(str != null) return new Word(); if(df.shapecbg.getSelectedCheckbox().getLabel()=="线") return new Line(); if(df.shapecbg.getSelectedCheckbox().getLabel()=="方") return new Rec(); if(df.shapecbg.getSelectedCheckbox().getLabel()=="圆") return new Circular(); return new Line(); } private boolean getFill(){ if(df.shapeFillcbg.getSelectedCheckbox().getLabel()=="实心") return true; return false; } private void drawShape(Graphics g){ for(int i=0; i<shapes.size(); i++){ shapes.get(i).draw(g); } } class MousePaint1 extends MouseAdapter { public void mousePressed(MouseEvent e) { shape=getShape(); //shape=new Shape(); shape.x = e.getX(); shape.y = e.getY(); shape.c = getColor(); shape.fill=getFill(); shape.str=str; //shape.g = g; //System.out.println("Press"); } public void mouseReleased(MouseEvent e) { if(shape != null) shapes.add(shape); shape = null; str = null; repaint(); System.out.println(shapes.size()); } } class MousePaint2 extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { shape.x1=e.getX(); shape.y1=e.getY(); //shape.draw(g); repaint(); //System.out.println("draw current"); } public void mouseMoved(MouseEvent e) { } } } Shape 文件代码,这是所有图形的父类,以此实现多态: import java.awt.Color; import java.awt.Graphics; public class Shape { int x; int y; int x1; int y1; int width; int height; Color c; String str = null; boolean fill = false; //Graphics g; public void draw(Graphics g) { //System.out.println("shape draws"); //g.draw3DRect(20, 20, 20, 20, true); } public int getDrawX(){ if(x>x1)return x1; return x; } public int getDrawY(){ if(y>y1)return y1; return y; } public int getWidth() { return Math.abs(x-x1); } public int getHeight() { return Math.abs(y-y1); } } Line 文件代码: import java.awt.Color; import java.awt.Graphics; public class Line extends Shape { public void draw(Graphics g) { //System.out.println("draw line"); Color currcolor=g.getColor(); g.setColor(c); g.drawLine(x, y, x1, y1); g.setColor(currcolor); } } Rec 文件代码: import java.awt.Color; import java.awt.Graphics; public class Rec extends Shape { public void draw(Graphics g) { Color currcolor=g.getColor(); g.setColor(c); if(this.fill==true){ g.fill3DRect(this.getDrawX(),this.getDrawY(), this.getWidth(), this.getHeight(), true); } else g.draw3DRect(this.getDrawX(),this.getDrawY(), this.getWidth(), this.getHeight(), true); g.setColor(currcolor); } } Word 文件代码: import java.awt.Color; import java.awt.Graphics; public class Word extends Shape { public void draw(Graphics g) { Color currcolor=g.getColor(); g.setColor(c); g.drawString(str, x, y); System.out.println("word"); g.setColor(currcolor); } }