Pixel Xmas Tree – Example 10

This is post will be my last example code I shared this year, I must admit I have had allot of fun learning #Java by examples and by trial and error and looking forward to continue next year but not before I share my last bit of code creating a xmas tree pixel by pixel until a full pixel tree is formed with it’s own little lights, it also loops 4 time changing the color of the lights on the tree.

Here is a code snip of the entire code availble below.


@Override
public void paint(Graphics g) {
super.paint(g);

for (int count = 4; count > 0; count -= 1)
{
for(int y = 0; y < HEIGHT; y++) { //TOP DOWN for(int x = 0; x < WIDTH; x++) { //LEFT TO RIGH if (x >= 197 && x < = 203 && y >= 1 && y < =5){ g.setColor(Color.GREEN); } else if (x >= 195 && x < = 205 && y >= 5 && y < =10){ g.setColor(Color.GREEN); } else if (x >= 193 && x < = 207 && y >= 10 && y < =15){ if (x >= 196 && x < = 201 && y >= 10 && y < =15){ g.setColor(Color.RED); } else { g.setColor(Color.GREEN); }

This is what you will have when fully compiled.

lesson10

If you want to download the code and change it, improve it then please share your new code here with us.

Full code available here: pixeltree

Again if you make improvements, share with it with me I would love to see it.

One comment:

Leave a Reply