Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By: Sammy Samkough
- package com.samkough.sketch.main;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class SketchEvent implements MouseMotionListener, ActionListener
- {
- private static final int WIDTH = 600;
- private static final int HEIGHT = 600;
- JFrame frame;
- JPanel pane, top, canvas, bottom, right;
- JButton red, blue, green, pink, eraser, clear, line, oval;
- Color c;
- Font f;
- private int oldX, oldY, x, y;
- public SketchEvent()
- {
- // defining the objects
- frame = new JFrame("Sketch");
- pane = new JPanel();
- top = new JPanel();
- canvas = new JPanel();
- bottom = new JPanel();
- right = new JPanel();
- c = Color.RED;
- f = new Font("Arial", Font.BOLD, 10);
- // setting the layout of the JPanels
- frame.setContentPane(pane);
- pane.setLayout(new BorderLayout());
- top.setLayout(new FlowLayout());
- bottom.setLayout(new FlowLayout());
- right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
- // setting the background for the drawing JPanel
- canvas.setBackground(Color.WHITE);
- // setting the buttons
- red = new JButton("R");
- blue = new JButton("B");
- green = new JButton("G");
- pink = new JButton("P");
- eraser = new JButton("E");
- clear = new JButton("C");
- line = new JButton("L");
- oval = new JButton("O");
- // setting the size of the buttons
- red.setPreferredSize(new Dimension(50, 30));
- blue.setPreferredSize(new Dimension(50, 30));
- green.setPreferredSize(new Dimension(50, 30));
- pink.setPreferredSize(new Dimension(50, 30));
- eraser.setPreferredSize(new Dimension(50, 30));
- clear.setPreferredSize(new Dimension(50, 30));
- line.setPreferredSize(new Dimension(50, 50));
- oval.setPreferredSize(new Dimension(50, 50));
- // setting the font of the buttons
- red.setFont(f);
- blue.setFont(f);
- green.setFont(f);
- pink.setFont(f);
- eraser.setFont(f);
- clear.setFont(f);
- line.setFont(f);
- oval.setFont(f);
- // setting the region of the JPanels
- pane.add(top, BorderLayout.NORTH);
- pane.add(canvas, BorderLayout.CENTER);
- pane.add(bottom, BorderLayout.SOUTH);
- pane.add(right, BorderLayout.EAST);
- // adding all of the components
- top.add(red);
- top.add(blue);
- top.add(green);
- top.add(pink);
- bottom.add(eraser);
- bottom.add(clear);
- right.add(line);
- right.add(oval);
- // setting the addActionListeners
- red.addActionListener(this);
- blue.addActionListener(this);
- green.addActionListener(this);
- pink.addActionListener(this);
- eraser.addActionListener(this);
- clear.addActionListener(this);
- // setting the addMouseMotionlisteners
- canvas.addMouseMotionListener(this);
- // line.addMouseMotionListener(this);
- // oval.addMouseMotionListener(this);
- // setting up the frame
- frame.setSize(WIDTH, HEIGHT);
- frame.setLocationRelativeTo(null);
- frame.setResizable(false);
- // frame.setTitle("Sammy");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- public int getX()
- {
- return x;
- }
- public int getY()
- {
- return y;
- }
- public void mouseMoved(MouseEvent e)
- {
- oldX = e.getX();
- oldY = e.getY();
- }
- public void mouseDragged(MouseEvent e)
- {
- Graphics g = canvas.getGraphics();
- g.setColor(c);
- g.drawLine(oldX, oldY, e.getX(), e.getY());
- /*
- if (e.getSource() == line)
- {
- g.drawLine(oldX, oldY, e.getX(), e.getY());
- }
- if (e.getSource() == oval)
- {
- g.drawOval(oldX, oldY, e.getX(), e.getY());
- }
- */
- }
- public void actionPerformed(ActionEvent e)
- {
- Graphics g = canvas.getGraphics();
- if (e.getSource() == red)
- {
- c = Color.RED;
- }
- if (e.getSource() == blue)
- {
- c = Color.BLUE;
- }
- if (e.getSource() == green)
- {
- c = Color.GREEN;
- }
- if (e.getSource() == pink)
- {
- c = Color.PINK;
- }
- if (e.getSource() == eraser)
- {
- c = Color.WHITE;
- }
- if (e.getSource() == clear)
- {
- canvas.repaint();
- }
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------------
- // By: Sammy Samkough
- import javax.swing.*;
- import java.awt.*;
- public class SketchEventTester
- {
- public static void main(String args[])
- {
- SketchEvent s1 = new SketchEvent();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment