scopolamine

DrawingBoardWithMatrix

Apr 9th, 2011
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. import java.awt.AlphaComposite;
  2. import java.awt.BasicStroke;
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Point;
  8. import java.awt.RenderingHints;
  9. import java.awt.Shape;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseMotionAdapter;
  13. import java.awt.geom.*;
  14. import java.util.ArrayList;
  15. import javax.swing.JComponent;
  16. import javax.swing.JFrame;
  17. import javax.swing.ButtonGroup;
  18. import javax.swing.JPanel;
  19. import javax.swing.JButton;
  20. import java.awt.FlowLayout;
  21.  
  22. public class DrawingBoardWithMatrix extends JFrame{
  23.  
  24.  
  25.   public static void main(String[] args) {
  26.       new DrawingBoardWithMatrix();
  27.   }
  28.  
  29.  
  30.   public DrawingBoardWithMatrix() {
  31.     super("Mini Paint Applet");
  32.     ButtonGroup cbg = new ButtonGroup();
  33.     JButton lineButton = new JButton("Line");
  34.     JButton ovalButton = new JButton("Oval");
  35.     JButton rectangleButton = new JButton("Rectangle");
  36.     cbg.add(lineButton);
  37.     cbg.add(ovalButton);
  38.     cbg.add(rectangleButton);
  39.     JPanel shapePanel = new JPanel(new FlowLayout());
  40.     shapePanel.add(lineButton);
  41.     shapePanel.add(ovalButton);
  42.     shapePanel.add(rectangleButton);
  43.     this.setLayout(new BorderLayout());
  44.     this.add(shapePanel, BorderLayout.NORTH);
  45.     this.setSize(800, 800);
  46.     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47.     this.add(new PaintSurface(), BorderLayout.CENTER);
  48.     this.setVisible(true);
  49.   }
  50.  
  51.   private class PaintSurface extends JComponent {
  52.     ArrayList<Shape> shapes = new ArrayList<Shape>();
  53.     String shapeType = "Rectangle";
  54.  
  55.     Point startDrag, endDrag;
  56.  
  57.     public PaintSurface() {
  58.       this.addMouseListener(new MouseAdapter() {
  59.         public void mousePressed(MouseEvent e) {
  60.           startDrag = new Point(e.getX(), e.getY());
  61.           endDrag = startDrag;
  62.           repaint();
  63.         }
  64.  
  65.         public void mouseReleased(MouseEvent e) {
  66.             Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());
  67. //          Shape l = makeLine(startDrag.x, startDrag.y, e.getX(), e.getY());
  68. //          Shape el = makeEllipse(startDrag.x, startDrag.y, e.getX(), e.getY());
  69.           shapes.add(r);
  70. //          shapes.add(l);
  71. //          shapes.add(el);
  72.           startDrag = null;
  73.           endDrag = null;
  74.           repaint();
  75.         }
  76.       });
  77.  
  78.       this.addMouseMotionListener(new MouseMotionAdapter() {
  79.         public void mouseDragged(MouseEvent e) {
  80.           endDrag = new Point(e.getX(), e.getY());
  81.           repaint();
  82.         }
  83.       });
  84.     }
  85.     private void paintBackground(Graphics2D g2){
  86.       g2.setPaint(Color.LIGHT_GRAY);
  87.       for (int i = 0; i < getSize().width; i += 10) {
  88.         Shape line = new Line2D.Float(i, 0, i, getSize().height);
  89.         g2.draw(line);
  90.       }
  91.  
  92.       for (int i = 0; i < getSize().height; i += 10) {
  93.         Shape line = new Line2D.Float(0, i, getSize().width, i);
  94.         g2.draw(line);
  95.       }
  96.  
  97.  
  98.     }
  99.     public void paint(Graphics g) {
  100.       Graphics2D g2 = (Graphics2D) g;
  101.       g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  102.       paintBackground(g2);
  103.       Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK};
  104.       int colorIndex = 0;
  105.  
  106.       g2.setStroke(new BasicStroke(2));
  107.       g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
  108.  
  109.       for (Shape s : shapes) {
  110.         g2.setPaint(Color.BLACK);
  111.         g2.draw(s);
  112.         g2.setPaint(colors[(colorIndex++) % 6]);
  113.         g2.fill(s);
  114.       }
  115.  
  116.       if (startDrag != null && endDrag != null) {
  117.         g2.setPaint(Color.LIGHT_GRAY);
  118.         Shape r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
  119.         g2.draw(r);
  120.  
  121. //        Shape l = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
  122. //        g2.draw(l);
  123. //        Shape el = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
  124. //        g2.draw(el);
  125.       }
  126.     }
  127.  
  128. /*    private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
  129.         return new Line2D.Float(x1, y1, x2, y2);
  130.  }
  131. */
  132.  
  133.     private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
  134.       return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
  135.     }
  136.  
  137. /*    private Ellipse2D.Float makeEllipse(int x1, int y1, int x2, int y2) {
  138.       return new Ellipse2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
  139.     }
  140. */  
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment