Reputation: 13
I am trying to make a 2D game using Java Swing, but I am running into some trouble. I added ZOrder to my JLabels because I wanted them to appear in a certain order, but it caused them to start flickering, I am new to using Java GUI so I'm not too sure what's going on. Here is the code that I wrote:
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.*;
import java.time.Clock;
import javax.imageio.*;
import javax.swing.*;
import java.util.*;
public class World implements KeyListener {
// Creates world.
protected JFrame console = new JFrame("Tower of Fiction");
// Keeps track of all objects in the world.
protected ArrayList<JLabel> displayObjects = new ArrayList<JLabel>();
// A timer used to delay actions.
protected Clock clock = Clock.systemDefaultZone();
// Used to track menu button choice
protected int menuOption = 0;
// To close game.
private boolean close = false;
// For screen 2 activation.
private boolean screen2 = false;
private int ticker2 = 21;
private long delay2 = 0;
public World() throws IOException, FontFormatException {
// Creates a game console using JFrame.
console.setSize(1920, 1080);
console.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
console.setLayout(null);
console.addKeyListener(this);
drawText("Start Game", "BLKCHCRY.TTF", 100f, 201, 193, 59, 750, 440, 1);
drawText("Settings", "BLKCHCRY.TTF", 100f, 201, 193, 59, 828, 605, 1);
displayObjects.get(1).setVisible(false);
drawText("Quit Game", "BLKCHCRY.TTF", 100f, 201, 193, 59, 762, 775, 1);
displayObjects.get(2).setVisible(false);
drawImage("Animations and Images/Images/PNG/Fictionville Home Screen (First Time).png", 0, 0, 1);
console.setVisible(true);
while (close == false) {
if (screen2 == true) {
if (delay2 < clock.millis()) {
drawImage("Animations and Images/Animations/GIF/Load Screen Animation/Loading Screen" + ticker2
+ ".png", 0, 0, 0);
delay2 = clock.millis() + 100;
ticker2--;
if (ticker2 == 0) {
screen2 = false;
}
}
}
console.revalidate();
console.repaint();
}
}
// Adding an image to console.
public void drawImage(BufferedImage image, int xCord, int yCord, int order) {
JLabel asset = new JLabel(new ImageIcon(image));
Dimension size = asset.getPreferredSize();
asset.setBounds(xCord, yCord, size.width, size.height);
console.add(asset);
console.setComponentZOrder(asset, order);
displayObjects.add(asset);
}
// Adding an image to console using file path.
public void drawImage(String imagePath, int xCord, int yCord, int order) throws IOException {
BufferedImage image = ImageIO.read(new File(imagePath));
drawImage(image, xCord, yCord, order);
}
// Draw text on screen.
public void drawText(String text, String fontPath, float size, int R, int G, int B, int xCord, int yCord, int order)
throws FontFormatException, IOException {
JLabel label = new JLabel(text);
Font fontStyle = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream(fontPath)).deriveFont(size);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(fontStyle);
label.setForeground(new java.awt.Color(R, G, B));
label.setFont(fontStyle);
Dimension sizeOfWords = label.getPreferredSize();
label.setBounds(xCord, yCord, sizeOfWords.width, sizeOfWords.height);
console.add(label);
console.setComponentZOrder(label, order);
label.setDoubleBuffered(true);
displayObjects.add(label);
}
// Clears all images on console.
public void clear() {
console.getContentPane().removeAll();
}
// For resizing images.
public BufferedImage resizeImage(BufferedImage image, int width, int length) {
return toBufferedImage(image.getScaledInstance(width, length, Image.SCALE_DEFAULT));
}
public BufferedImage resizeImage(String imagePath, int width, int length) throws IOException {
BufferedImage image = ImageIO.read(new File(imagePath));
return toBufferedImage(image.getScaledInstance(width, length, Image.SCALE_DEFAULT));
}
private static BufferedImage toBufferedImage(Image img) {
if (img instanceof BufferedImage) {
return (BufferedImage) img;
}
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (screen2 == false) {
if (keyCode == KeyEvent.VK_DOWN) {
if (menuOption == 2) {
displayObjects.get(menuOption).setVisible(false);
menuOption = 0;
}
else {
displayObjects.get(menuOption).setVisible(false);
menuOption++;
}
displayObjects.get(menuOption).setVisible(true);
}
else if (keyCode == KeyEvent.VK_UP) {
if (menuOption == 0) {
displayObjects.get(menuOption).setVisible(false);
menuOption = 2;
}
else {
displayObjects.get(menuOption).setVisible(false);
menuOption--;
}
displayObjects.get(menuOption).setVisible(true);
}
if (menuOption == 2 && keyCode == KeyEvent.VK_ENTER) {
close = true;
console.dispose();
}
}
if (menuOption == 0 && keyCode == KeyEvent.VK_SPACE || menuOption == 0 && keyCode == KeyEvent.VK_ENTER) {
screen2 = true;
}
}
// Called when a key is released
@Override
public void keyReleased(KeyEvent e) {
// Handle key release if needed
}
// Called when a key is typed (pressed and released)
@Override
public void keyTyped(KeyEvent e) {
// Handle key typing if needed
}
Upvotes: 1
Views: 55