Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- public class Surface extends java.applet.Applet implements Runnable,
- ActionListener, KeyListener {
- int frame;
- int delay;
- int scene = 0;
- Thread animator;
- Image backbuffer;
- Graphics backg;
- Button play;
- Cubes cubes = new Cubes();
- public boolean left = false, right = false;
- int mx, my;
- Ship ship = new Ship(Color.gray);
- /**
- * Initialize the applet and compute the delay between frames.
- */
- public void init() {
- this.setBackground(Color.black);
- this.setSize(500, 400);
- int fps = 45;
- delay = (fps > 0) ? (1000 / fps) : 100;
- backbuffer = createImage(500, 400);
- backg = backbuffer.getGraphics();
- backg.setColor(Color.white);
- play = new Button(" Play! ");
- this.addKeyListener(this);
- this.setFocusable(true);
- add(play);
- // set the actionlistener to the button
- play.addActionListener(this);
- }
- /**
- * This method is called when the applet becomes visible on the screen.
- * Create a thread and start it.
- */
- public void start() {
- animator = new Thread(this);
- animator.start();
- }
- public void Moveship() {
- ship.distance += 1;
- if (ship.getX() > 20 && ship.getX() < 450) {
- if (right) {
- ship.IncrX(5);
- }
- if (left) {
- ship.IncrX(-5);
- }
- }
- }
- /**
- * This method is called by the thread that was created in the start method.
- * It does the main animation.
- */
- public void run() {
- // Remember the starting time
- long tm = System.currentTimeMillis();
- while (Thread.currentThread() == animator) {
- // Display the next frame of animation.
- this.requestFocus();
- if (scene == 1) {
- Moveship();
- if(ship.rl % 10 ==0){
- cubes.DrawNormal(backg);
- }
- }
- cubes.cubetick();
- Draw();
- repaint();
- // Delay depending on how far we are behind.
- try {
- tm += delay;
- Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
- } catch (InterruptedException e) {
- break;
- }
- // Advance the frame
- frame++;
- }
- }
- /**
- * This method is called when the applet is no longer visible. Set the
- * animator variable to null so that the thread will exit before displaying
- * the next frame.
- */
- public void stop() {
- animator = null;
- }
- public void Draw() {
- switch (scene) {
- case 0:
- play.setLocation(200, 300);
- Color bg = Color.white;
- Dimension d = new Dimension(500, 400);
- int h = d.height / 2;
- for (int x = 0; x < d.width; x++) {
- int y1 = (int) ((1.0 + Math.sin((x - frame) * 0.05)) * h);
- int y2 = (int) ((1.0 + Math.sin((x + frame) * 0.07)) * h);
- if (y1 > y2) {
- int t = y1;
- y1 = y2;
- y2 = t;
- }
- backg.setColor(bg);
- backg.drawLine(x, 0, x, y1);
- backg.drawLine(x, y2, x, d.height);
- backg.setColor(Color.black);
- backg.drawLine(x, y1, x, y2);
- }
- backg.setColor(Color.orange);
- backg.setFont(new Font("Arial", 0, 42));
- backg.drawString("JCube", 180, 50);
- break;
- case 1:
- play.setVisible(false);
- backg.setColor(Color.black);
- backg.fillRect(0, 0, 500, 400);
- backg.setColor(Color.orange);
- ship.DrawShip((Graphics2D) backg);
- backg.drawString("" + ship.distance, 20, 40);
- if(ship.rl % 10 ==0){
- cubes.DrawNormal(backg);
- }
- break;
- }
- }
- /**
- * Paint a frame of animation.
- */
- public void paint(Graphics g) {
- update(g);
- }
- /**
- * Paint a frame of animation.
- */
- public void update(Graphics g) {
- g.drawImage(backbuffer, 0, 0, this);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == play) {
- // set the text of the textfield to our message
- scene = 1;
- }
- }
- @Override
- public void keyPressed(KeyEvent k) {
- if (k.getKeyCode() == 37) {
- left = true;
- }
- if (k.getKeyCode() == 39) {
- right = true;
- }
- }
- @Override
- public void keyReleased(KeyEvent k) {
- if (k.getKeyCode() == 37) {
- left = false;
- }
- if (k.getKeyCode() == 39) {
- right = false;
- }
- }
- @Override
- public void keyTyped(KeyEvent k) {
- }
- }
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Polygon;
- import java.awt.geom.AffineTransform;
- public class Ship {
- int x=230,y=380;
- int distance=0;
- int rl = distance/10;
- /**
- * @return the rl
- */
- public int getRl() {
- return rl;
- }
- Color co=null;
- public int getDistance() {
- return distance;
- }
- public void setDistance(int distance) {
- this.distance = distance;
- }
- public Ship(Color c){
- co=c;
- }
- public int IncrX(int amount){
- return x+=amount;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- public void setX(int x) {
- this.x = x;
- }
- public void setY(int y) {
- this.y = y;
- }
- public Graphics DrawShip(Graphics2D g){
- g.setColor(co);
- Polygon poly = new Polygon();
- poly.addPoint((int) (x), y);
- poly.addPoint((int) (x+30), y);
- poly.addPoint((int) (x+15), y-30);
- g.fillPolygon(poly);
- g.setColor(new Color(200,150,50,200));
- g.drawLine(0, y-15, x+15, y-15);
- g.drawLine(500, y-15, x+15, y-15);
- g.drawLine(x+15, 0, x+15, y-15);
- // g.drawLine(000, y-30, 500, y-30);
- // g.drawLine(0, y, 500, y);
- //Hoping for some cube field like tilting
- //g.rotate((rotation * Math.PI / 180));
- return g;
- }
- }
- //
- import java.awt.Color;
- public class Cube {
- int x;
- int y;
- Color c;
- public Cube(int x, int y, Color color){
- this.x=x;
- this.y=y;
- this.c=color;
- }
- }
- import java.awt.Color;
- import java.awt.Graphics;
- import java.util.Random;
- public class Cubes {
- public Cube[] cubes = new Cube[5000];
- int place=0;
- Color re = new Color(200,30,30,200);
- public Cubes(){
- }
- public Graphics DrawNormal(Graphics g){
- int x1=0,x2=0,x3=0;
- Random r = new Random();
- x1=r.nextInt(500);
- x2=r.nextInt(500);
- x3=r.nextInt(500);
- cubes[place]= new Cube(x1,0,re);
- place++;
- cubes[place]= new Cube(x2,0,re);
- place++;
- cubes[place]= new Cube(x3,0,re);
- place++;
- for(int f=0;f<f+3;f++){
- g.setColor(Color.DARK_GRAY);
- g.draw3DRect(cubes[f].x, cubes[f].y, 30, 40, true);
- g.setColor(cubes[f].c);
- g.fill3DRect(cubes[f].x, cubes[f].y, 30, 40, true);
- }
- return g;
- }
- public void cubetick(){
- for(int j=0;j<place;j++){
- if(cubes[j]!=null){
- cubes[j].y+=5;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement