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