can anyone help me with an applet that allows users to drag circles with the mouse?
thanks . . .
Printable View
can anyone help me with an applet that allows users to drag circles with the mouse?
thanks . . .
It does not handle 'backwards' dragging (negative rectangles) but that i
figured you could mess with yourself :)
Code:import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* A JPanel extension for drawing and moving circles using the mouse
*
* I have ignored 'backwards' circles, DIY
*
* @author sjalle
* @version 1.0
*/
class DrawPanel extends JPanel implements MouseListener, MouseMotionListener {
private ArrayList circleRectList=null; // all the rectangles for the ovals
private Point startP=null; // mouse was last pressed at this point
private Point endP=null; // mouse was last moved to/released at this point
private Rectangle activeRectangle=null; // the rectangle of the currently dragged oval
private Point dragOffset=null; // the offset between the mouse click point and an ovals top-left
private boolean isDragging=false; // true when an oval is being dragged
/**
* Constructor
*/
public DrawPanel () {
super();
circleRectList=new ArrayList();
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
/**
* The screen update
* @param g
*/
public void update (Graphics g) {
Color c=g.getColor(); // save current context color
/**
* color background
*/
g.setColor(Color.blue);
g.fillRect(0,0,
this.getSize().width,
this.getSize().height);
/**
* draw circles
*/
for (int i=0; i<circleRectList.size(); i++) {
Rectangle cRect=(Rectangle)circleRectList.get(i);
// color a dragged cirlce cyan
if (cRect==activeRectangle) {
g.setColor(Color.cyan);
} else {
g.setColor(Color.white);
}
g.drawOval(cRect.x,
cRect.y,
cRect.width,
cRect.height);
drawOvalData(g, cRect.x,
cRect.y,
cRect.width,
cRect.height);
}
/**
* Draw current rectangle if user is drawing, if there is an
* activeRectangle then the user is just dragging, not drawing.
*/
if (startP!=null && activeRectangle==null) {
g.setColor(Color.cyan);
g.drawOval(startP.x,
startP.y,
endP.x-startP.x,
endP.y-startP.y);
drawOvalData(g, startP.x,startP.y,
endP.x-startP.x,
endP.y-startP.y);
}
g.setColor(c);// restore current context color
}
/**
* draw oval data
*/
private void drawOvalData(Graphics g, int x, int y, int w, int h) {
g.setColor(Color.yellow);
g.drawString("( X "+x+" Y "+y+" W "+w+" H "+h+" )", x+2, y-2);
}
/**
* Remove all current circles
*/
public void clearCircles () {
this.circleRectList.clear();
repaint(); // show new state
}
/**
* Just do the update
* @param g
*/
public void paint (Graphics g) {
update(g);
}
/**
* Mouse is pressed, reset all activeRectangle values
* @param e
*/
public void mousePressed(MouseEvent e) {
startP=endP=e.getPoint();
/**
* check if mouse is pressed down inside the rectangle of
* an old circle (oval).
* Scan the ld circles list and check for hit.
*/
for (int i=0; i<circleRectList.size(); i++) {
Rectangle r=(Rectangle)circleRectList.get(i);
if (r.contains(e.getPoint())) {
activeRectangle=r;
isDragging=true;
dragOffset=new Point(r.x-e.getPoint().x,
r.y-e.getPoint().y);
break;
}
}
}
/**
* Finish dragging/Store new cirlce in list
* if the mouse release was preceded by mouse dragging.
* @param e
*/
public void mouseReleased(MouseEvent e) {
if (startP==null)
return; // nothing has happened since last mouseReleased
if (activeRectangle==null) {
/**
* this is a newly drawn oval,
* create the rectangle and add to ist
*/
Rectangle r=new Rectangle (new Point(startP),
new Dimension(endP.x-startP.x,
endP.y-startP.y));
if (!r.isEmpty())
circleRectList.add(r);
}
repaint(); // show all circles
/**
* Initiate
*/
startP=null;
endP=null;
activeRectangle=null;
isDragging=false;
}
/**
* Mouse is held down while moving, either a circle is being
* drawn or a circle is being dragged
* @param e
*/
public void mouseDragged(MouseEvent e) {
endP = e.getPoint();
if (isDragging) {
/**
* just set the new location
*/
activeRectangle.setLocation(e.getPoint().x+dragOffset.x,
e.getPoint().y+dragOffset.y);
} else {
/**
* move bottom-right corner of new oval's rectangle
*/
endP.setLocation(e.getPoint());
}
repaint(); // resfresh display
}
/**
* Unused mouse interface methods
* @param e
*/
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
}
/**
* Driver frame for showing a DrawPanel
* @author sjalle
* @version 1.0
*/
public class DrawDragCircles extends JFrame implements ActionListener {
BorderLayout borderLayout1 = new BorderLayout();
JPanel buttonPanel = new JPanel();
JButton clearButton = new JButton();
JButton closeButton = new JButton();
DrawPanel drawPanel = new DrawPanel();
public DrawDragCircles() {
try {
jbInit();
// ensure proper program termination
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
catch(Exception e) {
e.printStackTrace();
}
}
/**
* ************************* MAIN ************************
* @param args
*/
public static void main(String[] args) {
DrawDragCircles ddc = new DrawDragCircles();
ddc.setBounds(20,20,500,500);
ddc.setVisible(true);
}
/**
* Handle button clicks
* @param e
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource()==clearButton) {
this.drawPanel.clearCircles();
} else if (e.getSource()==closeButton) {
System.exit(0);
}
}
/**
* Set up GUI (JBuilder style)
* @throws Exception
*/
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
clearButton.setText("Clear");
closeButton.setText("Close");
closeButton.addActionListener(this);
clearButton.addActionListener(this);
buttonPanel.setBorder(BorderFactory.createEtchedBorder());
this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.add(closeButton, null);
buttonPanel.add(clearButton, null);
this.getContentPane().add(drawPanel, BorderLayout.CENTER);
}
}
thanks for the help.
everything compiles except this line:
activeRectangle.setLocation(e.getPoint().x+dragOff set.x,e.getPoint().y+dragOffset.y);
when i compiled, the program says ')' expected and there is a ^ pointing under the x
Its a little thing with the text transport here, so what you see is the word 'dragOffset'
mangled into two words: 'dragOff set'....:)
thanks again for the help. i'm not that good at programming applets, you really saved me.
thanks . . .