OVH Cloud OVH Cloud

Problem avec la class Container et Component

1 réponse
Avatar
EVA
Bonjour,
le code java suivant fonctionne ; il permet de déplacer une balle jaune en
faisant un drag avec la souris.
Mais je voudrais pouvoir placer plusieurs composants du même type dans un
seul panel ; avoir plusieurs balles à déplacer avec la souris.
Pourriez-vous me donner une solution?
Merci d'avance...
-------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//////////////////////////////////////////////////////////////// class
DragDemo
/** This is an application because it has a main method.
It's also an applet because it extends JApplet.
*/

public class DragDemo extends JApplet {
//============================================================= method
main
/* public static void main(String[] args) {
JFrame window = new JFrame();
window.setTitle("Drag Demo");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new DragBallPanel());
window.pack();
window.show();
}//end main
*/
//====================================================== applet
constructor
public void init() {
DragBallPanel pan1=new DragBallPanel(0,0);
DragBallPanel pan2=new DragBallPanel(50,50);
Container contentPane1= getContentPane();
Container contentPane2= getContentPane();
contentPane1.add(pan1);
contentPane1.add(contentPane2.add(pan2));
}
}//endclass DragDemo

class DragBallPanel extends JPanel implements MouseListener,
MouseMotionListener {
private int _ballX ; // x coord - set from drag
private int _ballY ; // y coord - set from drag
private static final int BALL_DIAMETER = 40; // Diameter of ball
//--- instance variables
/** Ball coords. Changed by mouse listeners. Used by paintComponent.
*/

/** Position in ball of mouse press to make dragging look better. */
private int _dragFromX = 0; // pressed this far inside ball's
private int _dragFromY = 0; // bounding box.

/** true means mouse was pressed in ball and still in panel.*/
private boolean _canDrag = false;

//=============================================================
constructor
/** Constructor sets size, colors, and adds mouse listeners.*/
public DragBallPanel() {
setPreferredSize(new Dimension(300, 300));
setBackground(Color.blue);
}//endconstructor


public DragBallPanel(int ballx,int bally) {
this._ballX=ballx;this._ballY=bally;
setPreferredSize(new Dimension(300, 300));
setBackground(Color.blue);
setForeground(Color.yellow);
//--- Add the mouse listeners.
this.addMouseListener(this);
this.addMouseMotionListener(this);
}//endconstructor

//=================================================== method
paintComponent
/** Ball is drawn at the last recorded mouse listener coordinates. */
public void paintComponent(Graphics g) {
super.paintComponent(g); // Required for background.
g.fillOval(_ballX, _ballY, BALL_DIAMETER, BALL_DIAMETER);
}//end paintComponent

//===================================================== method
mousePressed
/** Set _canDrag if the click is in the ball (or in the bounding
box, which is lazy, but close enuf for this program).
Remember displacement (dragFromX and Y) in the ball
to use as relative point to display while dragging.
*/
public void mousePressed(MouseEvent e) {
int x = e.getX(); // Save the x coord of the click
int y = e.getY(); // Save the y coord of the click

if
(this.inball((double)x,(double)y,(double)_ballX,(double)_ballY,(double)BALL_
DIAMETER)==true) {
_canDrag = true;
_dragFromX = x - _ballX; // how far from left
_dragFromY = y - _ballY; // how far from top
} else {
_canDrag = false;
}
}//end mousePressed

public boolean inball(double x,double y, double coinx,double coiny,double
diam){

if((x-diam/2-coinx)*(x-diam/2-coinx)+(y-diam/2-coiny)*(y-diam/2-coiny)<diam*
diam/4){
return true;
}
return false;
}

//============================================================
mouseDragged
/** Set x,y to mouse position and repaint. */
public void mouseDragged(MouseEvent e) {
if (_canDrag) { // True only if button was pressed inside ball.
//--- Ball pos from mouse and original click displacement
_ballX = e.getX() - _dragFromX;
_ballY = e.getY() - _dragFromY;

//--- Don't move the ball off the screen sides
_ballX = Math.max(_ballX, 0);
_ballX = Math.min(_ballX, getWidth() - BALL_DIAMETER);

//--- Don't move the ball off top or bottom
_ballY = Math.max(_ballY, 0);
_ballY = Math.min(_ballY, getHeight() - BALL_DIAMETER);

this.repaint(); // Repaint because position changed.
}
}//end mouseDragged

//====================================================== method
mouseExited
/** Turn off dragging if mouse exits panel. */
public void mouseExited(MouseEvent e) {
_canDrag = true;
}//end mouseExited

//=============================================== Ignore other mouse
events.
public void mouseMoved (MouseEvent e) {} // ignore these events
public void mouseEntered (MouseEvent e) {} // ignore these events
public void mouseClicked (MouseEvent e) {} // ignore these events
public void mouseReleased(MouseEvent e) {} // ignore these events
}//endclass DragBallPanel

1 réponse

Avatar
Anthony Goubard
Bonjour,

Ton problème n'est pas très clair tu veux déplacer les balles en même temps?
Je te conseille de lire l'article
http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html [EN]
où il y a une demo où tu peux déplacer plusieurs images dans un panel.

Anthony
EVA wrote:
Bonjour,
le code java suivant fonctionne ; il permet de déplacer une balle jaune en
faisant un drag avec la souris.
Mais je voudrais pouvoir placer plusieurs composants du même type dans un
seul panel ; avoir plusieurs balles à déplacer avec la souris.
Pourriez-vous me donner une solution?
Merci d'avance...