import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; abstract class Figure { protected int x, y, width, height,start,end; protected Color color; public Figure(int x,int y,int w,int h,Color c) { this.x = x; this.y = y; width = w; height = h; color = c; } public void setSize(int w, int h) { width = w; height = h; } public void setLocation(int x, int y) { this.x = x; this.y = y; } abstract public void reshape(int x1,int y1,int x2,int y2); abstract public void paint(Graphics g); } class RectangleFigure extends Figure { public RectangleFigure(int x, int y, int w, int h, Color c) { super(x, y, w, h, c); } public void reshape(int x1,int y1,int x2,int y2) { x = Math.min(x1, x2); y = Math.min(y1, y2); width = Math.abs(x1 - x2); height = Math.abs(y1 - y2); } public void paint(Graphics g) { g.setColor(color); g.drawRect(x, y, width, height); } } class fRectangleFigure extends Figure { public fRectangleFigure(int x, int y, int w, int h, Color c) { super(x, y, w, h, c); } public void reshape(int x1,int y1,int x2,int y2) { x = Math.min(x1, x2); y = Math.min(y1, y2); width = Math.abs(x1 - x2); height = Math.abs(y1 - y2); } public void paint(Graphics g) { g.setColor(color); g.fillRect(x, y, width, height); } } class OvalFigure extends Figure{ public OvalFigure(int x, int y, int w, int h, Color c) { super(x, y, w, h, c); } public void reshape(int x1,int y1,int x2,int y2) { x = Math.min(x1, x2); y = Math.min(y1, y2); width = Math.abs(x1 - x2); height = Math.abs(y1 - y2); } public void paint(Graphics g) { g.setColor(color); g.drawOval(x, y, width, height); } } class fOvalFigure extends Figure { public fOvalFigure(int x,int y,int w,int h,Color c){ super(x, y, w, h, c); } public void reshape(int x1,int y1,int x2,int y2){ x = Math.min(x1, x2); y = Math.min(y1, y2); width = Math.abs(x1 - x2); height = Math.abs(y1 - y2); } public void paint(Graphics g){ g.setColor(color); g.fillOval(x, y, width, height); } } class LineFigure extends Figure { public LineFigure(int x,int y,int w,int h,Color c){ super(x, y, w, h, c); } public void reshape(int x1,int y1,int x2,int y2){ x = x1; y = y1; width = x2; height = y2; } public void paint(Graphics g){ g.setColor(color); g.drawLine(x, y, width, height); } } class DrawApplication { protected Vector
figures; protected Figure drawingFigure; protected Color currentColor; protected DrawPanel drawPanel; boolean fill = false; boolean rect = true; boolean oval = false; boolean line = false; public DrawApplication() { figures = new Vector
(); drawingFigure = null; currentColor = Color.red; } public void setDrawPanel(DrawPanel c) { drawPanel = c; } public int getNumberOfFigures() { return figures.size(); } public Figure getFigure(int index) { return (Figure)figures.elementAt(index); } public void createFigure(int x, int y) { Figure f = null; if(rect==true){ if(fill==false) f = new RectangleFigure(x, y, 0, 0, currentColor); else f = new fRectangleFigure(x, y, 0, 0, currentColor); } else if(oval==true){ if(fill==false) f = new OvalFigure(x, y, 0, 0, currentColor); else f = new fOvalFigure(x, y, 0, 0, currentColor); } else if(line==true){ f = new LineFigure(x, y, x, y, currentColor); } figures.addElement(f); drawingFigure = f; drawPanel.repaint(); } public void reshapeFigure(int x1,int y1,int x2,int y2) { if (drawingFigure != null) { drawingFigure.reshape(x1, y1, x2, y2); drawPanel.repaint(); } } public void setColor(Color c){ currentColor = c; } public Color getColor(){ return currentColor; } public void pRect(){ rect = true; oval = false; line = false; } public void pOval(){ rect = false; oval = true; line = false; } public void pLine(){ rect = false; oval = false; line = true; } public void pfill(){ fill = !fill; } public void allclear(){ figures = new Vector
(); drawingFigure = null; drawPanel.repaint(); currentColor = Color.red; } } class DrawPanel extends JPanel { /** * */ private static final long serialVersionUID = 1L; protected DrawApplication drawApplication; public DrawPanel(DrawApplication app) { setBackground(Color.white); drawApplication = app; } public void paintComponent(Graphics g) { super.paintComponent(g); for(int i = 0;i < drawApplication.getNumberOfFigures(); i++){ Figure f = drawApplication.getFigure(i); f.paint(g); } } } class DrawMouseListener implements MouseListener,MouseMotionListener { protected DrawApplication drawApplication; protected int dragStartX,dragStartY; public DrawMouseListener(DrawApplication a) { drawApplication = a; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { dragStartX = e.getX(); dragStartY = e.getY(); drawApplication.createFigure(dragStartX, dragStartY); } public void mouseReleased(MouseEvent e) { drawApplication.reshapeFigure(dragStartX,dragStartY,e.getX(),e.getY()); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { drawApplication.reshapeFigure(dragStartX,dragStartY,e.getX(),e.getY()); } public void mouseMoved(MouseEvent e) { } } class DrawMain { public static void main(String argv[]) { JFrame f = new JFrame("Draw"); final DrawApplication app = new DrawApplication(); DrawPanel c =new DrawPanel(app); DrawMouseListener dm = new DrawMouseListener(app); c.addMouseListener(dm); c.addMouseMotionListener(dm); app.setDrawPanel(c); JPanel p = new JPanel(); JPanel p2 = new JPanel(); final JButton cc = new JButton("F(Ô¥—Î¥Â)"); final JButton fc = new JButton("˜gÌ“h‚è‚‚Ԃµ"); final JButton ac = new JButton("‘SÁ‚µ"); final JButton rtc = new JButton("ŽlŠp"); final JButton otc = new JButton("‘ȉ~"); final JButton ltc = new JButton("ü"); cc.setForeground(app.getColor()); p.add(cc); p.add(fc); p.add(ac); p2.add(rtc); p2.add(otc); p2.add(ltc); cc.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (app.getColor() == Color.red){ app.setColor(Color.green); }else if(app.getColor() == Color.green){ app.setColor(Color.blue); }else if(app.getColor() == Color.blue){ app.setColor(Color.red); } cc.setForeground(app.getColor()); if(app.fill==true) fc.setForeground(app.getColor()); else fc.setForeground(Color.black); } }); fc.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ app.pfill(); fc.setForeground(app.getColor()); if(app.fill==false) fc.setForeground(Color.black); } }); ac.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ app.allclear(); if(app.fill==true) app.pfill(); fc.setForeground(Color.black); cc.setForeground(app.getColor()); } }); rtc.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ app.pRect(); } }); otc.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ app.pOval(); } }); ltc.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ app.pLine(); } }); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(p, BorderLayout.NORTH); f.getContentPane().add(c, BorderLayout.CENTER); f.getContentPane().add(p2, BorderLayout.SOUTH); f.setSize(400,300); f.setVisible(true); } }