home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / 2d / samples / BufferedShapeMover.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  6.1 KB  |  205 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.Applet;
  4. import java.awt.image.*;
  5.  
  6. public class BufferedShapeMover extends Applet{
  7.  
  8.     static protected Label label;
  9.  
  10.     public void init(){
  11.         //Initialize the layout.
  12.         setLayout(new BorderLayout());
  13.         add(new BSMCanvas());
  14.         label = new Label("Drag rectangle around within the area");
  15.         add("South", label);
  16.     }
  17.  
  18.     public static void main(String s[]) {
  19.         Frame f = new Frame("BufferedShapeMover");
  20.         f.addWindowListener(new WindowAdapter() {
  21.             public void windowClosing(WindowEvent e) {System.exit(0);}
  22.         });
  23.     Applet applet = new BufferedShapeMover();
  24.     f.add("Center", applet);
  25.     applet.init();
  26.     f.pack();
  27.         f.setSize(new Dimension(550,250));
  28.         f.show();
  29.     }
  30.  
  31. }
  32.  
  33. class BSMCanvas extends Canvas implements MouseListener, MouseMotionListener{
  34.  
  35.     Rectangle rect = new Rectangle(0, 0, 100, 50);
  36.     BufferedImage bi;
  37.     Graphics2D big;
  38.  
  39.         // Holds the coordinates of the user's last mousePressed event.
  40.     int last_x, last_y;
  41.     boolean firstTime = true;
  42.     TexturePaint fillPolka, strokePolka;
  43.         Rectangle area;
  44.  
  45.         // True if the user pressed, dragged or released the mouse outside of the rectangle; false otherwise.
  46.     boolean pressOut = false;        
  47.  
  48.     public BSMCanvas(){
  49.                 setBackground(Color.white);
  50.                 addMouseMotionListener(this);
  51.                 addMouseListener(this);
  52.  
  53.         // Creates the fill texture paint pattern.
  54.                 bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  55.                 big = bi.createGraphics();
  56.                 big.setColor(Color.pink);
  57.                 big.fillRect(0, 0, 7, 7);
  58.                 big.setColor(Color.cyan);
  59.                 big.fillOval(0, 0, 3, 3);
  60.                 Rectangle r = new Rectangle(0,0,5,5);
  61.                 fillPolka = new TexturePaint(bi, r);
  62.         big.dispose();
  63.  
  64.         //Creates the stroke texture paint pattern.
  65.                 bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
  66.         big = bi.createGraphics();        
  67.         big.setColor(Color.cyan);
  68.                 big.fillRect(0, 0, 7, 7);
  69.                 big.setColor(Color.pink);
  70.                 big.fillOval(0, 0, 3, 3);
  71.                 r = new Rectangle(0,0,5,5);
  72.                 strokePolka = new TexturePaint(bi, r);
  73.         big.dispose();
  74.     }
  75.  
  76.         // Handles the event of the user pressing down the mouse button.
  77.     public void mousePressed(MouseEvent e){
  78.  
  79.         last_x = rect.x - e.getX();
  80.         last_y = rect.y - e.getY();
  81.  
  82.                 // Checks whether or not the cursor is inside of the rectangle while the user is pressing themouse.
  83.         if(rect.contains(e.getX(), e.getY())){
  84.             updateLocation(e);
  85.         } else {
  86.         BufferedShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
  87.             pressOut = true;
  88.         }
  89.     }
  90.  
  91.         // Handles the event of a user dragging the mouse while holding down the mouse button.
  92.     public void mouseDragged(MouseEvent e){
  93.  
  94.         if(!pressOut){
  95.              updateLocation(e);
  96.         } else {  
  97.         BufferedShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
  98.         }
  99.     }
  100.  
  101.         // Handles the event of a user releasing the mouse button.
  102.     public void mouseReleased(MouseEvent e){
  103.  
  104.                 // Checks whether or not the cursor is inside of the rectangle when the user releases the
  105.         // mouse button.   
  106.         if(rect.contains(e.getX(), e.getY())){
  107.             updateLocation(e);
  108.         } else {
  109.         BufferedShapeMover.label.setText("First position the cursor on the rectangle and then drag.");
  110.             pressOut = false;
  111.         }
  112.     }
  113.  
  114.     // This method required by MouseListener.
  115.     public void mouseMoved(MouseEvent e){}
  116.  
  117.         // These methods are required by MouseMotionListener.
  118.     public void mouseClicked(MouseEvent e){}
  119.     public void mouseExited(MouseEvent e){}
  120.     public void mouseEntered(MouseEvent e){}
  121.     public void updateLocation(MouseEvent e){
  122.  
  123.         rect.setLocation(last_x + e.getX(), last_y + e.getY());
  124.                 /*
  125.                  * Updates the label to reflect the location of the
  126.                  * current rectangle 
  127.                  * if checkRect returns true; otherwise, returns error message.
  128.                  */
  129.                 if (checkRect()) {
  130.                     BufferedShapeMover.label.setText("Rectangle located at " +
  131.                                                      rect.getX() + ", " +
  132.                                                      rect.getY());
  133.                 } else {
  134.                     BufferedShapeMover.label.setText("Please don't try to "+
  135.                                                      " drag outside the area.");
  136.                 }
  137.         repaint();
  138.     }
  139.  
  140.     public void paint(Graphics g){
  141.                 update(g);
  142.     }
  143.  
  144.     public void update(Graphics g){
  145.         Graphics2D g2 = (Graphics2D)g;
  146.                  
  147.               if(firstTime){
  148.                     Dimension dim = getSize();
  149.                     int w = dim.width;
  150.                     int h = dim.height;
  151.                     area = new Rectangle(dim);
  152.             bi = (BufferedImage)createImage(w, h);
  153.             big = bi.createGraphics();
  154.             rect.setLocation(w/2-50, h/2-25);
  155.             big.setStroke(new BasicStroke(8.0f));
  156.             firstTime = false;
  157.         } 
  158.  
  159.         // Clears the rectangle that was previously drawn.
  160.         big.setColor(Color.white);
  161.         big.clearRect(0, 0, area.width, area.height);
  162.  
  163.         // Draws and fills the newly positioned rectangle to the buffer.
  164.         big.setPaint(strokePolka);
  165.         big.draw(rect);
  166.         big.setPaint(fillPolka);
  167.         big.fill(rect);
  168.  
  169.         // Draws the buffered image to the screen.
  170.         g2.drawImage(bi, 0, 0, this);
  171.     }
  172.     /*
  173.          * Checks if the rectangle is contained within the applet window.  If the rectangle
  174.          * is not contained withing the applet window, it is redrawn so that it is adjacent
  175.          * to the edge of the window and just inside the window.
  176.      */
  177.     boolean checkRect(){
  178.             if (area == null) {
  179.                 return false;
  180.             }
  181.         if(area.contains(rect.x, rect.y, 100, 50)){
  182.             return true;
  183.         }
  184.         int new_x = rect.x;
  185.         int new_y = rect.y;
  186.  
  187.         if((rect.x+100)>area.width){
  188.             new_x = area.width-99;
  189.         }
  190.         if(rect.x < 0){  
  191.             new_x = -1;
  192.         }
  193.         if((rect.y+50)>area.height){
  194.             new_y = area.height-49;
  195.         }
  196.         if(rect.y < 0){  
  197.             new_y = -1;
  198.         }
  199.         rect.setLocation(new_x, new_y);
  200.         return false;
  201.     }
  202.  
  203. }
  204.  
  205.