Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.AlphaComposite;
- import java.awt.BasicStroke;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.RenderingHints;
- import java.awt.Shape;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.geom.*;
- import java.util.ArrayList;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import javax.swing.ButtonGroup;
- import javax.swing.JPanel;
- import javax.swing.JButton;
- import java.awt.FlowLayout;
- public class DrawingBoardWithMatrix extends JFrame{
- public static void main(String[] args) {
- new DrawingBoardWithMatrix();
- }
- public DrawingBoardWithMatrix() {
- super("Mini Paint Applet");
- ButtonGroup cbg = new ButtonGroup();
- JButton lineButton = new JButton("Line");
- JButton ovalButton = new JButton("Oval");
- JButton rectangleButton = new JButton("Rectangle");
- cbg.add(lineButton);
- cbg.add(ovalButton);
- cbg.add(rectangleButton);
- JPanel shapePanel = new JPanel(new FlowLayout());
- shapePanel.add(lineButton);
- shapePanel.add(ovalButton);
- shapePanel.add(rectangleButton);
- this.setLayout(new BorderLayout());
- this.add(shapePanel, BorderLayout.NORTH);
- this.setSize(800, 800);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.add(new PaintSurface(), BorderLayout.CENTER);
- this.setVisible(true);
- }
- private class PaintSurface extends JComponent {
- ArrayList<Shape> shapes = new ArrayList<Shape>();
- String shapeType = "Rectangle";
- Point startDrag, endDrag;
- public PaintSurface() {
- this.addMouseListener(new MouseAdapter() {
- public void mousePressed(MouseEvent e) {
- startDrag = new Point(e.getX(), e.getY());
- endDrag = startDrag;
- repaint();
- }
- public void mouseReleased(MouseEvent e) {
- Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());
- // Shape l = makeLine(startDrag.x, startDrag.y, e.getX(), e.getY());
- // Shape el = makeEllipse(startDrag.x, startDrag.y, e.getX(), e.getY());
- shapes.add(r);
- // shapes.add(l);
- // shapes.add(el);
- startDrag = null;
- endDrag = null;
- repaint();
- }
- });
- this.addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseDragged(MouseEvent e) {
- endDrag = new Point(e.getX(), e.getY());
- repaint();
- }
- });
- }
- private void paintBackground(Graphics2D g2){
- g2.setPaint(Color.LIGHT_GRAY);
- for (int i = 0; i < getSize().width; i += 10) {
- Shape line = new Line2D.Float(i, 0, i, getSize().height);
- g2.draw(line);
- }
- for (int i = 0; i < getSize().height; i += 10) {
- Shape line = new Line2D.Float(0, i, getSize().width, i);
- g2.draw(line);
- }
- }
- public void paint(Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- paintBackground(g2);
- Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN , Color.RED, Color.BLUE, Color.PINK};
- int colorIndex = 0;
- g2.setStroke(new BasicStroke(2));
- g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
- for (Shape s : shapes) {
- g2.setPaint(Color.BLACK);
- g2.draw(s);
- g2.setPaint(colors[(colorIndex++) % 6]);
- g2.fill(s);
- }
- if (startDrag != null && endDrag != null) {
- g2.setPaint(Color.LIGHT_GRAY);
- Shape r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
- g2.draw(r);
- // Shape l = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
- // g2.draw(l);
- // Shape el = makeLine(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
- // g2.draw(el);
- }
- }
- /* private Line2D.Float makeLine(int x1, int y1, int x2, int y2) {
- return new Line2D.Float(x1, y1, x2, y2);
- }
- */
- private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
- return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
- }
- /* private Ellipse2D.Float makeEllipse(int x1, int y1, int x2, int y2) {
- return new Ellipse2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
- }
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment