Best Projects
Bug Trackingsystem,Books online shopping,college management system,HR management system,Internet banking,Online chat room ,Online exam,Telephone billing system,Banking application,ATM database,airways reservation system,virtual network computing ,calculator,SMTP(simple mail transfer protocol)
Archives
-
▼
2008
(101)
-
▼
July
(67)
- Adapter_Demo__Mini
- Applet_Add__Mini
- Applet_Frame__Mini
- Applet_Window__Mini
- Banner__Mini
- Browser__Mini
- Choice_List__Mini
- Clint_Test__Mini
- Color_Demo__Mini
- Double_Buffer__Mini
- First_Applet__Mini
- Grid__Mini
- HServlet__Mini
- Image_Load__Mini
- Image_Swing__Mini
- Inetaddress_Demo1__Mini
- InnerClassdemo__Mini
- Java_Buttons__Mini
- Java_Label__Mini
- Jdbc_test1__Mini
- Jdbc update__Mini
- Jlabeldemo__Mini
- JScrollPaneDemo_Mini
- Key_Events_Mini
- Lawte_Mini
- List_Demo_Mini
- Logo_Mini
- Observed_ImageLoad_Mini
- Rectangles_Mini
- Resize_Me_Mini
- Sample_Applet_Mini
- Sb_Demo_Mini
- Server_Test_Mini
- Servlet_Database_Mini
- servlet_To_Applet_Mini
- Simple_Key_Mini
- Text_Demo_Mini
- Textarea_Demo_Mini
- UDP_Server_Mini
- UDPClient_Mini
- Window_Events_Mini
- Advertising Agency
- Books Online Shopping
- BugTracking System
- College Management Sysyetm
- Doctors
- Human Resource Management System
- Internet Banking
- Intranet Chatting
- Java_Mail_Filter
- MIS Coding Nokia Soft
- Online Cources
- Online Exam
- Power Point Sliding
- Practical_Aircraft_Position
- project planning management
- Puyo Puyo game
- Sliding Window Protocol
- Telephone Billing system
- Voice over internet protocol
- No title
- Banking Application
- ATM Database
- airways reservation system
- Chat Room Application
- Snake(applet)
- Poor Man's PaintShop
-
▼
July
(67)
Snake(applet)
//applet Frame
import java.awt.Frame;
import java.awt.Event;
import java.awt.Dimension;
import java.applet.Applet;
// Applet to Application Frame window
class AppletFrame extends Frame
{
public static void startApplet(String className,
String title,
String args[])
{
// local variables
Applet a;
Dimension appletSize;
try
{
// create an instance of your applet class
a = (Applet) Class.forName(className).newInstance();
}
catch (ClassNotFoundException e) { return; }
catch (InstantiationException e) { return; }
catch (IllegalAccessException e) { return; }
// initialize the applet
a.init();
a.start();
// create new application frame window
AppletFrame f = new AppletFrame(title);
// add applet to frame window
f.add("Center", a);
// resize frame window to fit applet
// assumes that the applet sets its own size
// otherwise, you should set a specific size here.
appletSize = a.size();
f.pack();
f.resize(440,380);
// show the window
f.show();
} // end startApplet()
// constructor needed to pass window title to class Frame
public AppletFrame(String name)
{
// call java.awt.Frame(String) constructor
super(name);
}
// needed to allow window close
public boolean handleEvent(Event e)
{
// Window Destroy event
if (e.id == Event.WINDOW_DESTROY)
{
// exit the program
System.exit(0);
return true;
}
// it's good form to let the super class look at any
// unhandled events
return super.handleEvent(e);
} // end handleEvent()
} // end class AppletFrame
//snake
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import java.awt.Image.*;
import javax.swing.*;
public class Snake extends Applet implements ActionListener
{
int SnakeWidth = 10;
int SnakeLength = 10;
int SnakeSpeed[] = new int[SnakeLength]; // Speed of the snake
int x[] = new int[SnakeLength]; // X position of each oval
int y[] = new int[SnakeLength]; // Y position of each oval
int directionX[] = new int[SnakeLength]; // X Direction each oval is going (RIGHT, LEFT)
int directionY[] = new int[SnakeLength]; // Y Direction each oval is going (DOWN, UP)
Color SnakeColor[] = new Color[SnakeLength]; // Color of each oval
Timer timer;
Button B1;
Button B2;
Button B3;
public void init()
{
B1 = new Button("Feed Snake");
add(B1);
B3 = new Button("Starve Snake");
add(B3);
B2 = new Button("Exit");
add(B2);
for(int i = 0; i < SnakeLength; i++)
{
x[i] = (int)(i * 10 + getWidth()); // X-Position for each ball
y[i] = (int)(i * 10 + getHeight()); // Y-Position for each ball
directionX[i] = (int)(1 * 2); // X-Direction for each ball
directionY[i] = (int)(1 * 2); // Y-Direction for each ball
SnakeSpeed[i] = (int)(1); // Speed for each ball
}
timer = new Timer(1, this); //Set the delay
B1.addActionListener(this);
B2.addActionListener(this);
B3.addActionListener(this);
}
public void paint(Graphics g)
{
int i;
for(i = 0; i < SnakeLength; i++)
{
g.setColor(SnakeColor[i]); // Set the color to the color previously set
g.fillOval(x[i], y[i], SnakeWidth, SnakeWidth); // Draw the oval
if(x[i] >= getWidth() - SnakeWidth) // Check if the ball is at the edge of the screen
directionX[i] = 1; // Set the direction X to LEFT
if(x[i] <= 0) // Check if the ball is at the edge of the screen
directionX[i] = 0; // Set the direction X to RIGHT
if(y[i] >= getHeight() - SnakeWidth) // Check if the ball is at the edge of the screen
directionY[i] = 1; // Set the direction Y to UP
if(y[i] <= 0) // Check if the ball is at the edge of the screen
directionY[i] = 0; // Set the direction Y to DOWN
if(directionX[i] == 0) // Check if the ball is going RIGHT
x[i]+=SnakeSpeed[i]; // Add the value of the current balls speed to the current balls x position (Makes the ball move to the right)
if(directionX[i] == 1) // Check if the ball is going LEFT
x[i]-=SnakeSpeed[i]; // Add the value of the current balls speed to the current balls x position (Makes the ball move to the left)
if(directionY[i] == 0) // Check if the ball is going DOWN
y[i]+=SnakeSpeed[i]; // Add the value of the current balls speed to the current balls y position (Makes the ball move to the down)
if(directionY[i] == 1) // Check if the ball is going UP
y[i]-=SnakeSpeed[i]; // Add the value of the current balls speed to the current balls y position (Makes the ball move to the up)
if(x[i] <= 0) // Check if the ball is at the edge of the screen
{
setBackground(Color.black);
SnakeColor[i] = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255));
} // Random ball color
else if(x[i] >= getWidth() - SnakeWidth)
{
setBackground(Color.white);
SnakeColor[i] = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255));
} // Random ball color
else if(y[i] >= getHeight() - SnakeWidth)
{
setBackground(Color.darkGray);
SnakeColor[i] = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255));
} // Random ball color
else if(y[i] <= 0)
{
setBackground(Color.green);
SnakeColor[i] = new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255));
} // Random ball color
}
}
public void actionPerformed(ActionEvent e) // Called each time the timers delay is up
{
repaint();
if (e.getSource() == B1)
{SnakeWidth = SnakeWidth+2;}
else if (e.getSource() == B2)
{System.exit(0);}
else if (e.getSource() == B3)
{SnakeWidth = SnakeWidth-2;}
}
public void start()
{
timer.start(); // Start drawing
}
public static void main(String args[])
{
AppletFrame.startApplet("Snake","Snake",args);
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment