help with moving shapes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • taishin
    New Member
    • Jun 2008
    • 4

    help with moving shapes

    so far i got this...
    but the shapes that i created i need to be able to click and dragg and move around with the mouse..help!!!!

    and also some where here i need to draw and equal sided triangle


    IN PANEL

    import java.awt.*;
    import java.awt.Color;
    import java.awt.Graphi cs;
    import java.awt.Graphi cs2D;
    import java.awt.event. MouseEvent;
    import javax.swing.JPa nel;
    import java.awt.event. MouseListener;
    import java.awt.event. MouseMotionList ener;
    import java.sql.Array;
    import javax.swing.*;

    public class Drawing extends JPanel
    implements MouseListener, MouseMotionList ener {
    int size = 60;
    int x = 0;
    int y = 0;
    boolean dragging = false;

    public Drawing() {
    addMouseListene r(this);
    addMouseMotionL istener(this);
    }
    /** Creates new form Drawing */
    @Override public void paintComponent( Graphics g) {
    super.paintComp onent(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(ne w Color(125, 167, 116));
    g2d.fillOval(10 , 15, size, size);

    g2d.setColor(ne w Color(42, 179, 231));
    g2d.fillRect(13 0, 15, 90, size);

    g2d.setColor(ne w Color(70, 67, 123));
    g2d.fillRect(25 0, 15, 90, size);


    if (dragging) {
    g2d.setColor(ne w Color(255, 255, 255));
    g2d.drawString( "Dragging",x,y) ;
    }

    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="c ollapsed" desc="Generated Code">
    private void initComponents( ) {

    javax.swing.Gro upLayout layout = new javax.swing.Gro upLayout(this);
    this.setLayout( layout);
    layout.setHoriz ontalGroup(
    layout.createPa rallelGroup(jav ax.swing.GroupL ayout.Alignment .LEADING)
    .addGap(0, 400, Short.MAX_VALUE )
    );
    layout.setVerti calGroup(
    layout.createPa rallelGroup(jav ax.swing.GroupL ayout.Alignment .LEADING)
    .addGap(0, 300, Short.MAX_VALUE )
    );
    }// </editor-fold>

    public void mouseClicked(Mo useEvent e) {
    size = size + 5;
    this.repaint();
    }

    public void mousePressed(Mo useEvent e) {
    this.setCursor( Cursor.getPrede finedCursor(Cur sor.MOVE_CURSOR ));

    }

    public void mouseReleased(M ouseEvent e) {
    this.setCursor( Cursor.getPrede finedCursor(Cur sor.DEFAULT_CUR SOR));
    dragging = false;
    this.repaint();
    }

    public void mouseEntered(Mo useEvent e) {

    }

    public void mouseExited(Mou seEvent e) {

    }

    public void mouseDragged(Mo useEvent e) {
    dragging = true;
    x = e.getX();
    y = e.getY();
    this.repaint();
    }

    public void mouseMoved(Mous eEvent e) {

    }




    // Variables declaration - do not modify
    // End of variables declaration

    }
    IN FRAME

    import java.awt.*;
    import javax.swing.JFr ame;
    /**
    *
    * @author User
    */
    public class Ass4Main {


    public static void main(String[] args){
    Drawing draw = new Drawing();
    JFrame frame = new JFrame("Drawing ");

    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
    frame.add(draw) ;
    frame.setSize(5 00,500);
    frame.setLocati onRelativeTo(nu ll);
    frame.setVisibl e(true);

    }


    }
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    A common way to be able to drag images, shapes, etc... around is to
    1. Subclass JComponent to turn your image, shape, etc... into a component. You can even override the contains method to have a non-rectangular component.
    2. Put the components in a JLayeredPane

    Comment

    Working...