Run the code and see what it does.
```java
package ctree;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class CTree extends JFrame implements ActionListener{
/**
* @param args the command line arguments
*/
TPanel panel = new TPanel();
CTree()
{
add(panel);
this.setSize(640,480);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
Timer timer = new Timer(500, this);
this.setVisible(true);
timer.start();
}
public static void main(String[] args) {
// TODO code application logic here
new CTree();
}
@Override
public void actionPerformed(ActionEvent ae) {
//To change body of generated methods, choose Tools | Templates.
panel.changeColor();
panel.repaint();
}
}
```
```java
package ctree;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import javax.swing.JPanel;
public class TPanel extends JPanel{
boolean isBlue = true;
public void changeColor()
{
isBlue = !isBlue;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int [] x1 = {200,100,300};
int [] y1 = {100,200,200};
int [] x2 = {200,100,300};
int [] y2 = {200,300,300};
Polygon triangle1 = new Polygon(x1,y1,3);
Polygon triangle2 = new Polygon(x2,y2,3);
g2.setColor(Color.GREEN);
g2.fillPolygon(triangle1);
g2.fillPolygon(triangle2);
if(isBlue)
{
g2.setColor(Color.BLUE);
}
else
{
g2.setColor(Color.RED);
}
g2.fillOval(90, 200, 20, 20);
g2.fillOval(290, 200, 20, 20);
g2.fillOval(90, 300, 20, 20);
g2.fillOval(290, 300, 20, 20);
}
}
```
## Challenge
Improve the Christmas tree.
You will be awarded a level for your improvements as follows
1 Add a trunk
2 Add a star at the top
3 Make the lights flash at random
4 Make a three level Christmas tree
5 Use loops, rather than hard coding to make a three level tree
6 Use loops Make the tree narrow the higher up you go
7 Use loops to make an n level tree where 1 < n < 7
8 Add your own touch
This level will be your grade for this term's work.
As this is Christmas, the highest level tree wins a Mars Bar or equivalent.
In the event of a tie, the tree will be judged on aesthetic merit. The judge will be Mr Mkandawire