Advertisement
Guest User

Untitled

a guest
Jun 9th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. /*
  2.    Julian Avalos
  3.    Project 2
  4.    Simple Paint Program
  5.    Date Last Modified: 06/09/2014
  6. */
  7.    
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. import java.applet.*;
  12. import java.net.*;
  13.  
  14. public class Paint_Applet extends Applet implements ItemListener {
  15.  
  16.    //FIELD DECLARATION
  17.    private int lastX;
  18.    private int lastY;
  19.    private int x;
  20.    private int y;
  21.    private JButton clear;
  22.    private Graphics g;
  23.    private final int WIDTH = 500;
  24.    private final int HEIGHT = 500;
  25.    private Color current = Color.BLACK;
  26.    private JPanel panel = new JPanel();
  27.    private JPanel panel2 = new JPanel();
  28.    private JPanel panel3 = new JPanel();
  29.    private Choice Colors = new Choice();
  30.    private JLabel imageLabel;
  31.    
  32.    java.net.URL imageURL = Paint_Applet.class.getResource("download.jpg");
  33.    
  34.    
  35.    //CONSTRUCTOR
  36.    public void init(){
  37.      
  38.       //ADD COLOR CHOICES TO CHOICE OBJECT
  39.       Colors.add("Black");
  40.       Colors.add("Blue");
  41.       Colors.add("Red");
  42.       Colors.add("Yellow");
  43.       Colors.add("Green");
  44.      
  45.       //ADD LISTENER TO CHOICE OBJECT
  46.       Colors.addItemListener(this);
  47.      
  48.       //BUILD FRAME          
  49.       panel.setLayout(new BorderLayout());
  50.       //this.setBackground(Color.WHITE);
  51.       clear = new JButton();
  52.       clear.setIcon(new ImageIcon(imageURL));
  53.       panel3.add(Colors);
  54.       panel3.add(clear, BorderLayout.CENTER);
  55.       panel.add(panel2, BorderLayout.CENTER);
  56.       panel.add(panel3, BorderLayout.SOUTH);
  57.       panel2.setBackground(Color.WHITE);
  58.       add(panel);
  59.       //setSize(WIDTH, HEIGHT);
  60.       setVisible(true);
  61.      
  62.       //ADD LISTENERS TO CLEAR BUTTON AND MOUSE
  63.       clear.addActionListener(new ButtonListener());
  64.       panel2.addMouseListener(new MyMouseListener());
  65.       panel2.addMouseMotionListener(new MyMouseMotionListener());
  66.    }
  67.      
  68.    //ADD FUNCTIONALITY TO CLEAR BUTTON
  69.    private class ButtonListener implements ActionListener{
  70.       public void actionPerformed(ActionEvent e){      
  71.          g = panel2.getGraphics();
  72.          Rectangle rect = panel2.getBounds();
  73.          g.setColor(Color.WHITE);
  74.          g.fillRect(rect.x, rect.y, rect.width, rect.height);                            
  75.       }
  76.    }
  77.    
  78.    //ADD FUNCTIONALITY TO CHOICE OBJECT
  79.    public void itemStateChanged(ItemEvent e){
  80.       int currentColor = Colors.getSelectedIndex();
  81.       switch(currentColor){
  82.          case 0:
  83.             current = Color.black;
  84.             break;
  85.          case 1:
  86.             current = Color.blue;
  87.             break;
  88.          case 2:
  89.             current = Color.red;
  90.             break;
  91.          case 3:
  92.             current = Color.yellow;
  93.             break;
  94.          case 4:
  95.             current = Color.green;
  96.             break;
  97.       }
  98.    }                    
  99.  
  100.    //ADD FUNCTIONALITY TO MOUSE BUTTON
  101.    private class MyMouseListener implements MouseListener{
  102.       public void mouseClicked(MouseEvent e){}
  103.       public void mousePressed(MouseEvent e){
  104.          lastX = e.getX();
  105.          lastY = e.getY();
  106.       }
  107.       public void mouseReleased(MouseEvent e){}
  108.       public void mouseEntered(MouseEvent e){}
  109.       public void mouseExited(MouseEvent e){}
  110.    }
  111.    
  112.    //ADD FUNCTIONALITY TO MOUSE MOVEMENT
  113.    private class MyMouseMotionListener implements MouseMotionListener {
  114.       public void mouseDragged(MouseEvent e){
  115.          x = e.getX();
  116.          y = e.getY();
  117.          Graphics g = panel2.getGraphics();
  118.          g.setColor(current);
  119.          g.drawLine(lastX, lastY, x, y);
  120.          lastX = x;
  121.          lastY = y;
  122.       }  
  123.       public void mouseMoved(MouseEvent e){}  
  124.     }
  125.    
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement