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: Writing a program that calculates midpoints based on randomly picking vertices

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing a program that calculates midpoints based on randomly picking vertices

    What I want to do is start with three points that make an equilateral triangle. My code should pick two of the three points at random, calculate a midpoint (m), and plot it. Then from the midpoint m it generated, the code will pick another one of the three original points at random and compute a new midpoint (m2). The last step should be repeated 10,000 times. I'm just starting out with Java and am really lost and confused at this point. This is the code I have so far (please feel free to point out any mistakes I made in the code I have!):


    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Graphics;
    import java.awt.Dimension;

    public class Game
    {
    static final int HEIGHT = 500;
    static final int WIDTH = 500;

    public static void main(String[] args)
    {
    JFrame frame = new JFrame("The Game");
    Board board = new Board(WIDTH, HEIGHT);
    Point p1 = new Point(0,0);
    Point p2 = new Point(500, 0);
    Point p3 = new Point(0, 250);

    frame.setSize(WIDTH, HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setContentPane(board);

    board.addPoint(p1);
    board.addPoint(p2);
    board.addPoint(p3);

    frame.pack();
    frame.setVisible(true);

    }
    }

    class Board extends JPanel
    {
    public Board(int h, int w)
    {
    setPreferredSize(new Dimension(w, h));
    }

    public void addPoint(Point p)
    {
    ArrayList <Point> point = new ArrayList();
    points.add(p);
    }


    public void paint(Graphics g)
    {
    super.paint(g);

    int i = 0;

    while (i < 10000)
    {
    board.add();
    i++;
    }

    }
    }

    class Point
    {
    int x;
    int y;

    public Point(int x, int y)
    {
    x = this.x;
    y = this.y;
    }

    private static Point midPoint(Point p1, Point p2)
    {
    return new Point((p1.x + p2.x)/2, (p1.y + p2.y)/2);
    }

    public double getX()
    {
    return x;
    }

    public double getY()
    {
    return y;
    }

    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Writing a program that calculates midpoints based on randomly picking vertices

    In the future please wrap your code in the code tags to make it easier to read.
    What part are you stuck on exactly? What are you not able to figure out?

  3. #3
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Writing a program that calculates midpoints based on randomly picking vertices

    I imagine you are trying to plot a Sierpinski Triangle Fractal

    public void paint(Graphics g)
    {
    super.paint(g);
     
    int i = 0;
     
    while (i < 10000)
    {
    board.add();
    i++;
    }

    add what?

    public Point(int x, int y)
    {
    x = this.x;
    y = this.y;
    }
     
    // It should be the other way around
     
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }
     
    class Point {
    	int x;
    	int y;
     
    	public Point(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
     
    	// Would need to be public, and no need to make it static, also you can drop the second
    	// Point for a argument as you can use your currentPosition point as the this.x this.y . 
     
    	public Point midPoint(Point p1) {
    		return new Point((p1.x + x) / 2, (p1.y + y) / 2);
    	}
    // Return ints for the drawRect method
    	public int getX() {
    		return x;
    	}
     
    	public int getY() {
    		return y;
    	}
     
    }
    Read this to see why you should use paintComponent
    A Closer Look at the Paint Mechanism (The Java™ Tutorials > Creating a GUI With JFC/Swing > Performing Custom Painting)

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
     
    class Board extends JPanel {
    	Point currentPosition, temp;
    	Point topVertex, bottomLeftVertex, bottomRightVertex;
    	int random;
     
    	public Board(int h, int w) {
    		// setPreferredSize(new Dimension(w, h));
    		setSize(w, h);
     
    		// Math.random() returns a double between 0 and 1, cast it as an int
                    // Needs to be a pixel location within your JPanel hence * w and * h. Add 1
                    // It always rounds down when casting to an int, so need to add 1
    		currentPosition = new Point((int) (Math.random() * w) + 1,
    				(int) (Math.random() * h) + 1);
     
    		// set the Vertex Points
    		topVertex = new Point(w / 2, 0);
    		bottomLeftVertex = new Point(0, h);
    		bottomRightVertex = new Point(w, h);
     
     
    	}
     
    	public void paintComponent(Graphics g){
    		super.paintComponents(g);
     
     
    		for (int i = 0; i < 100000; i++){
    			// For each iteration get a random number 1>= and >=3
    			random = (int) (Math.random() * 3 + 1);
     
                            // Random assignment of Vertex points to the random number generated
    			if (random == 1){
                                    // Cause I can
    				g.setColor(Color.BLACK);
                                    // Get the Point to be Plotted, Notice the currentPosition is calling the method
    				temp = currentPosition.midPoint(topVertex);
                                    // To act as a point
    				g.fillRect(temp.getX(), temp.getY(), 1, 1);
                                    // Make sure to reassign the currentPosition
    				currentPosition = temp;
    			}
     
    			if (random == 2){
    				g.setColor(Color.RED);
    				temp = currentPosition.midPoint(bottomLeftVertex);
    				g.fillRect(temp.getX(), temp.getY(), 1, 1);
    				currentPosition = temp;
    			}
     
    			if (random == 3){
    				g.setColor(Color.GREEN);
    				temp = currentPosition.midPoint(bottomRightVertex);
    				g.fillRect(temp.getX(), temp.getY(), 1, 1);
    				currentPosition = temp;
    			}
    		}
     
    	}
     
    	/*
    	 * public void addPoint(Point p) { ArrayList<Point> point = new ArrayList();
    	 * points.add(p); }
    	 * 
    	 * public void paint(Graphics g) { super.paint(g);
    	 * 
    	 * //int i = 0;
    	 * 
    	 * /* while (i < 10000) { // ?? board.add(); i++; }
    	 * 
    	 * }
    	 */
    }

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    32
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Writing a program that calculates midpoints based on randomly picking vertices

    Thanks codehelppl0x,
    Very nice explain!

Similar Threads

  1. Find a java program that calculates
    By Zuhairi Abdullah in forum Object Oriented Programming
    Replies: 3
    Last Post: May 9th, 2013, 11:08 AM
  2. Program Shutting Down Randomly?
    By Tyluur in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 9th, 2013, 11:18 AM
  3. buttons randomly show up in program
    By lanepulcini in forum AWT / Java Swing
    Replies: 1
    Last Post: February 18th, 2012, 07:35 PM
  4. Making a program that calculates how many days you lived.
    By shifat96 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 10th, 2012, 12:34 AM
  5. Help with JFrame program that calculates average
    By ePerKar3 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2011, 08:48 AM

Tags for this Thread