import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class HeapDraw extends JFrame
{
private static final long serialVersionUID = (240)-678-7912;
private static ArrayList<Integer> heap = new ArrayList<Integer>();
private static ArrayList<Node> heapNode = new ArrayList<Node>();
private static int numRows;
private static ArrayList<Double> xCoords;
private static ArrayList<Double> yCoords;
private static JFrame drawing;
private static JPanel panel;
public static void main(String [] args)
{
doNothing();
}
public static void doNothing()
{
readHeap();
drawing = new JFrame("Heap Tree");
for(int i = 0; i < heap.size(); i++)
heapNode.add(new Node(heap.get(i)));
panel = new JPanel();
drawing.setSize(800, 600);
panel.setBackground(Color.WHITE);
drawing.setVisible(true);
drawing.add(panel);
try
{
Thread.sleep(200);
}
catch (InterruptedException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
for(int i = 0; i < heap.size(); i++)
{
panel.add(heapNode.get(i));
try
{
Thread.sleep(200);
panel.updateUI();
drawing.repaint();
}
catch (InterruptedException e)
{
JOptionPane.showMessageDialog(null, "Error: Interrupted", "Error", JOptionPane.ERROR_MESSAGE);
}
}
drawing.repaint();
numRows = getRows(heap);
xCoords = xCoords(heap, numRows, panel.getWidth());
yCoords = yCoords(heap, numRows, panel.getHeight());
System.out.println(heapNode.get(0));
System.out.println(xCoords.get(0));
System.out.println(yCoords.get(0));
moveTo(heapNode.get(0), (int)((double)xCoords.get(0)), (int)((double)yCoords.get(0)));
}
public static void readHeap()
{
try{
Scanner reader = new Scanner(new FileReader("numbers.txt"));
while(reader.hasNextInt())
heap.add(reader.nextInt());
}//end try clause for reading numbers and inserting them into a heap, and building it
catch(FileNotFoundException e)
{
JOptionPane.showMessageDialog(null, "404 Error: \nFile not Found", "404 Internal Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}//end catch clause for when input file is not found
}//end method readHeap
public static void moveTo(Node t, int xCoord, int yCoord)
{
int x = t.getX(), y=t.getY();
double xInt = xCoord>x?xCoord-x:x-xCoord; double yInt = yCoord>y?yCoord-y:y-yCoord;
xInt = xInt/100; yInt = yInt/100;
double newX = t.getX()+xInt, newY = t.getY()+yInt;
System.out.println(xInt);
System.out.println(yInt);
for(int i = 0; i<100; i++)
{
try
{
Thread.sleep(10);
t.setLocation((int)newX, (int)newY);
panel.updateUI();
drawing.repaint();
//System.out.println("X: " + t.getX() + "\nY: " + t.getY());
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
newX = newX+xInt; newY = newY+yInt;
}
System.out.println("here");
System.exit(0);
}
public static int getRows(ArrayList<Integer> a)
{
ArrayList<Integer> ref = new ArrayList<Integer>();
for(int i = 0; i < a.size(); i++)
ref.add((int)((.5*i)*(i+1)));
int i = 0;
while(ref.get(i) < a.size())
i++;
return i-1;
}
public static ArrayList<Double> xCoords(ArrayList<Integer> a, int numRows, int width)
{
ArrayList<Double> xCoords = new ArrayList<Double>();
double i = 2.0;
int power = 1;
while(i<=2*a.size())
{
double j = 1.0;
while(j <= i-1)
{
xCoords.add((j*width)/i);
j++;
}
i = Math.pow(2, power)+1;
power++;
}
return xCoords;
}
public static ArrayList<Double> yCoords(ArrayList<Integer> a, int numRows, int height)
{
ArrayList<Double> yCoords = new ArrayList<Double>();
double interval = height / numRows;
double i = 1;
int power = 0;
int multiplier = 0;
while(i<=2*a.size())
{
double j = 0;
while(j<i)
{
yCoords.add(interval*multiplier);
j++;
}
power++;
i = Math.pow(2, power);
multiplier++;
}
for(int k = 0; k < yCoords.size(); k++)
yCoords.set(k, yCoords.get(k) + 20);
return yCoords;
}
}