Results 1 to 3 of 3
Thread: Making Triangles
- 02-07-2013, 08:20 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
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); } }
- 02-07-2013, 08:46 PM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
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:
You will also need to initialize your coordinates:Java Code:private Random rand = new Random(10);
Java Code:private int[] xPoints = new int[3];
3. As far as I know, you can't do this:
It will give you some errors about how to use constants in the initializer.Java Code:xPoints = {10+x, 10+x, 15+x} yPoints = {10+y, 20+y, 15+y}
You can always set them later:
That should get you heading in a better direction I believe!Java Code:xPoints[0] = 10+x;
- 02-08-2013, 04:41 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Two triangles of stars next to each other
By Aero in forum New To JavaReplies: 3Last Post: 09-26-2011, 07:20 PM -
Printing triangles with * patterns
By jnjh in forum New To JavaReplies: 8Last Post: 06-21-2011, 12:15 PM -
Drawing out triangles
By leiferouis in forum New To JavaReplies: 24Last Post: 01-16-2009, 08:18 PM -
Triangles
By CodeDog in forum New To JavaReplies: 9Last Post: 10-14-2008, 09:18 PM -
asterisks triangles
By Dan121 in forum New To JavaReplies: 1Last Post: 01-12-2008, 07:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks