IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement Web en Java Discussion :

Drag and move two shapes in one JFrame


Sujet :

Développement Web en Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Inscrit en
    Mars 2013
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mars 2013
    Messages : 3
    Points : 4
    Points
    4
    Par défaut Drag and move two shapes in one JFrame
    Hi, I want to change the height of each blocks of a stacked bar graph with the mouse. If two blocks (rectangles) are stacked, I want that when the lower edge of the upper block is moved, it also moves the upper edge of the lower block. I want also to change the height of the upper block and/or the lower block.



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
     
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.*;
     
    public class Resizing extends JPanel {
        Rectangle rect = new Rectangle(100,100,150,150);
        Rectangle rect2 = new Rectangle(100,250,150,100);
     
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(new Color(0, 0, 200));
            g2.fill(rect);
            g2.setColor(new Color(0, 0, 100));
            g2.fill(rect2);
        }
     
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Resizing test = new Resizing();
            Resizer resizer = new Resizer(test);
            test.addMouseListener(resizer);
            test.addMouseMotionListener(resizer);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
        }
    }
     
    class Resizer extends MouseAdapter {
        Resizing component;
        boolean dragging = false;
        // Give user some leeway for selections.
        final int PROX_DIST = 3;
     
        public Resizer(Resizing r) {
            component = r;
        }
     
        public void mousePressed(MouseEvent e) {
            if(component.getCursor() != Cursor.getDefaultCursor()) {
                // If cursor is set for resizing, allow dragging.
                dragging = true;
            }
        }
     
        public void mouseReleased(MouseEvent e) {
            dragging = false;
        }
     
        public void mouseDragged(MouseEvent e) {
            if(dragging){
                Point p = e.getPoint();
                Rectangle r = component.rect;
                int type = component.getCursor().getType();
                int dx = p.x - r.x;
                int dy = p.y - r.y;
                switch(type) {
                    case Cursor.N_RESIZE_CURSOR:
                        int height = r.height - dy;
                        r.setRect(r.x, r.y+dy, r.width, height);
                        break;
                    case Cursor.S_RESIZE_CURSOR:
                        height = dy;
                        r.setRect(r.x, r.y, r.width, height);
                        break;
                    default:
                        System.out.println("unexpected type: " + type);
                }
                component.repaint();
            }
        }
     
        public void mouseMoved(MouseEvent e) {
            Point p = e.getPoint();
            if(!isOverRect(p)) {
                if(component.getCursor() != Cursor.getDefaultCursor()) {
                    // If cursor is not over rect reset it to the default.
                    component.setCursor(Cursor.getDefaultCursor());
                }
                return;
            }
            // Locate cursor relative to center of rect.
            int outcode = getOutcode(p);
            Rectangle r = component.rect;
            switch(outcode) {
                case Rectangle.OUT_TOP:
                    if(Math.abs(p.y - r.y) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.N_RESIZE_CURSOR));
                    }
                    break;
                case Rectangle.OUT_BOTTOM:
                    if(Math.abs(p.y - (r.y+r.height)) < PROX_DIST) {
                        component.setCursor(Cursor.getPredefinedCursor(
                                            Cursor.S_RESIZE_CURSOR));
                    }
                    break;
                default:    // center
                    component.setCursor(Cursor.getDefaultCursor());
            }
        }
     
        /**
         * Make a smaller Rectangle and use it to locate the
         * cursor relative to the Rectangle center.
         */
        private int getOutcode(Point p) {
            Rectangle r = (Rectangle)component.rect.clone();
            r.grow(-PROX_DIST, -PROX_DIST);
            return r.outcode(p.x, p.y);
        }
     
        /**
         * Make a larger Rectangle and check to see if the
         * cursor is over it.
         */
        private boolean isOverRect(Point p) {
            Rectangle r = (Rectangle)component.rect.clone();
            r.grow(PROX_DIST, PROX_DIST);
            return r.contains(p);
        }
    }
    Images attachées Images attachées  

Discussions similaires

  1. Drag and drop "de l'extérieur"
    Par Invité dans le forum C++Builder
    Réponses: 12
    Dernier message: 31/03/2020, 10h10
  2. "Drag and drop" avec directinput
    Par batosai dans le forum DirectX
    Réponses: 1
    Dernier message: 16/06/2004, 16h48
  3. [VB.NET] Microsoft TreeView drag and drop ?
    Par bigtoof dans le forum ASP.NET
    Réponses: 7
    Dernier message: 24/05/2004, 14h50
  4. [JSP][DRAG AND DROP]
    Par hamed dans le forum Servlets/JSP
    Réponses: 7
    Dernier message: 23/01/2004, 17h36
  5. drag and drop
    Par jujuesteban dans le forum Composants VCL
    Réponses: 5
    Dernier message: 20/06/2003, 09h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo