Advertisement
Nick-O-Rama

DrawDemo

Mar 11th, 2015
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.MouseAdapter;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7.  
  8. public class DrawDemo extends JApplet{
  9.  
  10.     private int x;
  11.     private int y;
  12.     private int[] xPoints;
  13.     private int[] yPoints;
  14.     private Graphics g;
  15.  
  16.     public void init()
  17.     {
  18.         getContentPane().setBackground(Color.BLACK);
  19.         setSize(500, 500);
  20.         xPoints = new int[6];
  21.         yPoints = new int[6];
  22.         addMouseListener(new GMouseListener());
  23.        
  24.     }
  25.    
  26.     public void paint(Graphics g)
  27.     {
  28.         super.paint(g);
  29.         g.setColor(Color.RED);
  30.         g.fillPolygon(xPoints, yPoints, xPoints.length);
  31.     }
  32.    
  33.     private class GMouseListener extends MouseAdapter
  34.     {
  35.         int count = 0;
  36.         public void mousePressed(MouseEvent e)
  37.         {
  38.            System.out.println(e.getX() + ", " + e.getY());
  39.             xPoints[count] = e.getX();
  40.             yPoints[count] = e.getY();
  41.             count++;
  42.            
  43.             if (count == 6)
  44.                 repaint();
  45.         }
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement