Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Julian Avalos
- Project 2
- Simple Paint Program
- Date Last Modified: 06/09/2014
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.applet.*;
- import java.net.*;
- public class Paint_Applet extends Applet implements ItemListener {
- //FIELD DECLARATION
- private int lastX;
- private int lastY;
- private int x;
- private int y;
- private JButton clear;
- private Graphics g;
- private final int WIDTH = 500;
- private final int HEIGHT = 500;
- private Color current = Color.BLACK;
- private JPanel panel = new JPanel();
- private JPanel panel2 = new JPanel();
- private JPanel panel3 = new JPanel();
- private Choice Colors = new Choice();
- private JLabel imageLabel;
- java.net.URL imageURL = Paint_Applet.class.getResource("download.jpg");
- //CONSTRUCTOR
- public void init(){
- //ADD COLOR CHOICES TO CHOICE OBJECT
- Colors.add("Black");
- Colors.add("Blue");
- Colors.add("Red");
- Colors.add("Yellow");
- Colors.add("Green");
- //ADD LISTENER TO CHOICE OBJECT
- Colors.addItemListener(this);
- //BUILD FRAME
- panel.setLayout(new BorderLayout());
- //this.setBackground(Color.WHITE);
- clear = new JButton();
- clear.setIcon(new ImageIcon(imageURL));
- panel3.add(Colors);
- panel3.add(clear, BorderLayout.CENTER);
- panel.add(panel2, BorderLayout.CENTER);
- panel.add(panel3, BorderLayout.SOUTH);
- panel2.setBackground(Color.WHITE);
- add(panel);
- //setSize(WIDTH, HEIGHT);
- setVisible(true);
- //ADD LISTENERS TO CLEAR BUTTON AND MOUSE
- clear.addActionListener(new ButtonListener());
- panel2.addMouseListener(new MyMouseListener());
- panel2.addMouseMotionListener(new MyMouseMotionListener());
- }
- //ADD FUNCTIONALITY TO CLEAR BUTTON
- private class ButtonListener implements ActionListener{
- public void actionPerformed(ActionEvent e){
- g = panel2.getGraphics();
- Rectangle rect = panel2.getBounds();
- g.setColor(Color.WHITE);
- g.fillRect(rect.x, rect.y, rect.width, rect.height);
- }
- }
- //ADD FUNCTIONALITY TO CHOICE OBJECT
- public void itemStateChanged(ItemEvent e){
- int currentColor = Colors.getSelectedIndex();
- switch(currentColor){
- case 0:
- current = Color.black;
- break;
- case 1:
- current = Color.blue;
- break;
- case 2:
- current = Color.red;
- break;
- case 3:
- current = Color.yellow;
- break;
- case 4:
- current = Color.green;
- break;
- }
- }
- //ADD FUNCTIONALITY TO MOUSE BUTTON
- private class MyMouseListener implements MouseListener{
- public void mouseClicked(MouseEvent e){}
- public void mousePressed(MouseEvent e){
- lastX = e.getX();
- lastY = e.getY();
- }
- public void mouseReleased(MouseEvent e){}
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
- }
- //ADD FUNCTIONALITY TO MOUSE MOVEMENT
- private class MyMouseMotionListener implements MouseMotionListener {
- public void mouseDragged(MouseEvent e){
- x = e.getX();
- y = e.getY();
- Graphics g = panel2.getGraphics();
- g.setColor(current);
- g.drawLine(lastX, lastY, x, y);
- lastX = x;
- lastY = y;
- }
- public void mouseMoved(MouseEvent e){}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement