hey need help to in jframe

Hi,

I need to be able to drag a circle in jframe

I try to do something but I don't know hy it does not work

cAn anybody help me plz

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass circleextends JFrame

{

publicstaticint size= 400;

publicstaticint r=50;

privateint x;

privateint y;

privateint cX;

privateint cY;

privateint dX;

privateint dY;

privateint a;

privateint b;

private MouseHandler mh;

privateboolean click =false;

public circle()

{

super("Drag circle");

cX=r/4;

cY=r/4;

mh=new MouseHandler();

addMouseListener(mh);

setSize(size,size);

setVisible(true);

}

publicvoid paint(Graphics g)

{

g.setColor(Color.RED);

g.fillOval(cX,cY,r*2,r*2);

}

privateclass MouseHandlerextends MouseAdapter

{

publicvoid MousePressed(MouseEvent me)

{

a=me.getX();

b=me.getY();

if (Math.sqrt((cX - a)*(cX - a) + (cY - b)*(cY - b)) < r)

dX=me.getX()-x;

dY=me.getY()-y;

}

publicvoid MouseDragged(MouseEvent me)

{

x = me.getX() - dX;

y=me.getY()-dY;

cX= x+r;

cY=y+r;

repaint();

}

}

publicstaticvoid main (String [] args)

{

circle c1 =new circle();

c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

[3592 byte] By [beegee123a] at [2007-10-2 14:26:23]
# 1
For one thing, you capitalized the methods of your MouseAdapter wrong. The "m" should be lowercase: mousePressed, mouseDragged. Therefore, you aren't overriding the methods the way you think you are.
MLRona at 2007-7-13 12:46:21 > top of Java-index,Java Essentials,New To Java...
# 2
still didn't work. I changed the thing that you told me.
beegee123a at 2007-7-13 12:46:21 > top of Java-index,Java Essentials,New To Java...
# 3

The code is rearranged a bit, and there is still a logic error in the circle positioning.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class circle extends JPanel

{

int size= 400;

int r=50;

int x;

int y;

int cX;

int cY;

int dX;

int dY;

int a;

int b;

private boolean click = false;

public circle()

{

cX=r/4;

cY=r/4;

addMouseListener(new MouseHandler());

addMouseMotionListener(new MouseHandler());

setPreferredSize(new Dimension(size,size));

setVisible(true);

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

g.setColor(Color.RED);

g.fillOval(cX,cY,r*2,r*2);

}

private class MouseHandler implements MouseListener,MouseMotionListener

{

public void mousePressed(MouseEvent me)

{

a=me.getX();

b=me.getY();

if (Math.sqrt((cX - a)*(cX - a) + (cY - b)*(cY - b)) < r)

{

dX=me.getX()-x;

dY=me.getY()-y;

}

}

public void mouseDragged(MouseEvent me)

{

x = me.getX() - dX;

y=me.getY()-dY;

cX= x+r;

cY=y+r;

repaint();

}

public void mouseMoved(MouseEvent me){}

public void mouseReleased(MouseEvent me){}

public void mouseClicked(MouseEvent me){}

public void mouseEntered(MouseEvent me){}

public void mouseExited(MouseEvent me){}

}

}

public class beegee

{

public static void main (String [] args)

{

JFrame frame = new JFrame("Drag circle");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new circle());

frame.pack();

frame.setVisible(true);

}

}

ChrisLesliea at 2007-7-13 12:46:21 > top of Java-index,Java Essentials,New To Java...
# 4

mouseDargged is not a member of MouseAdapter - you need a motion listener for this. Try the code I added here (I renamed circle to Circle according to java conventions)

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Circle extends JFrame {

public static int size = 400;

public static int r = 50;

private int x;

private int y;

private int cX;

private int cY;

private int dX;

private int dY;

private int a;

private int b;

private MouseHandler mh;

private boolean click = false;

public Circle() {

super("Drag circle");

cX = r / 4;

cY = r / 4;

mh = new MouseHandler();

addMouseListener(mh);

addMouseMotionListener(mh);

setSize(size, size);

setVisible(true);

}

public void paint(Graphics g) {

g.setColor(Color.RED);

g.fillOval(cX, cY, r * 2, r * 2);

}

private class MouseHandler extends MouseAdapter implements MouseMotionListener{

public void mousePressed(MouseEvent me)

{

a = me.getX();

b = me.getY();

if (Math.sqrt((cX - a) * (cX - a) + (cY - b) * (cY - b)) < r)

dX = me.getX() - x;

dY = me.getY() - y;

}

public void mouseDragged(MouseEvent me)

{

x = me.getX() - dX;

y=me.getY()-dY;

cX= x+r;

cY=y+r;

repaint();

}

public void mouseMoved(MouseEvent e) {

// nothing todo

}

}

public static void main(String[] args) {

Circle c1 = new Circle();

c1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

mezlera at 2007-7-13 12:46:21 > top of Java-index,Java Essentials,New To Java...