Results 1 to 10 of 10
  1. #1
    Need Help is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default need your help please

    Assignment for my gen ed college course. Below is my code for the project so far. My problem is that the leftAction, rightAction, and midAction only move the ovals. I need them to move the ovals and have the lines come with them.

    Any help?




    public class Driver extends ThreeButtons
    {
    // instance variables - replace the example below with your own
    private ThreeButtonFrame window;
    private Oval topV, rightV, leftV;
    private Line leftS, rightS, bottomS;


    /**
    * Constructor for objects of class Driver
    */
    public Driver()
    {
    // initialise instance variables
    ThreeButtonFrame theWin;
    theWin = new ThreeButtonFrame("Assignment 6");
    theWin.setLayout(null);
    theWin.setVisible(true);

    topV = new Oval(295,295,10,10);
    topV.setBackground(Color.blue);
    theWin.add(topV, 0);
    theWin.repaint();

    rightV = new Oval(345,370,10,10);
    rightV.setBackground(Color.blue);
    theWin.add(rightV, 0);
    theWin.repaint();

    leftV = new Oval(245,370,10,10);
    leftV.setBackground(Color.blue);
    theWin.add(leftV, 0);
    theWin.repaint();

    leftS = new Line(250,375,300,300);
    leftS.setBackground(Color.blue);
    theWin.add(leftS,0);
    theWin.repaint();

    rightS = new Line(350,375,300,300);
    rightS.setBackground(Color.blue);
    theWin.add(rightS,0);
    theWin.repaint();

    bottomS = new Line(250,375,350,375);
    bottomS.setBackground(Color.blue);
    theWin.add(bottomS,0);
    theWin.repaint();



    }

    public void leftAction() {
    int newX, newY;
    newX = leftV.getX() - 5;
    newY = leftV.getY();
    leftV.setLocation(newX, newY);
    leftV.repaint();
    }

    public void midAction() {
    int newX, newY;
    newX = topV.getX();
    newY = topV.getY() - 5;
    topV.setLocation(newX, newY);
    topV.repaint();

    }

    public void rightAction() {
    int newX, newY;
    newX = rightV.getX() + 5;
    newY = rightV.getY();
    rightV.setLocation(newX, newY);
    rightV.repaint();


    }





    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    in the xxxAction methods you are only changing the position of the oval and not the lines. There appear to be other errors unrelated to this in your code. For instance, you named the frame window but refer to it as theWin.

  3. #3
    Need Help is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    yes I am aware of the other issues. I can change those. Do you have any suggestions on how to change my code so that the lines move with the ovals?
    Thanks for any help.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Its fairly simple. You create the newX and newY local variables in the xxxAction methods. Use them to set the location of the lines as you did with the ovals

  5. #5
    Need Help is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    is there a way to do it that would make all of the ovals and lines into a solid triangle? And then, by moving the ovals, all the lines are adjusted accordingly?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Probably, it would take some thinking on your part though.

  7. #7
    Need Help is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    Been at it for hours and I am no further. I tried to combine them into a triangle - didn't come close at all. I'm trying now to extend the lines with each click but am not sure where to plug the newX and newY into since a line uses 2 points for its start and end.

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    leftV.setLocation(newX, newY);

    similarly, you can get the current position of the line, relative to the oval, and then set the position of the line like this:

    Java Code:
    leftS.setLocation(newX - difX, newY - difY);

    where difX and difY are the X and Y distances to keep the lines from the oval

  9. #9
    Need Help is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default

    How would I do it though if I wanted to keep the lines attached to the ovals as they move?

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    you set the position of the lines relative to the ovals...

    look at my previous post and put in fake values to see how it would work out:

    Java Code:
    //move the oval to 100,120
    leftV.setLocation(100,120)
    
    //move line with oval
    difX = 10 //keep 10 pixels horizontal away from oval
    difY = 12 //keep 12 pixels vertical away from oval
    leftS.setLocation(100 - 10, 120 - 12)

    so everytime you move the oval, update the position of the lines too

    try it with other values to see what happens

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •