Package vee.ui

Source Code of vee.ui.GamePanel

package vee.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;


public class GamePanel extends JPanel{

  private String projectRoot = "/home/jace/projects/Vee/src";
  private GameFrame gameFrame;
  private Font chancerFont;

  public GamePanel(GameFrame gameFrame) {
    this.gameFrame = gameFrame;
//    this.setSize(1080, 720);
    this.setPreferredSize(new Dimension(1080,720));

  }


  private Font setUIFont(String font) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    File chancerFile = new File(projectRoot+ "/fonts/" + font + ".ttf");
    try {
      chancerFont = Font.createFont(Font.TRUETYPE_FONT, chancerFile).deriveFont(Font.PLAIN, 25);
    } catch (FontFormatException | IOException ex) {
      Logger.getLogger(GamePanel.class.getName()).log(Level.SEVERE, null, ex);
      System.out.println("Can't open font.");
    }
    ge.registerFont(chancerFont);
    return chancerFont;
  }

  private void setBar(Graphics g) {
    g.setColor(Color.WHITE);
    g.setFont(chancerFont);

    String s = "health: "+this.gameFrame.fox.getCurrentHealth();

    //get the string height and width
    int stringLength = (int) g.getFontMetrics().getStringBounds(s, g).getWidth();
    int stringHeight = (int) g.getFontMetrics().getStringBounds(s, g).getHeight();


    // center the string vertically, and shift it to the far left horizontally
    int x = this.getWidth()/10 - stringLength/2;
    int y = this.getHeight()-stringHeight/2;

    g.drawString(s, x, y);

//    g.drawString(s, this.getHeight()/2, this.getWidth()/2);
  }

  @Override
  public void paintComponent(Graphics g){
      super.paintComponent(g);
      g.setColor(Color.BLACK);
      g.fillRect(0, 0, this.getWidth(), this.getHeight());
      this.setUIFont("menlo");
      this.setBar(g);


  }

}
TOP

Related Classes of vee.ui.GamePanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.