Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.67 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.KeyEvent;
  5. import java.awt.event.KeyListener;
  6.  
  7. public class Surface extends java.applet.Applet implements Runnable,
  8.         ActionListener, KeyListener {
  9.     int frame;
  10.     int delay;
  11.     int scene = 0;
  12.     Thread animator;
  13.     Image backbuffer;
  14.     Graphics backg;
  15.     Button play;
  16.     Cubes cubes = new Cubes();
  17.     public boolean left = false, right = false;
  18.     int mx, my;
  19.     Ship ship = new Ship(Color.gray);
  20.  
  21.     /**
  22.      * Initialize the applet and compute the delay between frames.
  23.      */
  24.     public void init() {
  25.         this.setBackground(Color.black);
  26.         this.setSize(500, 400);
  27.         int fps = 45;
  28.         delay = (fps > 0) ? (1000 / fps) : 100;
  29.         backbuffer = createImage(500, 400);
  30.         backg = backbuffer.getGraphics();
  31.         backg.setColor(Color.white);
  32.         play = new Button("      Play!      ");
  33.         this.addKeyListener(this);
  34.         this.setFocusable(true);
  35.         add(play);
  36.         // set the actionlistener to the button
  37.         play.addActionListener(this);
  38.  
  39.     }
  40.  
  41.     /**
  42.      * This method is called when the applet becomes visible on the screen.
  43.      * Create a thread and start it.
  44.      */
  45.     public void start() {
  46.         animator = new Thread(this);
  47.         animator.start();
  48.     }
  49.  
  50.     public void Moveship() {
  51.         ship.distance += 1;
  52.         if (ship.getX() > 20 && ship.getX() < 450) {
  53.             if (right) {
  54.  
  55.                 ship.IncrX(5);
  56.  
  57.             }
  58.             if (left) {
  59.  
  60.                 ship.IncrX(-5);
  61.             }
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * This method is called by the thread that was created in the start method.
  67.      * It does the main animation.
  68.      */
  69.     public void run() {
  70.  
  71.         // Remember the starting time
  72.         long tm = System.currentTimeMillis();
  73.         while (Thread.currentThread() == animator) {
  74.             // Display the next frame of animation.
  75.             this.requestFocus();
  76.             if (scene == 1) {
  77.                 Moveship();
  78.                 if(ship.rl % 10 ==0){
  79.                     cubes.DrawNormal(backg);
  80.                                         }
  81.             }
  82.             cubes.cubetick();
  83.             Draw();
  84.             repaint();
  85.  
  86.             // Delay depending on how far we are behind.
  87.             try {
  88.                 tm += delay;
  89.                 Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
  90.             } catch (InterruptedException e) {
  91.                 break;
  92.             }
  93.             // Advance the frame
  94.             frame++;
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * This method is called when the applet is no longer visible. Set the
  100.      * animator variable to null so that the thread will exit before displaying
  101.      * the next frame.
  102.      */
  103.     public void stop() {
  104.         animator = null;
  105.     }
  106.  
  107.     public void Draw() {
  108.         switch (scene) {
  109.         case 0:
  110.             play.setLocation(200, 300);
  111.             Color bg = Color.white;
  112.             Dimension d = new Dimension(500, 400);
  113.             int h = d.height / 2;
  114.             for (int x = 0; x < d.width; x++) {
  115.                 int y1 = (int) ((1.0 + Math.sin((x - frame) * 0.05)) * h);
  116.                 int y2 = (int) ((1.0 + Math.sin((x + frame) * 0.07)) * h);
  117.  
  118.                 if (y1 > y2) {
  119.                     int t = y1;
  120.                     y1 = y2;
  121.                     y2 = t;
  122.                 }
  123.  
  124.                 backg.setColor(bg);
  125.                 backg.drawLine(x, 0, x, y1);
  126.                 backg.drawLine(x, y2, x, d.height);
  127.                 backg.setColor(Color.black);
  128.                 backg.drawLine(x, y1, x, y2);
  129.             }
  130.             backg.setColor(Color.orange);
  131.             backg.setFont(new Font("Arial", 0, 42));
  132.             backg.drawString("JCube", 180, 50);
  133.             break;
  134.         case 1:
  135.             play.setVisible(false);
  136.             backg.setColor(Color.black);
  137.             backg.fillRect(0, 0, 500, 400);
  138.             backg.setColor(Color.orange);
  139.             ship.DrawShip((Graphics2D) backg);
  140.             backg.drawString("" + ship.distance, 20, 40);
  141.             if(ship.rl % 10 ==0){
  142.                 cubes.DrawNormal(backg);
  143.                
  144.                 }
  145.             break;
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * Paint a frame of animation.
  151.      */
  152.     public void paint(Graphics g) {
  153.         update(g);
  154.     }
  155.  
  156.     /**
  157.      * Paint a frame of animation.
  158.      */
  159.     public void update(Graphics g) {
  160.         g.drawImage(backbuffer, 0, 0, this);
  161.     }
  162.  
  163.     @Override
  164.     public void actionPerformed(ActionEvent e) {
  165.         if (e.getSource() == play) {
  166.             // set the text of the textfield to our message
  167.             scene = 1;
  168.         }
  169.  
  170.     }
  171.  
  172.     @Override
  173.     public void keyPressed(KeyEvent k) {
  174.         if (k.getKeyCode() == 37) {
  175.             left = true;
  176.         }
  177.         if (k.getKeyCode() == 39) {
  178.             right = true;
  179.         }
  180.  
  181.     }
  182.  
  183.     @Override
  184.     public void keyReleased(KeyEvent k) {
  185.  
  186.         if (k.getKeyCode() == 37) {
  187.             left = false;
  188.         }
  189.         if (k.getKeyCode() == 39) {
  190.             right = false;
  191.         }
  192.  
  193.     }
  194.  
  195.     @Override
  196.     public void keyTyped(KeyEvent k) {
  197.  
  198.     }
  199. }
  200.  
  201.  
  202. import java.awt.Color;
  203. import java.awt.Graphics;
  204. import java.awt.Graphics2D;
  205. import java.awt.Polygon;
  206. import java.awt.geom.AffineTransform;
  207.  
  208.  
  209. public class Ship {
  210.         int x=230,y=380;
  211.         int distance=0;
  212.         int rl = distance/10;
  213.        /**
  214.          * @return the rl
  215.          */
  216.         public int getRl() {
  217.             return rl;
  218.         }
  219.     Color co=null;
  220. public int getDistance() {
  221.                 return distance;
  222.         }
  223.         public void setDistance(int distance) {
  224.                 this.distance = distance;
  225.         }
  226. public Ship(Color c){
  227.         co=c;
  228. }
  229. public int IncrX(int amount){
  230.         return x+=amount;
  231. }
  232. public int getX() {
  233.         return x;
  234. }
  235. public int getY() {
  236.         return y;
  237. }
  238. public void setX(int x) {
  239.         this.x = x;
  240. }
  241. public void setY(int y) {
  242.         this.y = y;
  243. }
  244. public Graphics DrawShip(Graphics2D g){
  245.         g.setColor(co);
  246.         Polygon poly = new Polygon();
  247.         poly.addPoint((int) (x), y);
  248.         poly.addPoint((int) (x+30), y);
  249.         poly.addPoint((int) (x+15), y-30);
  250.         g.fillPolygon(poly);
  251.        g.setColor(new Color(200,150,50,200));
  252.         g.drawLine(0, y-15, x+15, y-15);
  253.         g.drawLine(500, y-15, x+15, y-15);
  254.         g.drawLine(x+15, 0, x+15, y-15);
  255. //      g.drawLine(000, y-30, 500, y-30);
  256. //      g.drawLine(0, y, 500, y);
  257.         //Hoping for some cube field like tilting
  258.         //g.rotate((rotation * Math.PI / 180));
  259.  
  260.         return g;
  261. }
  262. }
  263.  
  264.  
  265. //
  266.  
  267.  
  268. import java.awt.Color;
  269.  
  270.  
  271. public class Cube {
  272.         int x;
  273.         int y;
  274.         Color c;
  275. public Cube(int x, int y, Color color){
  276.         this.x=x;
  277.         this.y=y;
  278.         this.c=color;
  279. }
  280. }
  281.  
  282.  
  283. import java.awt.Color;
  284. import java.awt.Graphics;
  285. import java.util.Random;
  286.  
  287.  
  288. public class Cubes {
  289. public Cube[] cubes = new Cube[5000];
  290. int place=0;
  291.  
  292. Color re = new Color(200,30,30,200);
  293. public Cubes(){
  294.        
  295. }
  296. public Graphics DrawNormal(Graphics g){
  297.     int x1=0,x2=0,x3=0;
  298.     Random r = new Random();
  299.     x1=r.nextInt(500);
  300.     x2=r.nextInt(500);
  301.     x3=r.nextInt(500);
  302.         cubes[place]= new Cube(x1,0,re);
  303.         place++;
  304.         cubes[place]= new Cube(x2,0,re);
  305.         place++;
  306.         cubes[place]= new Cube(x3,0,re);
  307.         place++;
  308.  
  309.         for(int f=0;f<f+3;f++){
  310.             g.setColor(Color.DARK_GRAY);
  311.             g.draw3DRect(cubes[f].x, cubes[f].y, 30, 40, true);
  312.             g.setColor(cubes[f].c);
  313.             g.fill3DRect(cubes[f].x, cubes[f].y, 30, 40, true);
  314.         }
  315.     return g;
  316.    
  317. }
  318. public void cubetick(){
  319.     for(int j=0;j<place;j++){
  320.         if(cubes[j]!=null){
  321.     cubes[j].y+=5;
  322.         }
  323.     }
  324. }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement