Guest User

Untitled

a guest
Jun 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Point;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.awt.event.MouseMotionListener;
  8. import java.util.ArrayList;
  9. import javax.swing.JPanel;
  10. import java.awt.*;
  11.  
  12. public class Paint extends JPanel implements MouseListener, MouseMotionListener {
  13.  
  14.     private ArrayList points;
  15.     public Color drawColor;
  16.     public boolean setAsLine = false;
  17.     public boolean setAsPaint;
  18.  
  19.     public Paint() {
  20.         addMouseListener(this);
  21.         addMouseMotionListener(this);
  22.         points = new ArrayList();
  23.         drawColor = Color.CYAN;
  24.     }
  25.  
  26.     public void clear() {
  27.         points = new ArrayList();
  28.         this.repaint();
  29.     }
  30.  
  31.     public boolean setAsLine() {
  32.         setAsLine = true;
  33.         return setAsLine;
  34.     }
  35.  
  36.     public void paint(Graphics g) {
  37.         super.paint(g);
  38.         g.setColor(drawColor);
  39.         if (points.size() >= 2) {
  40.             for (int i = 1; i < points.size(); i++) {
  41.                 Point p = (Point) points.get(i);
  42.                 Point m = (Point) points.get(i - 1);
  43.                 if (setAsLine = true & ((p.x != -10) & m.x != -10))
  44.                     g.drawLine(p.x, p.y, m.x, m.y);
  45.                 if (setAsLine = false & ((p.x != -10) & m.x != -10))
  46.                     g.fillRect(p.x, p.y, m.x, m.y);
  47.             }
  48.         }
  49.     }
  50.  
  51.     public void drawLine(Graphics g) {
  52.         super.paint(g);
  53.         g.setColor(drawColor);
  54.  
  55.         if (points.size() >= 2) {
  56.             for (int i = 0; i < points.size() - 1; i++) {
  57.                 Point p = (Point) points.get(i);
  58.                 Point m = (Point) points.get(i + 1);
  59.                 g.drawLine(p.x, p.y, m.x, m.y);
  60.             }
  61.         }
  62.     }
  63.  
  64.     public void mouseDragged(MouseEvent e) {
  65.         points.add(new Point(e.getPoint()));
  66.         repaint();
  67.     }
  68.  
  69.     public void mouseMoved(MouseEvent e) {
  70.         points.add(new Point(-10, -10));
  71.     }
  72.  
  73.     public void mouseClicked(MouseEvent e) {
  74.     }
  75.  
  76.     public void mousePressed(MouseEvent e) {
  77.     }
  78.  
  79.     public void mouseReleased(MouseEvent e) {
  80.     }
  81.  
  82.     public void mouseEntered(MouseEvent e) {
  83.     }
  84.  
  85.     public void mouseExited(MouseEvent e) {
  86.     }
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. import java.awt.*;
  103. import java.awt.event.*;
  104. import javax.swing.*;
  105. import java.util.ArrayList;
  106.  
  107. public class Window extends JFrame implements ActionListener {
  108.  
  109.     private Paint Paint;
  110.     private JButton colorButton;
  111.     private JButton backColorButton;
  112.     private JButton clear;
  113.     private JButton brush;
  114.  
  115.     public Window() {
  116.  
  117.         JPanel windowPane = new JPanel();
  118.         this.getContentPane().add(windowPane);
  119.         windowPane.setLayout(new BorderLayout());
  120.  
  121.         JPanel topPanel = new JPanel();
  122.  
  123.         clear = new JButton("CLEAR");
  124.         clear.addActionListener(this);
  125.         topPanel.add(clear);
  126.        
  127.         brush=new JButton("Set as brush");
  128.         brush.addActionListener(this);
  129.         topPanel.add(brush);
  130.  
  131.         topPanel.add(new JLabel("Draw Color: "));
  132.         colorButton = new JButton(" ");
  133.         colorButton.setBackground(Color.cyan);
  134.         colorButton.addActionListener(this);
  135.         topPanel.add(colorButton);
  136.  
  137.         topPanel.add(new JLabel("Background Color: "));
  138.         backColorButton = new JButton(" ");
  139.         backColorButton.setBackground(Color.black);
  140.         backColorButton.addActionListener(this);
  141.         topPanel.add(backColorButton);
  142.  
  143.         windowPane.add(topPanel, BorderLayout.NORTH);
  144.  
  145.         Paint = new Paint();
  146.         Paint.setBackground(Color.black);
  147.  
  148.         windowPane.add(Paint, BorderLayout.CENTER);
  149.  
  150.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  151.         this.setSize(950, 650);
  152.         this.setTitle("Steven's Paint Program!");
  153.         this.setVisible(true);
  154.     }
  155.  
  156.     public static void main(String[] args) {
  157.         new Window();
  158.     }
  159.  
  160.     public void actionPerformed(ActionEvent e) {
  161.         if (e.getSource() == clear) {
  162.             Paint.clear();
  163.         }
  164.        
  165.         if (e.getSource() == brush) {
  166.             Paint.setAsLine();
  167.             Paint.repaint();
  168.             }
  169.  
  170.         if (e.getSource() == colorButton) {
  171.             Paint.drawColor = JColorChooser.showDialog(null,
  172.                     "Choose Draw Color", Paint.drawColor);
  173.             colorButton.setBackground(Paint.drawColor);
  174.             Paint.repaint();
  175.         }
  176.         if (e.getSource() == backColorButton) {
  177.             Paint.setBackground(JColorChooser.showDialog(null,
  178.                     "Choose Draw Color", Paint.getBackground()));
  179.             backColorButton.setBackground(Paint.getBackground());
  180.             Paint.repaint();
  181.         }
  182.     }
  183. }
Add Comment
Please, Sign In to add comment