Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. import javax.imageio.ImageIO;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.Date;
  8.  
  9.  
  10. public class Space extends JFrame{
  11.  
  12.    
  13.     /**
  14.      *
  15.      */
  16.     private static final long serialVersionUID = 1L;
  17.     private boolean running=false;
  18.     private long startZeit;
  19.     private long endZeit;
  20.     private Graphics g;
  21.     private Image img;
  22.     private int width;
  23.     private int height;
  24.    
  25.     /**
  26.      * erzeugt eine neue GameFrame, setzt sie visible und initialisiert alles
  27.      */
  28.     public Space(){
  29.         gameFrame();
  30.         this.setVisible(true);
  31.         init();
  32.     }
  33.    
  34.     /**
  35.      * initialisiert Variablen, startet einen einen Loop
  36.      */
  37.     public void init(){
  38.         running = true;
  39.         loop();
  40.     }
  41.    
  42.     /**
  43.      * updatet alle "x" Millisekunden
  44.      */
  45.     public void loop(){
  46.         while(running){
  47.             startZeit = System.nanoTime();
  48.             endZeit = startZeit;
  49.             updateGame();
  50.             paintToGame(g);
  51.             while(((endZeit-startZeit)/1000000L)<40){
  52.                 endZeit = System.nanoTime();
  53.             }
  54.         }
  55.     }
  56.    
  57.     /**
  58.      * hier findet das Spielgeschehen statt
  59.      */
  60.     public void updateGame(){
  61.        
  62.     }
  63.    
  64.     /**
  65.      * hier wird auf den GameFrame gezeichnet
  66.      */
  67.     public void paintToGame(Graphics g){
  68.         g.drawImage (img, 0, 0, this);
  69.         g.drawString("" + new Date(), width/2, height/2);
  70.     }
  71.    
  72.    
  73.     /**
  74.      * erzeugt den GameFrame
  75.      */
  76.     public void gameFrame(){
  77.        
  78.         GraphicsEnvironment env = GraphicsEnvironment
  79.                 .getLocalGraphicsEnvironment();
  80.         Rectangle bounds = env.getMaximumWindowBounds();
  81.         width = bounds.width;
  82.         height = bounds.height;
  83.         setSize(width, height);
  84.        
  85.         setTitle("SpaceArc v1.0");
  86.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  87.        
  88.         Container content= getContentPane();
  89.         createBackground(g);
  90.  
  91.     }
  92.    
  93.     public void createBackground(Graphics g){
  94.         if (img == null){
  95.             try {
  96.                 img = ImageIO.read(new File("C:\\Users\\SuperStar\\Desktop\\Workspace\\SpaceArc\\Images\\Bg.jpg"));
  97.                 g = img.getGraphics();
  98.             } catch (IOException e) {
  99.                 e.printStackTrace();
  100.             }
  101.         }
  102.         g.drawImage (img, 0, 0, this);
  103.     }
  104.    
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement