package ObjectPrinciple;
import java.awt.EventQueue;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import java.awt.Button;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JList;
public class DrawingBroad implements ItemListener{
private JFrame frame;
private Canvas canvas;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DrawingBroad window = new DrawingBroad();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void itemStateChanged(ItemEvent e) {
JRadioButton rdb;
rdb=(JRadioButton) e.getSource();//把获得的来源转换成单选按钮的类型
//强制转换 事件是由哪个对象发出来的
if(rdb.isSelected())//保证其中有一个按钮是被选中的
{
if (rdb.getText()=="Lines") {//也就是按钮的名称,做到匹配
canvas.setTodoAction(0);//在中做得事件进行编号
}
else if (rdb.getText()=="rectangle") {
canvas.setTodoAction(1);
}
else if (rdb.getText()=="circle") {
canvas.setTodoAction(2);
}//可以在此添加代码,新按钮
else if (rdb.getText()=="move") {
canvas.setTodoAction(100);
}
}
}
/**
* Create the application.
*/
public DrawingBroad() {//构造函数,
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JSplitPane splitPane = new JSplitPane();
splitPane.setDividerLocation(160);
frame.getContentPane().add(splitPane, BorderLayout.CENTER);
canvas= new Canvas();
splitPane.setRightComponent(canvas);
JList<String> list = new JList<String>();
splitPane.setLeftComponent(list);
canvas.setLayers(list);
JToolBar toolBar = new JToolBar();
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
JRadioButton rdbtnLines = new JRadioButton("lines");
rdbtnLines.setSelected(true);//首先选择一个按钮
rdbtnLines.addItemListener(this);
toolBar.add(rdbtnLines);
JRadioButton rdbtnRectangle = new JRadioButton("rectangle");
rdbtnRectangle.addItemListener(this);
toolBar.add(rdbtnRectangle);
JRadioButton rdbtnMove = new JRadioButton("move");
rdbtnMove.addItemListener(this);
toolBar.add(rdbtnMove);
JRadioButton rdbtnCircle = new JRadioButton("circle");
rdbtnCircle.addItemListener(this);
toolBar.add(rdbtnCircle);
ButtonGroup bg=new ButtonGroup();
bg.add(rdbtnLines);
bg.add(rdbtnRectangle);
bg.add(rdbtnMove);
bg.add(rdbtnCircle);
}
}
package ObjectPrinciple;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;
public class Canvas extends JPanel {
private int todoAction;
private Rectangle scope;
private ArrayList shapes;
private JList layers;
public void setLayers(JList<String> layers) {
this.layers = layers;
}
public void setTodoAction(int todoAction) {
this.todoAction = todoAction;
}
public Canvas() {
shapes= new ArrayList<Shape>();
addMouseMotionListener(mouseHandler);
addMouseListener(mouseHandler);
}
public void paintShape(Shape s) {
// TODO Auto-generated method stub
shapes.add(s);
String[] strings = new String[shapes.size()];
int i=0;
for (Shape shape:shapes) {
strings[i++]=shape.toString();
}
layers.setListData(strings);
repaint();
}
private MouseInputListener mouseHandler = new MouseInputAdapter() {
Point startPoint;
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
//初始化
startPoint = e.getPoint();
scope = new Rectangle();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
//消除矩形框
if(todoAction==100) {
Moving m = shapes.get(layers.getSelectedIndex());
m.move(e.getPoint().x, e.getPoint().y);
scope = null;
repaint();
}
else {
setScope(startPoint,e.getPoint());
if(scope.width > 0||scope.height>0)
if(todoAction==0) {
MyLine line = new MyLine();
line.setShape(scope.x, scope.y, scope.width, scope.height, 0, "line");
paintShape(line);
}else if(todoAction == 1) {
MyRectangle rectangle = new MyRectangle();
rectangle.setShape(scope.x, scope.y, scope.width, scope.height, 1, "rectangle");
paintShape(rectangle);
}else if (todoAction == 2) {
Circle circle= new Circle();
circle.setShape(scope.x, scope.y, scope.width, scope.height, 2, "circle");
paintShape(circle);
}
scope=null;
repaint();
}
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
if(scope==null) {
setScope(startPoint, e.getPoint());
}
repaint();
}
private void setScope(Point starPoint,Point endPoint) {
int x=Math.min(startPoint.x, endPoint.x);
int y=Math.min(startPoint.y, endPoint.y);
int w=Math.abs(startPoint.x - endPoint.x);
int h=Math.abs(startPoint.y - endPoint.y);
scope.setBounds(x,y,w,h);
}
};
@Override
public void print(Graphics g) {
// TODO Auto-generated method stub
super.print(g);
g.setColor(Color.BLACK);
if(scope != null)
g.drawRect(scope.x, scope.y, scope.width,scope.height);
//需要一个队列,需要把画的图形加到队列,调用每个元素的drawingshape方法
for(Drawing d:shapes) {
d.drawingShape(g);
}
}
}
package ObjectPrinciple;
import java.awt.Graphics;
public interface Drawing {
public void drawingShape(Graphics g);
}
package ObjectPrinciple;
public interface Moving {
public void move(int x,int y);
}
package ObjectPrinciple;
public abstract class Shape implements Drawing,Moving{
//位置
int x,y;
//宽度,高度
int width,height;
int shapetype;
String shapename;
public void setShape(int x, int y, int width, int height, int shapetype, String shapename) {
//super();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.shapetype = shapetype;
this.shapename = shapename;
}
@Override
public void move(int x, int y) {
// TODO Auto-generated method stub
this.x = x;
this.y = y;
}
@Override
public String toString() {
return shapename;
}
}
package ObjectPrinciple;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
public class MyLine extends Shape {
public void drawingShape(Graphics g) {
Graphics2D g2d=(Graphics2D) g;
Line2D line2d = new Line2D.Double(x,y,x+width,y+height);
g2d.draw(line2d);
}
}
package ObjectPrinciple;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
public class MyRectangle extends Shape {
@Override
public void drawingShape(Graphics g) {
// TODO Auto-generated method stub
Graphics2D g2d=(Graphics2D) g;
//Rectangle2D rect = new Line2D
Rectangle2D rect = new Rectangle2D.Double(x,y,x+width,y+height);
g2d.draw(rect);
}
}