Welcome to Dream.In.Code
Become a Java Expert!

Join 149,950 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,328 people online right now. Registration is fast and FREE... Join Now!




Click and Drag?

 
Closed TopicStart new topic

Click and Drag?, I'm very, very new to Java

brandon_v
11 Sep, 2008 - 04:18 PM
Post #1

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 26


My Contributions
Ok, I just started learning Java today, and I don't really know what it will take to get this to where I want.
I created this little applet that simply draws some crappy air balloon.

CODE
import java.awt.*;
import java.applet.*;

//Air Balloon Applet
//Brandon Vrooman

public class AirBalloon extends Applet
{    
    public void paint (Graphics g)
    {
        //Create Sky
        setBackground (Color.cyan);
        
        //Create Balloon Outline
        g.setColor (Color.black);
        g.drawArc (0, 0, 200, 200, 0, 180);
        g.drawArc (0, -80, 200, 350, 180, 90);
        g.drawArc (0, -80, 200, 350, 270, 90);

        //Create Balloon Colour
        g.setColor (Color.blue);
        g.fillArc (0, 0, 200, 200, 0, 180);
        g.fillArc (0, -80, 200, 350, 180, 90);
        g.fillArc (0, -80, 200, 350, 270, 90);
        
        //Continue Outline
        g.setColor (Color.black);
        g.drawArc (60, 0, 80, 200, 0, 180);
        g.drawArc (60, -80, 80, 350, 180, 90);
        g.drawArc (60, -80, 80, 350, 270, 90);
        
        //Continue Balloon Colour
        g.setColor (Color.magenta);
        g.fillArc (60, 0, 80, 200, 0, 180);
        g.fillArc (60, -80, 80, 350, 180, 90);
        g.fillArc (60, -80, 80, 350, 270, 90);
        
        //Add Ropes
        g.setColor (Color.black);
        g.drawLine (20, 200, 50, 320);
        g.drawLine (100, 200, 100, 320);
        g.drawLine (120 + 60, 200, 90 + 60, 320);
        g.drawLine (20, 200, 180, 200);
        
        //Create Basket
        Polygon basket = new Polygon();
        basket.addPoint(20, 320);    // A            A.    ___________     B.
        basket.addPoint(180, 320);    // B                 \          /
        basket.addPoint(150, 380);    // C            D.       \_______/    C.
        basket.addPoint(50, 380);    // D                    Basket =D
        
        //Create Basket Colour
        g.setColor (Color.red);
        g.fillPolygon(basket);
        
        //Add Basket Planks
        g.setColor (Color.black);
        g.drawLine (20, 320, 180, 320);
        g.drawLine (25, 330, 175, 330);
        g.drawLine (30, 340, 170, 340);
        g.drawLine (35, 350, 165, 350);
        g.drawLine (40, 360, 160, 360);
        g.drawLine (45, 370, 155, 370);
        g.drawLine (50, 380, 150, 380);        
    }
}


What I want is to be able to click anywhere on the air balloon, and be able to drag it around inside the applet boundaries.
I have no idea what it will take to get it to that point.
Any help would be awesome!

Thanks.

This post has been edited by brandon_v: 11 Sep, 2008 - 05:40 PM
User is online!Profile CardPM
+Quote Post

brandon_v
RE: Click And Drag?
11 Sep, 2008 - 05:39 PM
Post #2

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 26


My Contributions
ok, after a little research, this is what I came up with.
It still doesn't work at all though, but maybe it's closer.

CODE
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

//Air Balloon Applet
//Brandon Vrooman

