Advertisement
tyridge77

Window

Feb 20th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.98 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. // Where our rendering and the heart of the game takes place
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14. import java.awt.AWTException;
  15. import java.awt.Color;
  16. import java.awt.Component;
  17. import java.awt.Dimension;
  18. import java.awt.Font;
  19. import java.awt.FontFormatException;
  20. import java.awt.Graphics;
  21. import java.awt.GraphicsEnvironment;
  22. import java.awt.Image;
  23. import java.awt.Point;
  24. import java.awt.Rectangle;
  25. import java.awt.Robot;
  26. import java.awt.Toolkit;
  27. import java.awt.image.BufferStrategy;
  28. import java.awt.image.BufferedImage;
  29. import java.io.BufferedReader;
  30. import java.io.File;
  31. import java.io.IOException;
  32. import java.io.Reader;
  33. import java.util.HashMap;
  34. import java.util.Random;
  35.  
  36. import javax.imageio.ImageIO;
  37. import javax.sound.sampled.LineUnavailableException;
  38. import javax.sound.sampled.UnsupportedAudioFileException;
  39. import javax.swing.Box;
  40. import javax.swing.BoxLayout;
  41. import javax.swing.Icon;
  42. import javax.swing.ImageIcon;
  43. import javax.swing.JButton;
  44. import javax.swing.JComponent;
  45. import javax.swing.JFrame;
  46. import javax.swing.JLabel;
  47. import javax.swing.JPanel;
  48. import javax.swing.SwingConstants;
  49. import javax.swing.border.EmptyBorder;
  50.  
  51. import kuusisto.tinysound.Music;
  52. import kuusisto.tinysound.Sound;
  53. import kuusisto.tinysound.TinySound;
  54.  
  55. class Window extends JFrame {
  56.  
  57. private static final long serialVersionUID = 1L;
  58.    
  59. GameMain game;
  60.  
  61. JLabel title;
  62. JPanel WindowPanel;
  63. JLabel Title;
  64. JButton Enter;
  65. JButton Enter2;
  66.  
  67. JButton Settings;
  68. JLabel torch;
  69.  
  70. BackgroundPane MenuPane;
  71.  
  72. Component TitleGap;
  73.  
  74.  
  75.  
  76.  
  77. Music TitleMusicSound;  
  78. Music LonelyVoyage;
  79.  
  80. int PixelSize = 10;
  81.  
  82.  
  83. static Vector2D TorchLocation;
  84.  
  85.  
  86. int lightx = 0;
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  boolean DrawBoundries = true;
  94.  boolean DisplayFPS = true;
  95.  
  96.  
  97.  static final int ScreenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
  98.  static final int ScreenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
  99.    
  100.  
  101.  BufferedImage canvas;
  102.  
  103.  
  104.  void SetFont(JComponent obj,String type,int size)
  105.  {
  106.   Font font = new Font(type,Font.PLAIN,size);
  107.   obj.setFont(font);
  108.  }
  109.  
  110.  LightSource globallight;
  111.  
  112.  public void SetMenuVisible(boolean b)
  113.  {
  114.   WindowPanel.setVisible(b);
  115.  }
  116.  public void ChangeStatus(String type) throws IOException       // Changes the game status and rendering consequentially
  117. , UnsupportedAudioFileException, LineUnavailableException
  118.  {
  119.   if(type=="Game")
  120.   {  
  121.      
  122.      
  123.  
  124.    MenuPane.setVisible(false);
  125.    TitleMusicSound.stop();
  126.    
  127.  
  128.    LonelyVoyage = TinySound.loadMusic("SoundAssets\\LonelyVoyage.wav");
  129.    LonelyVoyage.play(true);
  130.    
  131.    //Torch HouseTorch = new Torch(GraphicsConfiguration,new Point(100+300,460));
  132.    
  133.    globallight = new LightSource(GraphicsConfiguration,0f,1.0f,new Point(100+300+125,200+300/2),null);
  134.    
  135.    GraphicsObject House = new GraphicsObject(GraphicsConfiguration,"House",new Point(50,300),3,6);
  136.    //GraphicsObject Box = new GraphicsObject(GraphicsConfiguration,"WoodBlock",new Point(75,835),50,50);
  137.  
  138.    //GraphicsObject Figure = new GraphicsObject(GraphicsConfiguration,"FigureTests",new Point(100+670,400));
  139.   }
  140.  }
  141.  
  142.  
  143.  
  144.  HashMap<Point,Color> pixelbase = new HashMap<Point,Color>();
  145.  GraphicsObject[] GraphicsObjects = new GraphicsObject[1000];
  146.  LightSource[] LightSources = new LightSource[1000];
  147.  
  148.  
  149.  // Graphics Object Configuration
  150.  // Used as the base configuration cache for all GraphicsObjects,
  151.  // and lets added classes show themselves into their respective arrays
  152.  
  153.  GraphicsObjectConfiguration GraphicsConfiguration = new GraphicsObjectConfiguration(this);
  154.  
  155.  File img = new File("src\\ImageAssets\\House.png");
  156.  
  157.  
  158.  BufferedImage image;
  159.  int[] originalcolor;
  160.  
  161.  
  162.  Point TopLeft;
  163.  
  164.  public float lerp(float a,float b,float t)
  165.  {
  166.   return a*(1-t)+(b*t);
  167.  }
  168.  public float clamp(float v,float min,float max)
  169.  {
  170.   if(v<min)
  171.   {
  172.    return min;
  173.   }
  174.   if(v>max)
  175.   {
  176.    return max;
  177.   }
  178.   return v;
  179.  }
  180.  public Color lerpcolors(Color color1,Color color2,float perc)
  181.  {
  182.   Color newcolor;
  183.   newcolor = new Color(
  184.   (int) lerp(color1.getRed(),color2.getRed(),perc),
  185.   (int) lerp(color1.getGreen(),color2.getGreen(),perc),
  186.   (int) lerp(color1.getBlue(),color2.getBlue(),perc)
  187.   );
  188.   return newcolor;
  189.  }
  190.  
  191.  public LightSource getNearestLightSource(Point pixelpoint)
  192.  {
  193.   int leastdistance = 10000;
  194.   LightSource NearestLight = null;
  195.   for(int i = 0;i<LightSources.length;i++)
  196.   {
  197.    LightSource light = LightSources[i];
  198.    if (light!=null)
  199.    {
  200.    
  201.    int distance = (int) light.location.distance(pixelpoint);
  202.    if((distance<leastdistance) && distance <= light.Range*2)
  203.    {
  204.     NearestLight=light;
  205.     leastdistance=distance;
  206.    }
  207.   }
  208.   }
  209.   return NearestLight;
  210.  }
  211.  public LightSource[] getInLightSources(Point pixelpoint)
  212.  {
  213.   LightSource[] inlightsources = null;
  214.   int sourcescount=-1;
  215.  
  216.   for(int i = 0;i<LightSources.length;i++)
  217.   {
  218.    LightSource light = LightSources[i];
  219.    if (light!=null)
  220.    {
  221.     int distance = (int) light.location.distance(pixelpoint);
  222.     if (distance<light.Range)
  223.     {
  224.      if(inlightsources==null)
  225.      {
  226.       inlightsources = new LightSource[10];
  227.      }
  228.      sourcescount++;
  229.      inlightsources[sourcescount]=light;
  230.     }
  231.    }
  232.   }
  233.   return inlightsources;
  234.  }
  235.  public int getOccupied(Object[] a)
  236.  {
  237.   int count = 0;
  238.   for(int i = 0;i<a.length;i++)
  239.   {
  240.    if(a[i]!=null)
  241.    {
  242.     count++;  
  243.    }
  244.   }
  245.   return count;
  246.  }
  247.  
  248.  
  249.  
  250.  public static float randomfloat(int min, int max) {
  251.  
  252.         Random rand = new Random();
  253.  
  254.         int randomNum = rand.nextInt((max - min) + 1) + min;
  255.  
  256.         return (float) randomNum;
  257. }
  258.  
  259.  
  260.  float lasttick = -1000;
  261.  int inc = 0;
  262.  public void render() throws InterruptedException, IOException
  263.  {
  264.  
  265.   inc+=1;
  266.   if(GameMain.GameStatus!="Game"){return;}
  267.   BufferStrategy bs = this.getBufferStrategy();
  268.   if(bs==null)
  269.   {
  270.    createBufferStrategy(3);
  271.    return;
  272.   }
  273.  
  274.   Graphics g = bs.getDrawGraphics();
  275.  
  276.  
  277.  
  278.  
  279.   globallight.Range = 200;//globallight.Range+1;
  280.   //globallight.Range = randomfloat(190,210);
  281.  
  282. Point ImageLocation = new Point(250,250);
  283.  
  284.  
  285. g.fillRect(0, 0,ScreenWidth,ScreenHeight);      // Fill a black outline
  286. g.setColor(Color.BLACK);
  287.  
  288.  
  289.  
  290.  
  291. for(int i = 0;i<GraphicsObjects.length;i++)
  292. {
  293.    
  294.     GraphicsObject object = GraphicsObjects[i];
  295.     if(object!=null)
  296.     {
  297.        
  298.     GraphicsObjectConfiguration.config config;
  299.     config = object.config;
  300.     BufferedImage image = config.image;
  301.     int width = config.width;
  302.     int height = config.height;
  303.     Point location = object.center;
  304.     int locationx = location.x;
  305.     int locationy = location.y;
  306.     int sizex = object.sizex;
  307.     int sizey = object.sizey;
  308.  
  309.     HashMap<Point, Color> PointToColor = config.configuration;
  310.    
  311.     int[] originalcolor = config.originalcolor; // A reference to the original color array, to set back to during the end
  312.     int[] modcolor = config.modcolor;
  313.  
  314.    
  315.     boolean lightsourcefound = false;
  316.    
  317.     boolean Rewriting = false;
  318.    
  319.     if(Rewriting==false)            // Rewrite mod color
  320.     {
  321.         for(int x = 0;x<width;x++)
  322.         {
  323.          for(int y = 0;y<width;y++)
  324.          {
  325.              
  326.              Point pixelpoint = new Point(x,y);
  327.              Point pixelpointglobal = new Point(locationx+x,locationy+y);
  328.              int pixelcolor;
  329.              pixelcolor = image.getRGB(x, y);
  330.              //PointToColor.get(pixelpoint);
  331.              //if(pixelcolor!=null)
  332.              //{
  333.                  //LightSource light = getNearestLightSource(pixelpointglobal);
  334.                  //System.out.println(light);
  335.                  //int red = pixelcolor.getRed();
  336.                  //int green = pixelcolor.getGreen();
  337.                  //int blue = pixelcolor.getBlue();
  338.                 // if(light!=null)                 
  339.                 // {
  340.                   //lightsourcefound=true;
  341.                   // Light each pixel based on the Gaussian profile
  342.                  // float lightbrightness = light.Brightness;
  343.                  // float distance_from_lightsource = (float) pixelpointglobal.distance(light.location);
  344.                  // float brightperc = 1;
  345.                  // (float) clamp((float)
  346.                  // (Math.pow(2.718,-(Math.pow(distance_from_lightsource,2)/Math.pow(light.Range,2)))),0,1)
  347.                  // *lightbrightness;
  348.                                        
  349.                  // red = (int) clamp((red*brightperc*lightbrightness),0.0f,255.0f);
  350.                  // green = (int) clamp((green*brightperc*lightbrightness),0.0f,255.0f);
  351.                  // blue = (int) clamp((blue*brightperc*lightbrightness),0.0f,255.0f);       
  352.                  
  353.                  // pixelcolor = new Color(red,green,blue,255); // Draw pixel
  354.                  // modcolor[width*y+x]=pixelcolor.getRGB();
  355.                 // }
  356.              //}
  357.          }
  358.       }
  359.      
  360.        
  361.      image.setRGB(0,0,width,height,modcolor,0,width);  
  362.      
  363.      
  364.     }
  365.    
  366.    
  367.      
  368.  
  369.        g.drawImage(image,locationx,locationy,sizex,sizey,null);    
  370.      
  371.        image.setRGB(0,0,width,height,originalcolor,0,width);
  372.   }
  373.  
  374.    
  375. }
  376.    
  377.                      
  378.                      
  379.                      
  380.                      
  381.                      
  382.                  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.   if(DisplayFPS==true)
  389.   {
  390.     g.setColor(Color.RED);
  391.    g.drawString("FPS: "+game.ticks,25,50);
  392.   }
  393.  
  394.  
  395.   if(!DrawBoundries)
  396.   {
  397.   for(int i = 0;i<GraphicsObjects.length;i++)           // Draw Boundaries
  398.   {
  399.    GraphicsObject o = GraphicsObjects[i];
  400.    if (o!=null)
  401.    {
  402.     Point center = o.center;
  403.     if(center!=null)
  404.     {
  405.      
  406.     g.drawRect(center.x,center.y,1,1);
  407.    
  408.     }
  409.  
  410.    }
  411.    
  412.    
  413.   }
  414.  }
  415.  
  416.   bs.show();
  417.  }
  418.  
  419.  
  420.  
  421.  public class BackgroundPane extends JPanel {
  422.  
  423.      /**
  424.      *
  425.      */
  426.     private static final long serialVersionUID = 1L;
  427.     private BufferedImage img;
  428.  
  429.      @Override
  430.      public Dimension getPreferredSize() {
  431.          BufferedImage img = getBackgroundImage();
  432.  
  433.          Dimension size = super.getPreferredSize();
  434.          if (img != null) {
  435.              size.width = Math.max(size.width, img.getWidth());
  436.              size.height = Math.max(size.height, img.getHeight());
  437.          }
  438.  
  439.          return size;
  440.      }
  441.  
  442.      public BufferedImage getBackgroundImage() {
  443.          return img;
  444.      }
  445.  
  446.      public void setBackgroundImage(BufferedImage value) {
  447.          if (img != value) {
  448.              BufferedImage old = img;
  449.              img = value;
  450.              firePropertyChange("background", old, img);
  451.              revalidate();
  452.              repaint();
  453.          }
  454.      }
  455.  
  456.      @Override
  457.      protected void paintComponent(Graphics g) {
  458.          super.paintComponent(g);
  459.          BufferedImage bg = getBackgroundImage();
  460.    
  461.          if (bg != null) {
  462.              int x = (getWidth() - bg.getWidth()) / 2;
  463.              int y = (getHeight() - bg.getHeight()) / 2;
  464.              g.drawImage(bg, x, y, this);
  465.              
  466.              File torchfile = new File(GameMain.ImageAssets+"Torch.png");
  467.  
  468.              ImageIcon torch = new ImageIcon(torchfile.getPath());
  469.              Image torchimage = torch.getImage();
  470.              System.out.println(GameMain.GameStatus);
  471.              
  472.              if(TorchLocation!=null)
  473.              {
  474.                g.drawImage(torchimage,TorchLocation.x,TorchLocation.y,this);
  475.              }
  476.              else
  477.              {
  478.              
  479.              }
  480.          }
  481.      }
  482.  
  483.  }
  484.  
  485.  
  486.  public void AddGraphicsObject(GraphicsObject object)
  487.  {
  488.      
  489.  }
  490.  
  491.  public Window(GameMain gameMain) throws FontFormatException, IOException, UnsupportedAudioFileException, LineUnavailableException, AWTException
  492.  {
  493.   super("Verwarring");      // super calls The JFrame constructor
  494.    
  495.     TinySound.init();
  496.  
  497.    
  498.    
  499.   game=gameMain;
  500.  
  501.  
  502.   canvas = new Robot().createScreenCapture(new Rectangle(0,0,ScreenWidth,ScreenHeight));
  503.  
  504.  
  505.  
  506.      
  507.      
  508.      
  509.      
  510.  
  511.  
  512.  
  513.  
  514.   TitleMusicSound = TinySound.loadMusic("SoundAssets\\TitleMusic.wav");  
  515.   TitleMusicSound.setVolume(0.5);
  516.   TitleMusicSound.play(false);
  517.  
  518.  
  519.  
  520.   File Cardinal = new File(GameMain.ImageAssets+"Cardinal.ttf");
  521.   Font CardinalFont = Font.createFont(NORMAL, Cardinal);
  522.   GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(CardinalFont);     // Registering Cardinal Font
  523.  
  524.  
  525.  
  526.  
  527.  
  528.   WindowPanel = new JPanel();       // Main Menu Panel
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.   MenuPane = new BackgroundPane();
  539.  
  540.   MenuPane.setLayout(new BoxLayout(MenuPane,BoxLayout.Y_AXIS));
  541.  
  542.  
  543.   WindowPanel.add(MenuPane);
  544.  
  545.   try {
  546.       BufferedImage bg = ImageIO.read(new File(GameMain.ImageAssets+"Gradient.png"));
  547.       MenuPane.setBackgroundImage(bg);
  548.   } catch (IOException ex) {
  549.       ex.printStackTrace();
  550.   }
  551.  
  552.  
  553.   TitleGap = Box.createRigidArea(new Dimension(0,40));
  554.  
  555.  
  556.   Title = new JLabel();
  557.   Title.setText("");
  558.   Title.setOpaque(false);
  559.   Title.setBackground(Color.PINK);
  560.   Title.setHorizontalAlignment(SwingConstants.CENTER);
  561.   Title.setAlignmentX(CENTER_ALIGNMENT);
  562.   Title.setMaximumSize(new Dimension(700,180));
  563.   Title.setPreferredSize(Title.getMaximumSize());
  564.  
  565.  
  566.  
  567.   Title.setBorder(new EmptyBorder(20, 20, 20, 20));
  568.  
  569.  
  570.   Enter = new JButton();
  571.  
  572.   Enter.setText("Singleplayer");
  573.   Enter.setHorizontalAlignment(SwingConstants.CENTER);
  574.   Enter.setBackground(Color.RED);
  575.   Enter.setOpaque(false);
  576.   Enter.setMaximumSize(new Dimension(170+250,200));
  577.   Enter.setHorizontalAlignment(SwingConstants.CENTER);
  578.   Enter.setAlignmentX(CENTER_ALIGNMENT);
  579.   Enter.setBorderPainted(false);
  580.   Enter.setBackground(new Color(255,255,255));
  581.   Enter.setFocusable(false);
  582.   Enter.setContentAreaFilled(false);
  583.  
  584.  
  585.   Enter2 = new JButton();
  586.  
  587.   Enter2.setText("Multiplayer");
  588.   Enter2.setHorizontalAlignment(SwingConstants.CENTER);
  589.   Enter2.setBackground(Color.RED);
  590.   Enter2.setOpaque(false);
  591.   Enter2.setMaximumSize(new Dimension(170+250,200));
  592.   Enter2.setHorizontalAlignment(SwingConstants.CENTER);
  593.   Enter2.setAlignmentX(CENTER_ALIGNMENT);
  594.   Enter2.setBorderPainted(false);
  595.   Enter2.setBackground(new Color(255,255,255));
  596.   Enter2.setFocusable(false);
  597.   Enter2.setContentAreaFilled(false);
  598.  
  599.  
  600.  
  601.   Settings = new JButton();
  602.   Settings.setText("Settings");
  603.   Settings.setHorizontalAlignment(SwingConstants.CENTER);
  604.   Settings.setBackground(Color.RED);
  605.   Settings.setOpaque(false);
  606.   Settings.setMaximumSize(new Dimension(170+250,200));
  607.   Settings.setHorizontalAlignment(SwingConstants.CENTER);
  608.   Settings.setAlignmentX(CENTER_ALIGNMENT);
  609.   Settings.setBorderPainted(false);
  610.   Settings.setForeground(Color.BLACK);
  611.   Settings.setBackground(new Color(255,255,255));
  612.   Settings.setFocusable(false);
  613.   Settings.setContentAreaFilled(false);
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.   SetFont(Enter,"Cardinal",64);
  622.   SetFont(Enter2,"Cardinal",64);
  623.   SetFont(Settings,"Cardinal",64);
  624.  
  625.   UIMouseListener OnHoverEnter = new UIMouseListener(this,MenuPane,Enter,TitleGap);
  626.   UIMouseListener OnHoverEnter2 = new UIMouseListener(this,MenuPane,Enter2,TitleGap);
  627.   UIMouseListener OnHoverSettings = new UIMouseListener(this,MenuPane,Settings,TitleGap);
  628.  
  629.   Font Cardinal104 = new Font("Cardinal",Font.PLAIN,104);
  630.  
  631.  
  632.  
  633.  
  634.  
  635.   File GameIcon = new File(GameMain.ImageAssets+"GameIcon.png");
  636.   Icon icon = new ImageIcon(GameIcon.getPath());
  637.   Title.setIcon(icon);
  638.  
  639.   File GameWindowIconFile  = new File(GameMain.ImageAssets+"GameWindowIcon.png"); // Window Icon
  640.   Image GameWindowIcon = new ImageIcon(GameWindowIconFile.getPath()).getImage();
  641.  
  642.  
  643.  
  644.  
  645.  
  646.   MenuPane.add(Title);
  647.   MenuPane.add(TitleGap);
  648.   MenuPane.add(Enter);
  649.   MenuPane.add(Enter2);
  650.   MenuPane.add(Settings);
  651.  
  652.  
  653.  
  654.    
  655.  
  656.  
  657.   this.setIconImage(GameWindowIcon);
  658.   this.add(WindowPanel);
  659.  
  660.  
  661.  }
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement