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 4 of 4

Thread: Make a polygon appear in random places using Japplet and Canvas

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Make a polygon appear in random places using Japplet and Canvas

    I can make the circles appear in random places, but need a little direction to make the polygon appear in random places.

    import java.awt.*;
     
    public class Circle
    {
        private int number;
        private int diameter;
        private Color colour;
     
        public Circle(int num)
        {
            number = num;
            diameter = randomInt(20, 50);
            colour = new Color(randomInt(0, 255),randomInt(0, 255),randomInt(0, 255));
        }
     
        public int getDiameter()
        {
            return diameter;
        }
     
        private int randomInt(int min, int max)
        {
            return (int)(Math.random() * (max - min + 1) + min);
        }
     
        public void display(Graphics g, int x, int y)
        {
            int radius = diameter / 2;
     
            //THE POLYGON IS SET IN ONE PLACE
            int[] polyx = {6, 59, 84, 126, 151, 194, 194, 6};
            int[] polyy = {47, 47, 27, 27, 47, 47, 77, 77};
            g.setColor(colour);
            g.fillPolygon(polyx, polyy, 8);
     
            //THE CIRCLES ARE DRAWN IN RANDOM PLACES
            g.setColor(colour);
            g.fillOval(x, y, diameter, diameter);
            g.setColor(Color.BLACK);
            g.drawOval(x, y, diameter, diameter);
            g.drawString("" + number,  x + radius, y + radius);
        }
    }
     {
     }


  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: Make a polygon appear in random places using Japplet and Canvas

    Do you want the shape to stay the same and only the location to move?
    Chose a new x,y location and add the new values to all the x and y values that define the polygon.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Location
    Shrewsbury, UK
    Posts
    12
    My Mood
    Cool
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Make a polygon appear in random places using Japplet and Canvas

    the polygon is a car shape, and I've got to make 10 cars appear randomly in the applet. So everytime the Applet is run they all appear in different places.

  4. #4
    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: Make a polygon appear in random places using Japplet and Canvas

    Have you tried what I suggested? What happened?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Polygon Clipping Java, Help Please.
    By wtosh in forum What's Wrong With My Code?
    Replies: 50
    Last Post: January 16th, 2013, 09:29 PM
  2. Trying to make 3 images become random in an Applet. Need help
    By slahsdash in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 9th, 2012, 09:39 PM
  3. Creating Polygon (not visual)
    By teen-omar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 11th, 2010, 03:28 PM
  4. make textbox in canvas
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: October 6th, 2009, 07:10 AM