Advertisement
Guest User

Render class

a guest
Feb 10th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. package com.maestrum;
  2.  
  3. import java.awt.BasicStroke;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.RenderingHints;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.image.ConvolveOp;
  11. import java.awt.image.Kernel;
  12. import java.util.Random;
  13.  
  14. public class Render implements Runnable{
  15.    
  16.     public Graphics2D g2d;
  17.    
  18.     public double xScroll,yScroll;
  19.  
  20.     public int frames;
  21.     public int fps;
  22.     public long lastTime;
  23.    
  24.     public int[] pSequence = new int[40];  
  25.     public SpaceGame game;
  26.    
  27.     public Entity trackedEntity;
  28.    
  29.     public Random rand;
  30.    
  31.     public Render(SpaceGame game, Graphics2D g){
  32.         this.game = game;
  33.         this.g2d = g;
  34.         this.rand = new Random();
  35.         for(int i = 0 ; i < 40; i++){
  36.             pSequence[i] = rand.nextInt(15) + 1;
  37.         }
  38.     }
  39.  
  40.     @Override
  41.     public void run() {    
  42.         renderLoop();
  43.     }
  44.    
  45.     public void renderLoop(){
  46.         while(true){
  47.             game.render();
  48.             if(System.currentTimeMillis() - lastTime >= 1000){
  49.                 fps = frames;
  50.                 frames = 0;
  51.                 lastTime = System.currentTimeMillis();
  52.             }
  53.             frames++;
  54.         }
  55.     }
  56.    
  57.     public void renderPlanets(){
  58.         overlayImage(ImageHandler.background, 0, 0, 1.5);
  59.         for(int i = 0 ; i < 20; i++){
  60.             overlayImage(ImageHandler.planets[pSequence[i]/4][pSequence[i]%4], i * 400 - xScroll/pSequence[i], i * pSequence[i]*40 - yScroll/pSequence[i]*2, pSequence[i]);
  61.         }
  62.     }
  63.    
  64.     private class PlanetRenderer {
  65.        
  66.        
  67.        
  68.     }
  69.    
  70.     public void overlayString(String s, double x, double y, int fontSize, int colour){
  71.         drawString(s, x+xScroll, y+yScroll, fontSize, colour);
  72.     }
  73.    
  74.     public void overlayRectangle(double x, double y, int xs, int ys, int colour){
  75.         drawRectangle(x+xScroll, y+yScroll, xs, ys, colour);
  76.     }
  77.    
  78.     public void overlayBlurred(BufferedImage img, double x, double y, double scale){
  79.         drawImageBlurred(img, x+xScroll, y+yScroll, scale);
  80.     }
  81.  
  82.     public void overlayImage(BufferedImage img, double x, double y, double scale){
  83.         drawImage(img, x+xScroll, y+yScroll, scale);
  84.     }
  85.    
  86.     public BufferedImage execute(BufferedImage img) {
  87.         // TODO Auto-generated method stub
  88.         float weight = 1.0f/2.0f;
  89.        
  90.         float [] elements = {weight, weight, weight, weight, weight, weight, weight, weight, weight};
  91.        
  92.         Kernel k = new Kernel (3,3,elements);
  93.         ConvolveOp op = new ConvolveOp(k);
  94.        
  95.         BufferedImage dest = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
  96.        
  97.         op.filter(img, dest);
  98.         return dest;
  99.     }
  100.    
  101.     public void drawImageBlurred(BufferedImage img, double x, double y, double scale){
  102.         x -= xScroll;
  103.         y -= yScroll;
  104.         BufferedImage image = new BufferedImage((int)(img.getWidth()*scale), (int)(img.getHeight()*scale), BufferedImage.TYPE_INT_ARGB);
  105.         Graphics g = image.getGraphics();
  106.         g.drawImage(img, 0, 0, (int)(img.getWidth()*scale), (int)(img.getHeight()*scale), null);
  107.         execute(image);
  108.         g2d.drawImage(image, (int)x, (int)y, null);
  109.         g.dispose();
  110.     }
  111.    
  112.     public void drawString(String s, Vector2D pos, int fontSize, int colour){
  113.         drawString(s, pos.x, pos.y, fontSize, colour);
  114.     }
  115.    
  116.     public void drawString(String s, double x, double y, int fontSize, int colour){
  117.         if(s == null){
  118.             return;
  119.         }
  120.         x -= xScroll;
  121.         y -= yScroll;
  122.         BufferedImage img = new BufferedImage(s.length()*fontSize+1, fontSize*2, BufferedImage.TYPE_INT_ARGB);
  123.         Graphics g = img.getGraphics();
  124.         g.setColor(new Color(colour));
  125.         g.setFont(new Font("Consolas", Font.BOLD, fontSize));
  126.         g.drawString(s, 0, img.getHeight()/2);
  127.         g2d.drawImage(img, (int)x, (int)y, null);
  128.         g.dispose();
  129.     }
  130.    
  131.     public void drawImage(BufferedImage img, Vector2D pos, double scale){
  132.         drawImage(img, pos.x, pos.y, scale);
  133.     }
  134.    
  135.     public void drawLine(Vector2D v1, Vector2D v2, int colour, int width){
  136.         drawLine(v1.x, v1.y, v2.x, v2.y, colour, width);
  137.     }
  138.    
  139.     public void drawLine(double x1, double y1, double x2, double y2, int colour, int lWidth){
  140.         x1 -= xScroll;
  141.         y1 -= yScroll;
  142.         x2 -= xScroll;
  143.         y2 -= yScroll;
  144.         g2d.setColor(new Color(colour));
  145.         g2d.setStroke(new BasicStroke(lWidth));
  146.         g2d.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
  147.     }
  148.    
  149.     public void drawImage(BufferedImage img, double x, double y, double scale){
  150.         x -= xScroll;
  151.         y -= yScroll;
  152.         BufferedImage image = new BufferedImage((int)(img.getWidth()*scale), (int)(img.getHeight()*scale), BufferedImage.TYPE_INT_ARGB);
  153.         Graphics g = image.getGraphics();
  154.         g.drawImage(img, 0, 0, (int)(img.getWidth()*scale), (int)(img.getHeight()*scale), null);
  155.         g2d.drawImage(image, (int)x, (int)y, null);
  156.         g.dispose();
  157.     }
  158.    
  159.     public void drawRectangle(Vector2D pos, int xs, int ys, int colour){
  160.         drawRectangle(pos.x, pos.y, xs, ys, colour);
  161.     }
  162.    
  163.     public void drawRectangle(double x, double y, int xs, int ys, int colour){
  164.         if(xs <= 0){
  165.             return;
  166.         }
  167.         x -= xScroll;
  168.         y -= yScroll;
  169.         BufferedImage image = new BufferedImage(xs, ys, BufferedImage.TYPE_INT_RGB);
  170.         Graphics2D g = (Graphics2D) image.getGraphics();
  171.         g.setColor(new Color(colour));
  172.         g.fillRect(0, 0, xs, ys);
  173.         g2d.drawImage(image, (int)x, (int)y, null);
  174.         g.dispose();
  175.     }
  176.    
  177.     public void drawImageRotated(BufferedImage img, Vector2D pos, double scale, double angle) {
  178.         drawImageRotated(img, pos.x, pos.y, scale, angle);
  179.     }
  180.    
  181.      public void drawImageRotated(BufferedImage img, double x, double y, double scale, double angle) {
  182.             x -= xScroll;
  183.             y -= yScroll;  
  184.             BufferedImage image = new BufferedImage((int)(img.getWidth() * 1.5D), (int)(img.getHeight() * 1.5D), 2);
  185.             Graphics2D g = (Graphics2D)image.getGraphics();
  186.             g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  187.             g.rotate(Math.toRadians(angle), image.getWidth() / 2, image.getHeight() / 2);
  188.             g.drawImage(img, image.getWidth() / 2 - img.getWidth() / 2, image.getHeight() / 2 - image.getHeight() / 2, null);
  189.             g2d.drawImage(image, (int)(x-image.getWidth()*scale/2), (int)(y-image.getHeight()*scale/2), (int)(image.getWidth()*scale), (int)(image.getHeight()*scale), null);
  190.             g.dispose();     
  191.      }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement