0% found this document useful (0 votes)
30 views4 pages

KPop Game: Java Swing Implementation

KPopGame is a Java Swing application that implements a simple game where players click on a moving target to score points. The game features sound effects for hits, misses, and bonuses, and uses timers to control the visibility of the target. The score increases with successful hits, and players can earn bonus points at certain milestones.

Uploaded by

vcd5ycp2tb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

KPop Game: Java Swing Implementation

KPopGame is a Java Swing application that implements a simple game where players click on a moving target to score points. The game features sound effects for hits, misses, and bonuses, and uses timers to control the visibility of the target. The score increases with successful hits, and players can earn bonus points at certain milestones.

Uploaded by

vcd5ycp2tb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

AudioInputStream;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class KPopGame extends JFrame {


private MyPanel gamePanel;
private JLabel scoreLabel;
private int score = 0;
private boolean visible = false;
private Color currentColor = [Link];
private int x, y;
private Font font;
private Random rand = new Random();
private Timer showTimer, delayTimer, popTimer;
private Clip hitClip, missClip, popClip, bonusClip;

public KPopGame() {
setTitle("K Pop Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

scoreLabel = new JLabel("Score: 0", [Link]);


add(scoreLabel, [Link]);

gamePanel = new MyPanel();


add(gamePanel, [Link]);

font = new Font("Arial", [Link], 100);

initTimers();
initSounds();

pack();
setSize(800, 650);
setVisible(true);

show(); // Start the game


}

private void initTimers() {


showTimer = new Timer(1000, e -> {
[Link]();
if (visible) {
playMiss();
}
[Link]();
});
[Link](false);
delayTimer = new Timer(250, e -> {
[Link]();
show();
});
[Link](false);

popTimer = new Timer(200, e -> {


[Link]();
hide();
});
[Link](false);
}

private void initSounds() {


try {
hitClip = [Link]();
[Link]([Link](new File("[Link]")));

missClip = [Link]();
[Link]([Link](new File("[Link]")));

popClip = [Link]();
[Link]([Link](new File("[Link]")));

bonusClip = [Link]();
[Link]([Link](new File("[Link]")));
} catch (IOException | LineUnavailableException |
UnsupportedAudioFileException ex) {
[Link]("Could not load sounds: " + [Link]());
// Continue without sounds
}
}

private void playHit() {


if (hitClip != null && ![Link]()) {
[Link](0);
[Link]();
}
}

private void playMiss() {


if (missClip != null && ![Link]()) {
[Link](0);
[Link]();
}
}

private void playPop() {


if (popClip != null && ![Link]()) {
[Link](0);
[Link]();
}
}

private void playBonus() {


if (bonusClip != null && ![Link]()) {
[Link](0);
[Link]();
}
}

private void show() {


visible = true;
currentColor = [Link];
x = [Link]([Link]() - 100);
y = [Link]([Link]() - 100) + 50;
[Link]();
[Link]();
}

private void hide() {


visible = false;
[Link]();
[Link]();
}

private void hit() {


[Link]();
currentColor = [Link];
[Link]();
playPop();
playHit();
score += 100;
if (score % 500 == 0) {
score += 1000;
playBonus();
}
[Link]("Score: " + score);
[Link]();
}

private class MyPanel extends JPanel implements MouseListener {


public MyPanel() {
addMouseListener(this);
setPreferredSize(new Dimension(800, 600));
setBackground([Link]);
}

@Override
protected void paintComponent(Graphics g) {
[Link](g);
if (visible) {
Graphics2D g2d = (Graphics2D) g;
[Link](font);
[Link](currentColor);
[Link]("k", x, y);
}
}

@Override
public void mouseClicked(MouseEvent e) {
if (!visible) return;
FontMetrics fm = getFontMetrics(font);
int strW = [Link]("k");
int ascent = [Link]();
int descent = [Link]();
int clickX = [Link]();
int clickY = [Link]();
if (clickX >= x && clickX < x + strW &&
clickY >= y - ascent && clickY < y + descent) {
hit();
}
}

@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}

public static void main(String[] args) {


[Link](KPopGame::new);
}
}

You might also like