Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.util.ArrayList;
- import javax.swing.JPanel;
- import java.awt.*;
- public class Paint extends JPanel implements MouseListener, MouseMotionListener {
- private ArrayList points;
- public Color drawColor;
- public boolean setAsLine = false;
- public boolean setAsPaint;
- public Paint() {
- addMouseListener(this);
- addMouseMotionListener(this);
- points = new ArrayList();
- drawColor = Color.CYAN;
- }
- public void clear() {
- points = new ArrayList();
- this.repaint();
- }
- public boolean setAsLine() {
- setAsLine = true;
- return setAsLine;
- }
- public void paint(Graphics g) {
- super.paint(g);
- g.setColor(drawColor);
- if (points.size() >= 2) {
- for (int i = 1; i < points.size(); i++) {
- Point p = (Point) points.get(i);
- Point m = (Point) points.get(i - 1);
- if (setAsLine = true & ((p.x != -10) & m.x != -10))
- g.drawLine(p.x, p.y, m.x, m.y);
- if (setAsLine = false & ((p.x != -10) & m.x != -10))
- g.fillRect(p.x, p.y, m.x, m.y);
- }
- }
- }
- public void drawLine(Graphics g) {
- super.paint(g);
- g.setColor(drawColor);
- if (points.size() >= 2) {
- for (int i = 0; i < points.size() - 1; i++) {
- Point p = (Point) points.get(i);
- Point m = (Point) points.get(i + 1);
- g.drawLine(p.x, p.y, m.x, m.y);
- }
- }
- }
- public void mouseDragged(MouseEvent e) {
- points.add(new Point(e.getPoint()));
- repaint();
- }
- public void mouseMoved(MouseEvent e) {
- points.add(new Point(-10, -10));
- }
- public void mouseClicked(MouseEvent e) {
- }
- public void mousePressed(MouseEvent e) {
- }
- public void mouseReleased(MouseEvent e) {
- }
- public void mouseEntered(MouseEvent e) {
- }
- public void mouseExited(MouseEvent e) {
- }
- }
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.ArrayList;
- public class Window extends JFrame implements ActionListener {
- private Paint Paint;
- private JButton colorButton;
- private JButton backColorButton;
- private JButton clear;
- private JButton brush;
- public Window() {
- JPanel windowPane = new JPanel();
- this.getContentPane().add(windowPane);
- windowPane.setLayout(new BorderLayout());
- JPanel topPanel = new JPanel();
- clear = new JButton("CLEAR");
- clear.addActionListener(this);
- topPanel.add(clear);
- brush=new JButton("Set as brush");
- brush.addActionListener(this);
- topPanel.add(brush);
- topPanel.add(new JLabel("Draw Color: "));
- colorButton = new JButton(" ");
- colorButton.setBackground(Color.cyan);
- colorButton.addActionListener(this);
- topPanel.add(colorButton);
- topPanel.add(new JLabel("Background Color: "));
- backColorButton = new JButton(" ");
- backColorButton.setBackground(Color.black);
- backColorButton.addActionListener(this);
- topPanel.add(backColorButton);
- windowPane.add(topPanel, BorderLayout.NORTH);
- Paint = new Paint();
- Paint.setBackground(Color.black);
- windowPane.add(Paint, BorderLayout.CENTER);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(950, 650);
- this.setTitle("Steven's Paint Program!");
- this.setVisible(true);
- }
- public static void main(String[] args) {
- new Window();
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == clear) {
- Paint.clear();
- }
- if (e.getSource() == brush) {
- Paint.setAsLine();
- Paint.repaint();
- }
- if (e.getSource() == colorButton) {
- Paint.drawColor = JColorChooser.showDialog(null,
- "Choose Draw Color", Paint.drawColor);
- colorButton.setBackground(Paint.drawColor);
- Paint.repaint();
- }
- if (e.getSource() == backColorButton) {
- Paint.setBackground(JColorChooser.showDialog(null,
- "Choose Draw Color", Paint.getBackground()));
- backColorButton.setBackground(Paint.getBackground());
- Paint.repaint();
- }
- }
- }
Add Comment
Please, Sign In to add comment