public class AirBalloon
extends Applet
implements MouseListener, MouseMotionListener
{    
    int xoffset;
    int yoffset;

    public void init()
    {
        //Create Sky
        setBackground (Color.cyan);
        
        xoffset = 0;
        yoffset = 0;
        
        addMouseListener(this);
        addMouseMotionListener (this);
    }
    
    public void mouseDragged (MouseEvent e)
    {
        int xold;
        int yold;
        xold = xoffset;
        yold = yoffset;
        
        xoffset = e.getX() - xold;
        yoffset = e.getY() - yold;
        
        repaint ();
    }
    
    public void mouseEntered (MouseEvent e){}
    public void mouseExited (MouseEvent e){}
    public void mouseClicked (MouseEvent e){}
    public void mousePressed (MouseEvent e){}
    public void mouseReleased (MouseEvent e){}
    public void mouseMoved (MouseEvent e){}
    
    public void paint (Graphics g)
    {
        
        g.drawString
        
        //Create Balloon Outline
        g.setColor (Color.black);
        g.drawArc (0 + xoffset, 0 + yoffset, 200, 200, 0, 180);
        g.drawArc (0 + xoffset, -80 + yoffset, 200, 350, 180, 90);
        g.drawArc (0 + xoffset, -80 + yoffset, 200, 350, 270, 90);
        
        //Create Balloon Colour
        g.setColor (Color.blue);
        g.fillArc (0 + xoffset, 0 + yoffset, 200, 200, 0, 180);
        g.fillArc (0 + xoffset, -80 + yoffset, 200, 350, 180, 90);
        g.fillArc (0 + xoffset, -80 + yoffset, 200, 350, 270, 90);
        
        //Continue Outline
        g.setColor (Color.black);
        g.drawArc (60 + xoffset, 0 + yoffset, 80, 200, 0, 180);
        g.drawArc (60 + xoffset, -80 + yoffset, 80, 350, 180, 90);
        g.drawArc (60 + xoffset, -80 + yoffset, 80, 350, 270, 90);
        
        //Continue Balloon Colour
        g.setColor (Color.magenta);
        g.fillArc (60 + xoffset, 0 + yoffset, 80, 200, 0, 180);
        g.fillArc (60 + xoffset, -80 + yoffset, 80, 350, 180, 90);
        g.fillArc (60 + xoffset, -80 + yoffset, 80, 350, 270, 90);
        
        //Add Ropes
        g.setColor (Color.black);
        g.drawLine (20 + xoffset, 200 + yoffset, 50 + xoffset, 320 + yoffset);
        g.drawLine (100 + xoffset, 200 + yoffset, 100 + xoffset, 320 + yoffset);
        g.drawLine (120 + 60 + xoffset, 200 + yoffset, 90 + 60, 320 + yoffset);
        g.drawLine (20 + xoffset, 200 + yoffset, 180 + xoffset, 200 + yoffset);
        
        //Create Basket
        Polygon basket = new Polygon();
        basket.addPoint(20 + xoffset, 320 + yoffset);        // A            A.    ___________     B.
        basket.addPoint(180 + xoffset, 320 + yoffset);        // B                 \          /
        basket.addPoint(150 + xoffset, 380 + yoffset);        // C            D.       \_______/    C.
        basket.addPoint(50 + xoffset, 380 + yoffset);        // D                    Basket =D
        
        //Create Basket Colour
        g.setColor (Color.red);
        g.fillPolygon(basket);
        
        //Add Basket Planks
        g.setColor (Color.black);
        g.drawLine (20 + xoffset, 320 + yoffset, 180 + xoffset, 320 + yoffset);
        g.drawLine (25 + xoffset, 330 + yoffset, 175 + xoffset, 330 + yoffset);
        g.drawLine (30 + xoffset, 340 + yoffset, 170 + xoffset, 340 + yoffset);
        g.drawLine (35 + xoffset, 350 + yoffset, 165 + xoffset, 350 + yoffset);
        g.drawLine (40 + xoffset, 360 + yoffset, 160 + xoffset, 360 + yoffset);
        g.drawLine (45 + xoffset, 370 + yoffset, 155 + xoffset, 370 + yoffset);
        g.drawLine (50 + xoffset, 380 + yoffset, 150 + xoffset, 380 + yoffset);        
    }
}


Hopefully its better to work with that?
Thanks
User is online!Profile CardPM
+Quote Post

pbl
RE: Click And Drag?
11 Sep, 2008 - 06:26 PM
Post #3

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,574



Thanked: 233 times
Dream Kudos: 75
My Contributions
Look at the class MousMotionListenet in the API


User is online!Profile CardPM
+Quote Post

brandon_v
RE: Click And Drag?
11 Sep, 2008 - 07:25 PM
Post #4

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 26


My Contributions
Ok, I've finally got it! (sorta)
Thanks anywho.
User is online!Profile CardPM
+Quote Post

1lacca
RE: Click And Drag?
12 Sep, 2008 - 01:07 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
This tutorial might help as well
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 1/8/09 05:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month