Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How can I make the shapes move at the same time when the user enters "up" it should move up. It works for rectangle but i don't know how to move the others like that. (btw i am new to java).

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can I make the shapes move at the same time when the user enters "up" it should move up. It works for rectangle but i don't know how to move the others like that. (btw i am new to java).

    I want to move the shapes at the same time. How can i do that? Any ideas or suggestions? Thanks.

    __________________________________________________ __
    import javax.swing.JFrame;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import javax.swing.JComponent;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Line2D;
    import java.awt.Color;
    import java.awt.Polygon;
    import java.util.Scanner;

    public class swingBot
    {
    public static void main(String[] args) {

    // contruction of new JFrame object
    JFrame frame = new JFrame();

    // mutators
    frame.setSize(400,400);
    frame.setTitle("SwingBot");

    // program ends when window closes
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    Robot r = new Robot();

    frame.add(r);

    // voila!
    frame.setVisible(true);

    // your Scanner-based command loop goes here
    Scanner in = new Scanner(System.in);
    System.out.println(" Enter up, down, left or right to move the rectangle: ");
    String w = in.nextLine();
    // your Scanner-based command loop goes here

    while (true){
    if ("down".equalsIgnoreCase(w)){
    r.moveBot(0,10);
    }
    else if ("up".equalsIgnoreCase(w)){
    r.moveBot(0,-10);
    }
    else if ("left".equalsIgnoreCase(w)){
    r.moveBot(-10,0);
    }
    else if ("right".equalsIgnoreCase(w)){
    r.moveBot(10,0);
    }
    // call methods on the Robot instance like w.moveBot(10,10) in response to
    // user input
    System.out.println(" Enter up, down, left or right to move the rectangle: ");
    w = in.nextLine();
    }

    }
    public static class Robot extends JComponent
    {
    private Rectangle rect = new Rectangle(20,20);
    private Ellipse2D.Double oval = new Ellipse2D.Double(40,40,100,150);
    private Line2D.Double line = new Line2D.Double(30,120,150,120);

    public void paintComponent(Graphics g)
    {
    Graphics2D g2 = (Graphics2D) g;

    // set the color
    g2.setColor(Color.RED);

    // draw the shape`
    g2.fill(rect);

    g2.draw(oval);
    g2.fill(oval);
    g2.draw(line);
    g2.setColor(Color.GREEN);

    int[] x = new int[]{10,50,130,170};
    int[] y = new int[]{200,30,30,200};
    g.drawPolygon (x, y, x.length);



    }

    public void moveBot(int x, int y)
    {
    // move the rectangle
    rect.translate(x,y);

    // redraw the window
    repaint();

    }

    }

    }
    Last edited by judyfinny; October 1st, 2014 at 09:45 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I make the shapes move at the same time when the user enters "up" it should move up. It works for rectangle but i don't know how to move the others like that. (btw i am new to java).

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    move the shapes at the same time.
    Make all the moves in the paintComponent() method (at the same time).
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How can I make image to move on java for android aplication?
    By Mikael in forum Android Development
    Replies: 5
    Last Post: May 6th, 2014, 05:08 PM
  2. [SOLVED] How to get the arrow keys to move rectangle automatically
    By Edmacelroy in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 4th, 2013, 10:11 PM
  3. Having difficult to move("focus") one sprite frame to another. Please help.
    By marksquall in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 14th, 2013, 10:53 AM
  4. [SOLVED] is there any good formula's for 2d java game - to make ur player move right or left?
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 23rd, 2012, 01:42 AM
  5. java maze move charcter?
    By coolcool1980 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 10th, 2011, 02:18 PM

Tags for this Thread