Jump to content

My Christmas Program(Unfinished) [Java]


Pasha

Recommended Posts

Hello everyone!, This is my first post and I just wanted to share this little code that I made in time for Christmas :)

It's currently unfinished as you can see if you run it, the "ornaments" are all over the screen. Not concentrated on the tree

I was thinking about using Pixel Grabber to move and regulate the "ornaments" :)

Well, Here you go :) (by the way, I'm still learning Java...so its not like a perfect program)


// Made By: Pasha Kravtsov (c) 2010
// nVidia_Freak,Darius,Flibo,bowtie 
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.util.List;
import java.util.Iterator;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ChristmasTree extends Applet implements MouseListener, Runnable {
int width = 500,height = 500;
BufferedImage bufferImg;
Graphics bufferG;
BufferedImage tree;
Thread t,t1;
JFrame F;
MyCanvas C;
Random r = new Random();
List<Lights> lights;
public ChristmasTree() {		
	F = new JFrame("Christmas Tree ");
	F.setSize(width,height);
	C = new MyCanvas();
	C.setSize(width,height);
	C.setVisible(true);
	C.addMouseListener(this);
	F.add(C);
	F.setVisible(true);
	F.setResizable(false);
	F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      	 	String path = "images/tree.jpg";
      	 	File file = new File(path);
      	 	 try {
               	tree = ImageIO.read(file);
           	}catch(IOException e) { e.printStackTrace(); }
      	 	bufferImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
      	 	bufferG = bufferImg.getGraphics();
      	 	lights = new ArrayList<Lights>(10);
      	 	for (int i = 0; i <= 10; i++) {
      	 		lights.add(new Lights(r.nextInt(width),r.nextInt(height)));
      	 	}
      	 	t = new Thread(this);
      	 	t.start();
}
public static void main() {
	ChristmasTree T = new ChristmasTree();
} 

public void run() {
	t1 = Thread.currentThread();
   		while(t1 == t){
    			updateSpace();
   			try{
    				t1.sleep(200);  // SLEEP FOR 150 MILLISECONDS
   			}catch(InterruptedException e){ e.printStackTrace(); }
  		}	
}

public void updateSpace() {
	bufferG.clearRect(0,0,width,height);
	bufferG.drawImage(tree,0, 0,width,height,this);
	for (Iterator<Lights> iter = lights.iterator(); iter.hasNext() {
	   Lights L = iter.next(); 
	   L.paint(bufferG);
	}
	bufferG.setColor(Color.black);
	bufferG.drawString("Made by Pasha!!! :x",15,15);
	C.repaint();
}

public void mouseExited(MouseEvent me) { }
public void mouseEntered(MouseEvent me) { }
public void mouseReleased(MouseEvent me) { }
public void mousePressed(MouseEvent me) { 
   lights.add(new Lights(me.getX(),me.getY()));
   }
public void mouseClicked(MouseEvent me) { 

}
private class Lights {
	Color[] ornaments = { Color.red, Color.green, Color.blue, Color.yellow };
	Color lightColor;	
	Random r = new Random();
	int x,y;
	int width = 6,height = 6;	

	public Lights(int x,int y) {
		this.x=x;
		this.y=y;
		lightColor = ornaments[r.nextInt(ornaments.length)];
	}
	public void paint(Graphics g) {
	    lightColor = ornaments[r.nextInt(ornaments.length)];
	    g.setColor(lightColor);
		g.fillOval(x,y,width,height);
	}
}

private class MyCanvas extends Canvas {
	public MyCanvas() { super(); }
	public void paint(Graphics g) { g.drawImage(bufferImg,0,0,this); }
}
}

Share this post


Link to post
Share on other sites

As far as i know i dont have a java compiler. I just copied the code into notepad++(my XHTML/CSS/JS editor) and saved as .java and assumed it would run. I am toatly clueless about java.

 

hahaha its ok (: i would recommend the software "BlueJ" as its very easy to use... its free too when you've downloaded that goto file -> new project then look for new/make class then double click it paste code then compile it and then right click on the class and do "Run Applet"

Share this post


Link to post
Share on other sites

Okay i did that and i get this error.

java.lang.reflect.InvocationTargetException

at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)

at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)

at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)

at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)

at java.security.AccessControlContext.checkPermission(Unknown Source)

at java.security.AccessController.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkExit(Unknown Source)

at javax.swing.JFrame.setDefaultCloseOperation(Unknown Source)

at ChristmasTree.<init>(ChristmasTree.java:36)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at java.lang.Class.newInstance0(Unknown Source)

at java.lang.Class.newInstance(Unknown Source)

at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Exception: java.lang.reflect.InvocationTargetException

Thats running it in firefox.

Share this post


Link to post
Share on other sites

Okay i did that and i get this error.

java.lang.reflect.InvocationTargetException

at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)

at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)

at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)

at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission exitVM.0)

at java.security.AccessControlContext.checkPermission(Unknown Source)

at java.security.AccessController.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkExit(Unknown Source)

at javax.swing.JFrame.setDefaultCloseOperation(Unknown Source)

at ChristmasTree.<init>(ChristmasTree.java:36)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at java.lang.Class.newInstance0(Unknown Source)

at java.lang.Class.newInstance(Unknown Source)

at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)

at java.awt.event.InvocationEvent.dispatch(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

Exception: java.lang.reflect.InvocationTargetException

Thats running it in firefox.

 

i'm not sure why your running it in FireFox

cause when i run it it runs in AppletViewer....

lol i have no idea why it does that 'cause it works perfectly for me

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...