added a nice highlight when you hover over the planet on the main menu

This commit is contained in:
dvorakmortonxd 2026-10-19 12:35:54 +02:00
commit 501cdba995
2 changed files with 47 additions and 16 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View file

@ -11,23 +11,37 @@ import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.util.Random; import java.util.Random;
public class Game extends JPanel implements ActionListener, KeyListener { public class Game extends JPanel implements ActionListener, KeyListener {
private final int MENU_STAR_COUNT = 75; private final int windowWidth = 1000;
private final int windowHeight = 600;
private final int MENU_STAR_COUNT = 256;
private final int[] starX = new int[MENU_STAR_COUNT]; private final int[] starX = new int[MENU_STAR_COUNT];
private final int[] starY = 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 final Timer timer = new Timer(16, this); // 60 FPS
private Font caveat; private Font caveat;
private int mouseX, mouseY;
private BufferedImage planetZX311; private BufferedImage planetZX311;
private int planetfloat = 0; private BufferedImage planetZX311h;
private String planetfloatDirection = "up"; // this makes the cool floating animation on the main menu private int planetYOffset = 0;
private String planetYOffsetDirection = "up"; // this makes the cool floating animation on the main menu
private int planetX = 580;
private int planetY = 120+ planetYOffset /6;
private int planetSize = 128*3;
private Rectangle planetCollision = new Rectangle(planetX, planetY, planetSize, planetSize);
private boolean hoveringOverPlanet = false;
public Game() { public Game() {
// sets the window size // sets the window size
setPreferredSize(new Dimension(800,600)); setPreferredSize(new Dimension(windowWidth,windowHeight));
// sets the star count for the main menu // sets the star count for the main menu
Random rand = new Random(); Random rand = new Random();
for (int i = 0; i < MENU_STAR_COUNT; i++) { for (int i = 0; i < MENU_STAR_COUNT; i++) {
starX[i] = rand.nextInt(800); starX[i] = rand.nextInt(windowWidth);
starY[i] = rand.nextInt(600); starY[i] = rand.nextInt(windowHeight);
} }
// loads up the fonts // loads up the fonts
try { try {
@ -39,29 +53,42 @@ public class Game extends JPanel implements ActionListener, KeyListener {
// loads up the image assets // loads up the image assets
try { try {
planetZX311 = ImageIO.read(new File("assets/planet-ZX311.png")); planetZX311 = ImageIO.read(new File("assets/planet-ZX311.png"));
planetZX311h = ImageIO.read(new File("assets/planet-ZX311_highlighted.png"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
});
setBackground(Color.BLACK); setBackground(Color.BLACK);
setFocusable(true); setFocusable(true);
addKeyListener(this); addKeyListener(this);
timer.start(); timer.start();
} }
@Override @Override
public void actionPerformed(ActionEvent e) { // update step public void actionPerformed(ActionEvent e) { // update step
if (planetfloatDirection == "up") { hoveringOverPlanet = planetCollision.contains(mouseX, mouseY);
planetfloat++; if (planetYOffsetDirection == "up") {
if (planetfloat > 45) { planetYOffset++;
planetfloatDirection = "down"; if (planetYOffset > 90) {
planetYOffsetDirection = "down";
} }
} else { } else {
planetfloat--; planetYOffset--;
if (planetfloat < 0) { if (planetYOffset <= 0) {
planetfloatDirection = "up"; planetYOffsetDirection = "up";
} }
} }
planetY = 120+ planetYOffset /6;
repaint(); // triggers render repaint(); // triggers render
} }
@ -75,11 +102,15 @@ public class Game extends JPanel implements ActionListener, KeyListener {
} }
g.setColor(Color.GRAY); g.setColor(Color.GRAY);
g.setFont(caveat.deriveFont(Font.BOLD, 72f)); g.setFont(caveat.deriveFont(Font.BOLD, 96f));
g.drawString("Very Cool Space Sim", 120, 80); g.drawString("Very Cool Space Sim", 136, 96);
if (planetZX311 != null) { if (planetZX311 != null) {
g.drawImage(planetZX311, 375, 150+planetfloat/3, 128*3, 128*3, this); if (!hoveringOverPlanet) {
g.drawImage(planetZX311, planetX, planetY, planetSize, planetSize, this);
} else {
g.drawImage(planetZX311h, planetX, planetY, planetSize, planetSize, this);
}
} }
} }