java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.awt arrow Do rubber-band drawing in Java
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4086
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Do rubber-band drawing in Java E-mail
User Rating: / 17
PoorBest 

The following code snippet shows how to implement rubber-drawing with Java.

import java.applet.*;
import java.awt.*;
import java.util.Vector;

public class SimpleCAD extends Applet {
    
    int w = 200;
    int h = 200;
    Vector lines = new Vector();
    int np = 0;
    int x1,y1;
    int x2,y2;
    int xl,yl;
    Image offImg;
    Graphics offGra;
    Button btnClear, btnUndo;
    
    public void init() {
        
        setLayout(new FlowLayout());
        btnClear = new Button("Clear");
        btnUndo = new Button("Undo");
        add(btnClear);
        add(btnUndo);
        setBackground(new Color(0).black);
        setForeground(new Color(0).white);
    }
    
    public void Dragupdate(Graphics g) {
    /*
     ** rubber-band effect
     */
        g.setXORMode(getBackground());
        setForeground(new Color(0).blue);
        if (xl != -1){
            // erase the old line
            g.drawLine(x1, y1, xl, yl);
            if (x2 != -1) {
                // draw the new one
                g.drawLine(x1, y1, x2, y2);
            }
        }
    }
    
    public void update(Graphics g) {
        
        // draw an offScreen drawing
        Dimension dim = getSize();
        if (offGra == null) {
            offImg = createImage(dim.width, dim.height);
            offGra = offImg.getGraphics();
        }
        offGra.setColor(new Color(0).black);
        offGra.fillRect(0,0,dim.width, dim.height);
        offGra.setColor(new Color(0).white);
        offGra.setPaintMode();
        for (int i=0; i < np; i++) {
            Rectangle p = (Rectangle)lines.elementAt(i);
            if (p.width != -1) {
                offGra.drawLine(p.x, p.y, p.width, p.height);
            }
        }
        // put the OffScreen image OnScreen
        g.drawImage(offImg,0,0,null);
    }
    
    public boolean handleEvent(Event e) {
        
        switch (e.id) {
            case Event.MOUSE_DOWN:
                // new starting point
                x1 = e.x;
                y1 = e.y;
                // begin an new drawing process
                x2 = -1;
                return true;
            case Event.MOUSE_UP:
                // end a drawing process
                lines.addElement(new Rectangle(x1, y1, e.x, e.y));
                np++;
                x2 = xl = -1;
                repaint();
                return true;
            case Event.MOUSE_DRAG:
                // xl yl line to be erased
                xl = x2;
                yl = y2;
                // x2 y2 last current point
                x2 = e.x;
                y2 = e.y;
                Dragupdate(getGraphics());
                return true;
        }
        return super.handleEvent(e);
    }
    
    public boolean action(Event e, Object o) {
        if (e.target == btnClearresetDrawing();
        if (e.target == btnUndo)  undo();
        return true;
    }
    
    public void undo() {
        if (np>0) {
            lines.removeElementAt(np-1);
            np--;
            repaint();
        }
    }
    
    public void resetDrawing() {
        lines.removeAllElements();
        np=0;
        repaint();
    }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.