Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package space;
- import Entities.Planet;
- import java.awt.BorderLayout;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.GraphicsEnvironment;
- import java.awt.Rectangle;
- import java.awt.RenderingHints;
- import java.awt.Toolkit;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Space extends Canvas implements Runnable {
- //public int SCREENWIDTH = getScreenSize().width;
- //public int SCREENHEIGHT = getScreenSize().height;
- public int SCREENWIDTH = 1280;
- public int SCREENHEIGHT = 720;
- public String TITLE = "Space";
- private JFrame frame;
- private Graphics2D g;
- private BufferedImage image = new BufferedImage(SCREENWIDTH, SCREENHEIGHT, BufferedImage.TYPE_INT_RGB);
- private int FPS = 60;
- private int gametime = 0;
- private Thread thread;
- private boolean running = false;
- Overlay overlay;
- Screen screen;
- Planet planet;
- public Mouse mouse;
- public Space() {
- g = (Graphics2D) image.getGraphics();
- }
- public void init() {
- Space space = new Space();
- JPanel p = new JPanel();
- p.setPreferredSize(new Dimension(SCREENWIDTH/5, SCREENHEIGHT));
- p.setMinimumSize(new Dimension(SCREENWIDTH/5, SCREENHEIGHT));
- p.setMaximumSize(new Dimension(SCREENWIDTH/5, SCREENHEIGHT));
- p.setBackground(Color.red);
- frame = new JFrame(TITLE);
- frame.pack();
- frame.setPreferredSize(new Dimension(SCREENWIDTH, SCREENHEIGHT));
- frame.setMinimumSize(new Dimension(SCREENWIDTH, SCREENHEIGHT));
- frame.setMaximumSize(new Dimension(SCREENWIDTH, SCREENHEIGHT));
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.add(p);
- frame.add(space, BorderLayout.CENTER);
- frame.setResizable(false);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- space.start();
- }
- public static void main(String[] args) {
- new Space().init();
- }
- public Dimension getScreenSize() {
- int screenWidth = (Toolkit.getDefaultToolkit().getScreenSize()).width;
- int screenHeight = (Toolkit.getDefaultToolkit().getScreenSize()).height;
- Dimension scrnSize = Toolkit.getDefaultToolkit().getScreenSize();
- Rectangle winSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
- int taskBarHeight = scrnSize.height - winSize.height;
- Dimension finalScreenSize = new Dimension((screenWidth), (screenHeight - taskBarHeight));
- return finalScreenSize;
- }
- public void start() {
- mouse = new Mouse();
- addMouseListener(mouse);
- addMouseMotionListener(mouse);
- screen = new Screen(SCREENWIDTH, SCREENHEIGHT, mouse);
- thread = new Thread(this, "Display");
- thread.start();
- running = true;
- }
- public void stop() {
- running = false;
- try {
- thread.join();
- } catch (Exception e) {
- }
- }
- @Override
- public void run() {
- long lastTime = System.nanoTime();
- long timer = System.currentTimeMillis();
- final double ns = 1000000000.0 / FPS;
- double delta = 0;
- int frames = 0;
- int updates = 0;
- requestFocus();
- while (running) {
- long currentTime = System.nanoTime();
- delta = delta += (currentTime - lastTime) / ns;
- lastTime = currentTime;
- while (delta >= 1) {
- update();
- updates++;
- delta--;
- }
- render();
- frames++;
- if (System.currentTimeMillis() - timer > 1000) {
- timer += 1000;
- System.out.println("Updates: " + updates + " Frames: " + frames);
- updates = 0;
- frames = 0;
- }
- }
- stop();
- }
- public void update() {
- screen.update(gametime);
- gametime++;
- }
- public void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- Graphics g = bs.getDrawGraphics();
- Graphics2D g2d = (Graphics2D) g;
- RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
- g2d.setRenderingHints(rh);
- g2d.drawImage(image, 0, 0, getWidth(), getHeight(), null);
- g2d.setColor(new Color(5,5,5));
- g2d.fillRect(0, 0, SCREENWIDTH, SCREENHEIGHT);
- screen.render(g2d);
- g2d.dispose();
- bs.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement