Hi, I am a first Year Computer Science student learning java. First, what I want help with is not a homework assignment, I just started it on my own. This program is supposed to test the "Chaos Game" The chaos game is explained here: Chaos Game
So, the basics that I know I need to do are to find get a random point inside the triangle, connect it to a random vertex, find the midpoint, and then color in that dot. Some problems I had were filling in a single space, and for some reason, the line that I use instead of a dot is not being put inside the triangle. Here is my code.
/**
* @(#)FractalStart.java
*
*
* @author
* @version 1.00 2007/11/15
*/
import javax.swing.JFrame;
public class FractalStart
{
public static void main(String[] args)
{
JFrame frame = new JFrame ("Fractal");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TriangleButton panel = new TriangleButton();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
//Draws some triangles,
//Does some Fractal stuff with it.
//11-02-07
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.ArrayList;
public class Fractal extends JPanel
{
private int[] triangle01 = {150, 300, 150};
private int[] triangle02 = {300, 300, 150};
Random gen = new Random();
int point1 = gen.nextInt(300);
int point2 = gen.nextInt(300);
int side = 3;
int point3;
int point4;
int midPoint1;
int midPoint2;
int point01;
int point02;
int[] dot1;
int[] dot2;
ArrayList<Integer> dots = new ArrayList<Integer>(); //holds all the points that are made in this program
Polygon triangle = new Polygon(triangle01, triangle02, triangle01.length);
public Fractal()
{
addKeyListener (new DirectionListener());
setFocusable(true);
setBackground (Color.black);
setPreferredSize (new Dimension (1000,1000));
}
public void paintComponent (Graphics page)
{
//This should be drawing each part dot through the loop
super.paintComponent (page);
page.setColor(Color.black);
for (int count = 0; count < dots.size(); count++)
{
int Npoint = dots.get(count);
count++;
int Npoint1 = dots.get(count);
int[] joe = {Npoint, Npoint1};
int[] joe1 = {Npoint + 1, Npoint1 + 1};
page.setColor(Color.red);
page.drawPolyline(joe,joe1,joe.length);
}
page.setColor(Color.white);
page.drawPolygon(triangle);
}
public void fractal1()
{
//Makes two random points inside the triangle
//and then find the midpoint between that and a random side
do
{
point1 = gen.nextInt(500); //point inside triangle
point2 = gen.nextInt(500);
side = gen.nextInt(2);
}while (triangle.contains(point1,point2));
point3 = triangle01[side]; //x point
point4 = triangle02[side]; //y point of veritice
Integer midPoint1 = (point1 + point3) / 2;
Integer midPoint2 = (point2 + point4) / 2;
dots.add(midPoint1); //adds the points to the dot.
dots.add(midPoint2);
}
private class DirectionListener implements KeyListener
{
public void keyPressed (KeyEvent event)
{
switch (event.getKeyCode())
{
case KeyEvent.VK_DOWN:
fractal1();
break;
}
repaint();
}
public void keyTyped (KeyEvent event) {}
public void keyReleased (KeyEvent event) {}
}
}
The problem is that the dots aren't being put inside the triangle, only on the right line of it.... Why is this? What's a good solution? I've been debugging for a while and it still escapes me what is going on.
Thank you for your help! Oh, and tell me anything that might make it better, but simple, cause this is my first year.