标题
基于MVC开发一个简易图形编辑器,要求能够在GUI图形界面做出简单的圆形、矩形、椭圆、线段等简单图形。
实验截图
model 层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.graph.model; import java.awt.Graphics; import java.awt.Point; //定义图形抽象类 public abstract class Graph { protected Point pointx; protected Point pointy; public Graph() {} public Graph(Point pointx,Point pointy) { this.pointx=pointx; this.pointy=pointy; } public Point getPointx() { return pointx; } public void setPointx(Point pointx) { this.pointx = pointx; } public Point getPointy() { return pointy; } public void setPointy(Point pointy) { this.pointy = pointy; } public abstract void paint(Graphics g); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.graph.model; import java.awt.Graphics; import java.awt.Point; //画圆 public class Cricle extends Graph { public Cricle(Point pointx,Point pointy) { super(pointx,pointy); } @Override public void paint(Graphics g) { // TODO Auto-generated method stub int r=(int) Math.sqrt((pointy.x-pointx.x)*(pointy.x-pointx.x)+ (pointy.y-pointx.y)* (pointy.y-pointx.y)); g.drawOval(pointx.x, pointx.y, r, r); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.graph.model; import java.awt.Graphics; import java.awt.Point; //画直线 public class Line extends Graph { public Line(Point pointx,Point pointy) { super(pointx,pointy); } public void paint(Graphics g) { g.drawLine(pointx.x,pointx.y,pointy.x,pointy.y); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.graph.model; import java.awt.Graphics; import java.awt.Point; //画椭圆 public class Oval extends Graph { public Oval(Point pointx,Point pointy) { super(pointx,pointy); } public void paint(Graphics g) { g.drawOval(pointx.x, pointx.y, pointy.x-pointx.x, pointy.y-pointx.y); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.graph.model; import java.awt.Graphics; import java.awt.Point; //画矩形 public class Rectangle extends Graph { public Rectangle(Point pointx,Point pointy) { super(pointx,pointy); } public void paint(Graphics g) { g.drawRect(pointx.x, pointx.y, pointy.x-pointx.x, pointy.y-pointx.y); } } |
Controller 层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | package com.graph.controller; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JPanel; import com.graph.model.Cricle; import com.graph.model.Graph; import com.graph.model.Line; import com.graph.model.Oval; import com.graph.model.Rectangle; public class Adapter extends JPanel implements MouseListener{ String shape = ""; //图案类型 Point[] point = new Point[2]; Graph graph;//得到抽象类 public Adapter() { super(); this.setBackground(Color.white); point[0] = new Point(-1,-1); point[1] = new Point(-1,-1); addMouseListener(this); } public String getShape() { return shape; } public void setShape(String shape) { this.shape = shape; } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // 鼠标按下 point[0] = new Point(e.getX(),e.getY()); } @Override public void mouseReleased(MouseEvent e) { //鼠标释放 point[1] = new Point(e.getX(),e.getY()); repaint(); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public void paint(Graphics g) { super.paint(g); if(shape.equals("直线")) { graph=new Line(point[0],point[1]); }else if(shape.equals("矩形")) { graph=new Rectangle(point[0],point[1]); }else if(shape.equals("椭圆")) { graph =new Oval(point[0],point[1]); }else if(shape.equals("圆")) { graph=new Cricle(point[0],point[1]); } if(graph!=null)graph.paint(g); } } |
View 层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package com.graph.view; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JToggleButton; import com.graph.controller.Adapter; public class Frame extends JFrame { private JToggleButton []button=new JToggleButton[4]; private Adapter adapter=new Adapter(); ButtonGroup buttonGroup = new ButtonGroup(); JPanel jp1 = new JPanel(); public Frame() { button[0] =new JToggleButton("直线"); button[1] =new JToggleButton("矩形"); button[2] =new JToggleButton("椭圆"); button[3] =new JToggleButton("圆"); DrawShaperListener buttonListener = new DrawShaperListener(); for(int i=0;i<button.length;i++) { button[i].addActionListener(buttonListener); buttonGroup.add(button[i]); jp1.add(button[i]); } getContentPane().add(jp1,BorderLayout.NORTH); getContentPane().add(adapter,BorderLayout.CENTER); this.setSize(400,400); this.setTitle("Webb 图形编辑器"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public class DrawShaperListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { for (int i=0;i<button.length;i++){ if (e.getSource()==button[i]){ //判断来自于哪个按钮 adapter.setShape(button[i].getText()); } } } } public static void main(String[] args) { // TODO Auto-generated method stub new Frame(); } } |