summaryrefslogtreecommitdiff
path: root/labs/lab07_tohSolver/THComponent.java
blob: 3a61dc5ca6308650cb0c8960c9a189d9d89f44c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;

import javax.swing.JComponent;

@SuppressWarnings("serial")
public class THComponent extends JComponent
{
	private int width;
	private int height;
	private int leftMargin = 10;
	private int rightMargin = 10;
	private int topMargin = 10;
	private int bottomMargin = 10;
	private int baseHeight = 10;
	private double poleWidth = 10;
	private TowerOfHanoi towers;
	private int numberOfDiscs;
	private double largestDiscSize;
	private double smallestDiscSize = 20;
	private double defaultReductionSize = 20.0;
	private double defaultDiscHeight = 20.0;
	private double[] discSizes;
	
	public THComponent(TowerOfHanoi aTowers)
	{
		towers = aTowers;
		numberOfDiscs = towers.getNumberOfDiscs();
		discSizes = new double[numberOfDiscs];
	}
	
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		
		width = this.getWidth();
		height = this.getHeight();

		int drawingWidth = width - (leftMargin + rightMargin);
		int drawingHeight = height - (topMargin + bottomMargin);

		// draw the base;
		
		Rectangle2D.Double rect = new Rectangle2D.Double(leftMargin , height - (bottomMargin + baseHeight), drawingWidth, baseHeight);
		g2.draw(rect);
		
		// draw poles;
		
		double halfDistance = ((double) drawingWidth / 6);
		largestDiscSize = (halfDistance * 2) - poleWidth;
		
		for(int i = 1; i <= 5; i = i + 2)
		{
			rect.setRect((leftMargin + (halfDistance * i)) - (poleWidth / 2), topMargin, poleWidth, drawingHeight - baseHeight);
			g2.draw(rect);
		}
		
		// draw discs
		
		double totalReduction = (largestDiscSize - smallestDiscSize) / (numberOfDiscs - 1);
		double actualReduction;
		
		if(totalReduction > defaultReductionSize)
		{
			actualReduction = defaultReductionSize;
		}
		else
		{
			actualReduction = totalReduction;
		}
		
		double totalHeight = drawingHeight - baseHeight;
		double tempDiscHeight = totalHeight / (numberOfDiscs + 1);
		double actualDiscHeight;
		
		if(tempDiscHeight > defaultDiscHeight)
		{
			actualDiscHeight = defaultDiscHeight;
		}
		else
		{
			actualDiscHeight = tempDiscHeight;
		}
		
		for(int i = 0; i < numberOfDiscs; i++)
		{
			discSizes[i] = largestDiscSize - (actualReduction * ((numberOfDiscs - 1) - i));
		}
		
		int[][] t = new int[3][];
		for(int i = 0; i < 3; i++)
		{
			t[i] = towers.getArrayOfDiscs(i);
		}
			
		for(int i = 0; i <= 2; i++)
		{
			for(int j = 0; j < t[i].length; j++)
			{
				double x = (leftMargin + (halfDistance * (1 + (2 * i)))) - (discSizes[t[i][j]] / 2);
				double y = ((topMargin + drawingHeight) - baseHeight) - (actualDiscHeight * (j + 1));
				rect.setRect(x,y,discSizes[t[i][j]],actualDiscHeight);
				g2.setColor(Color.GREEN);
				g2.fill(rect);
				g2.setColor(Color.BLACK);
				g2.draw(rect);
			}
		}
	}
}