feat: day 1 progress
This commit is contained in:
116
src/main/java/eu/midnightdust/yaytris/ui/TetrisUI.java
Normal file
116
src/main/java/eu/midnightdust/yaytris/ui/TetrisUI.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package eu.midnightdust.yaytris.ui;
|
||||
|
||||
import eu.midnightdust.yaytris.game.Space;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.LineBorder;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.io.IOException;
|
||||
|
||||
import static eu.midnightdust.yaytris.Settings.guiScale;
|
||||
|
||||
public class TetrisUI extends JFrame {
|
||||
JLabel titleLabel;
|
||||
GameCanvas gamePanel;
|
||||
JPanel menuPanel;
|
||||
|
||||
public TetrisUI() {
|
||||
Space space = new Space();
|
||||
this.setLayout(null);
|
||||
this.setTitle("Tetris");
|
||||
this.setSize((int) (400 * guiScale), (int) (300 * guiScale));
|
||||
this.setResizable(false);
|
||||
this.getContentPane().setBackground(Color.DARK_GRAY);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setWindowPosition(this, 0);
|
||||
|
||||
titleLabel = new JLabel("Tetris");
|
||||
titleLabel.setForeground(Color.WHITE);
|
||||
Image titleImage;
|
||||
try {
|
||||
titleImage = ImageIO.read(this.getClass().getResourceAsStream("/textures/logo.png"));
|
||||
} catch (IOException | NullPointerException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
titleLabel = new JLabel();
|
||||
titleLabel.setIcon(new ImageIcon(new ImageIcon(titleImage).getImage().getScaledInstance(scale(110), scale(30), Image.SCALE_DEFAULT)));
|
||||
this.add(titleLabel);
|
||||
|
||||
gamePanel = new GameCanvas(this);
|
||||
gamePanel.setBackground(Color.BLACK);
|
||||
gamePanel.setBorder(new LineBorder(Color.GRAY, scale(2)));
|
||||
this.add(gamePanel);
|
||||
|
||||
rescale();
|
||||
openMainMenu(null);
|
||||
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
private void rescale() {
|
||||
this.setSize((int) (400 * guiScale), (int) (300 * guiScale));
|
||||
titleLabel.setBounds(scale(225), scale(7), scale(110), scale(30));
|
||||
gamePanel.setBounds(scale(10), scale(10), scale(150), scale(260));
|
||||
for (Component comp : this.getComponents()){
|
||||
if (comp instanceof JComponent) setFontScale((JComponent) comp);
|
||||
}
|
||||
}
|
||||
|
||||
public static int scale(int bound) {
|
||||
return (int) (bound * guiScale);
|
||||
}
|
||||
public static void setFontScale(JComponent label) {
|
||||
//if (label.getFont() != null) label.setFont(label.getFont().deriveFont((float) label.getFont().getSize() * guiScale));
|
||||
}
|
||||
|
||||
public void openMainMenu(ActionEvent actionEvent) {
|
||||
if (this.menuPanel != null) this.remove(menuPanel);
|
||||
rescale();
|
||||
menuPanel = new MainMenu(scale(170), scale(40), scale(220), scale(230), this);
|
||||
menuPanel.setBackground(Color.DARK_GRAY);
|
||||
menuPanel.setBorder(new LineBorder(Color.GRAY, scale(2)));
|
||||
this.add(menuPanel);
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public void openSettings(ActionEvent actionEvent) {
|
||||
if (this.menuPanel != null) this.remove(menuPanel);
|
||||
menuPanel = new SettingsMenu(scale(170), scale(40), scale(220), scale(230), this);
|
||||
menuPanel.setBackground(Color.DARK_GRAY);
|
||||
menuPanel.setBorder(new LineBorder(Color.GRAY, scale(2)));
|
||||
this.add(menuPanel);
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
// Source: https://stackoverflow.com/a/19746437
|
||||
private void setWindowPosition(JFrame window, int screen)
|
||||
{
|
||||
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
GraphicsDevice[] allDevices = env.getScreenDevices();
|
||||
int topLeftX, topLeftY, screenX, screenY, windowPosX, windowPosY;
|
||||
|
||||
if (screen < allDevices.length && screen > -1)
|
||||
{
|
||||
topLeftX = allDevices[screen].getDefaultConfiguration().getBounds().x;
|
||||
topLeftY = allDevices[screen].getDefaultConfiguration().getBounds().y;
|
||||
|
||||
screenX = allDevices[screen].getDefaultConfiguration().getBounds().width;
|
||||
screenY = allDevices[screen].getDefaultConfiguration().getBounds().height;
|
||||
}
|
||||
else
|
||||
{
|
||||
topLeftX = allDevices[0].getDefaultConfiguration().getBounds().x;
|
||||
topLeftY = allDevices[0].getDefaultConfiguration().getBounds().y;
|
||||
|
||||
screenX = allDevices[0].getDefaultConfiguration().getBounds().width;
|
||||
screenY = allDevices[0].getDefaultConfiguration().getBounds().height;
|
||||
}
|
||||
|
||||
windowPosX = ((screenX - window.getWidth()) / 2) + topLeftX;
|
||||
windowPosY = ((screenY - window.getHeight()) / 2) + topLeftY;
|
||||
|
||||
window.setLocation(windowPosX, windowPosY);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user