the main menu is finally starting to get its shape

This commit is contained in:
dvorakmortonxd 2026-10-19 09:09:19 +02:00
commit 6f04c40141
6 changed files with 109 additions and 18 deletions

View file

@ -0,0 +1,81 @@
package dev.dvorakmortonxd;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.IOException;
import java.util.Random;
public class Game extends JPanel implements ActionListener, KeyListener {
private final int MENU_STAR_COUNT = 75;
private final int[] starX = new int[MENU_STAR_COUNT];
private final int[] starY = new int[MENU_STAR_COUNT];
private final Timer timer = new Timer(16, this); // 60 FPS
private Font caveat;
public Game() {
// sets the window size
setPreferredSize(new Dimension(800,600));
// sets the star count for the main menu
Random rand = new Random();
for (int i = 0; i < MENU_STAR_COUNT; i++) {
starX[i] = rand.nextInt(800);
starY[i] = rand.nextInt(600);
}
// loads up the font
try {
caveat = Font.createFont(Font.TRUETYPE_FONT, new File("assets/CaveatBrush-Regular.ttf"));
} catch (FontFormatException | IOException e) {
e.printStackTrace();
caveat = new Font("Arial", Font.PLAIN, 12);
}
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) { // update step
repaint(); // triggers render
}
@Override
protected void paintComponent(Graphics g) { // render step
super.paintComponent(g); // this clears the previous frame
g.setColor(Color.WHITE);
for (int i = 0; i < MENU_STAR_COUNT; i++) { // drawing the stars
g.fillOval(starX[i], starY[i], 2, 2);
}
g.setColor(Color.GRAY);
g.setFont(caveat.deriveFont(Font.BOLD, 72f));
g.drawString("Very Cool Space Sim", 38, 80);
}
@Override
public void keyPressed(KeyEvent e) { // input
switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE -> System.exit(0);
}
}
@Override
public void keyReleased(KeyEvent e) { // what happens when you let go of a key
}
@Override public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
JFrame frame = new JFrame("My cool space sim");
frame.setResizable(false); // makes it so gamers cant just resize the window and leave black parts of the window
frame.add(new Game());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // makes the X button on the titlebar functional
frame.setLocationRelativeTo(null); // centers the window on the screen
frame.setVisible(true); // makes the window visible
}
}

View file

@ -1,17 +0,0 @@
package dev.dvorakmortonxd;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}