Results 1 to 3 of 3
  1. #1
    truegamer95 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Exclamation Making Triangles

    I've been trying to fill stars and have random placement for a while now but I've only started doing triangles. This is my code but command prompt gives me an error having to do with my arrays for the x points. I'm not sure what's wrong and my teacher has no idea either. Someone please give me some explanation for this and some suggestions

    Much appreciation.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    
    public class PP4_12
    {
    	public static void main (String[] args)
    	{
    		JFrame frame = new JFrame ("");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		frame.getContentPane().add(new StarPanel());
    		
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    
    class StarPanel extends JPanel
    {
    	private Star s1;
    	
    	public StarPanel()
    	{
    		s1 = new Star();
    		
    		setPreferredSize(new Dimension(600, 400));
    		setBackground(Color.darkGray);
    	}
    	
    	public void paintComponent(Graphics page)
    	{
    		super.paintComponent(page);
    		
    		s1.draw(page);
    	}
    }
    
    class Star
    {
    	private Polygon p1, p2, p3, p4 ,p5, p6;
    	private Random rand;
    	private int x;
    	private int y;
    	private int[] xPoints;
    	private int[] yPoints;
    	
    	public void draw(Graphics page)
    	{	
    		page.setColor(Color.yellow);
    		
    		p1 = new Polygon();
    		
    		x = rand.nextInt(10)
    		x = x*10;
    		y = x;
    		xPoints = {10+x, 10+x, 15+x}
    		yPoints = {10+y, 20+y, 15+y}
    		page.fillPolygon(xPoints, yPoints, 5);
    	}
    }

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Making Triangles

    To get all that code working, you'll have to fix a few things.

    1. Fix all the missing semicolons. In the end of your code they seem to be missing every second line or something.
    2. You will get NullpointerException's if you don't initialize your variables. Fix this!

    Tip:

    Java Code:
    	private Random rand = new Random(10);
    You will also need to initialize your coordinates:

    Java Code:
    	private int[] xPoints = new int[3];

    3. As far as I know, you can't do this:
    Java Code:
            xPoints = {10+x, 10+x, 15+x}
            yPoints = {10+y, 20+y, 15+y}
    It will give you some errors about how to use constants in the initializer.

    You can always set them later:
    Java Code:
    		xPoints[0] = 10+x;
    That should get you heading in a better direction I believe!

  3. #3
    truegamer95 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Smile Re: Making Triangles

    Quote Originally Posted by Zyril View Post
    To get all that code working, you'll have to fix a few things.

    1. Fix all the missing semicolons. In the end of your code they seem to be missing every second line or something.
    2. You will get NullpointerException's if you don't initialize your variables. Fix this!

    Tip:

    Java Code:
    	private Random rand = new Random(10);
    You will also need to initialize your coordinates:

    Java Code:
    	private int[] xPoints = new int[3];

    3. As far as I know, you can't do this:
    Java Code:
            xPoints = {10+x, 10+x, 15+x}
            yPoints = {10+y, 20+y, 15+y}
    It will give you some errors about how to use constants in the initializer.

    You can always set them later:
    Java Code:
    		xPoints[0] = 10+x;
    That should get you heading in a better direction I believe!


    Thanks, I ended up fixing it by mostly scrapping what I did. I did use a couple things from your reply though; it's working now

Similar Threads

  1. Two triangles of stars next to each other
    By Aero in forum New To Java
    Replies: 3
    Last Post: 09-26-2011, 07:20 PM
  2. Printing triangles with * patterns
    By jnjh in forum New To Java
    Replies: 8
    Last Post: 06-21-2011, 12:15 PM
  3. Drawing out triangles
    By leiferouis in forum New To Java
    Replies: 24
    Last Post: 01-16-2009, 08:18 PM
  4. Triangles
    By CodeDog in forum New To Java
    Replies: 9
    Last Post: 10-14-2008, 09:18 PM
  5. asterisks triangles
    By Dan121 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM

Tags for this Thread

Posting Permissions

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