arrastrar objeto usando el subprograma Java MouseListener
Frecuentes
Visto 6,312 veces
0
I'm trying to create an applet that draws a circle (defined as an object) to the screen then this circle can be dragged across the screen using the mouse. So far when the mouse is pressed the object is drawn and can be dragged, but what I want it to do is draw the object when the applet is started then allow the user to click on the object and drag it. Any help or clues would be much appreciated. here is the code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class sheepDog extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
manAndDog dog;
int xposR;
int yposR;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
dog.display(g);
}
public void actionPerformed(ActionEvent ev)
{}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseMoved(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{}
public void mouseDragged(MouseEvent e)
{
dog = new manAndDog(xposR, yposR);
xposR = e.getX();
yposR = e.getY();
repaint();
}
}
class manAndDog implements MouseListener, MouseMotionListener
{
int xpos;
int ypos;
int circleWidth = 30;
int circleHeight = 30;
Boolean mouseClick;
public manAndDog(int x, int y)
{
xpos = x;
ypos = y;
mouseClick = true;
if (!mouseClick){
xpos = 50;
ypos = 50;
}
}
public void display(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(xpos, ypos, circleWidth, circleHeight);
}
public void mousePressed(MouseEvent e)
{
mouseClick = true;
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseMoved(MouseEvent e)
{}
public void mouseClicked(MouseEvent e)
{}
public void mouseDragged(MouseEvent e)
{
if (mouseClick){
xpos = e.getX();
ypos = e.getY();
}
}
}
Muchas Gracias
2 Respuestas
1
En primera start
method of your applet, assign a location for the manAndDog
objeto y llamada repaint
Reimeus is more correct, the init
method is a better place to initalise the manAndDog.
Hope you don't mind some feedback ;)
- Deberías estar llamando
super.paint(g)
en tupaint
method. In fact, I'd encourage you to useJApplet
y anularpaintComponent
, but that's just me - I don't see the need to continuously recreate the manAndDog object.
For example. If you added a method setLocation
, you could simply call 'setLocation` when the mouse is dragged.
public void mouseDragged(MouseEvent e) {
dog.setLocation(xposR, yposR);
xposR = e.getX();
yposR = e.getY();
repaint();
}
This is more efficient as it's not continuously creating short lived objects. It also means you can do more with the manAndDog
object, such as apply animation. IMHO
Respondido 25 ago 12, 01:08
1
The simplest way is to create the ManAndDog
objeto en tu init()
método, algo como:
dog = new ManAndDog(0, 0);
Respondido 25 ago 12, 01:08
+1 para el uso de init
en lugar de start
, much better idea ;) - Programador loco
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java applet mouseevent or haz tu propia pregunta.
1) Por favor, aprenda común Convenciones de nomenclatura de Java (specifically the case used for the names) for class, method & attribute names & use it consistently. 2) If the teacher did not specify 'applet' code a frame. If they did specify an applet, tell them they are not fit to teach, and should be ashamed of themselves. - Andrew Thompson
public void mouseDragged(MouseEvent e) { dog = new manAndDog(xposR, yposR);
A better design would be to only create as many instances ofmanAndDog
as appear on-screen (e.g. 1), and have get/set methods for the x,y position. - Andrew Thompson@AndrewThompson +1 for shaming the teacher :) - MadProgrammer
@MadProgrammer I was about to write 'smack them upside the head', but was afraid the OP might take that too literally and I'd get sued for advising it. - Andrew Thompson
@AndrewThompson I'll hold 'em ;) - MadProgrammer