Agree, Swing sucks, but still, it’s the standard library, so I’m just trying to learn the basics before moving on.
I finished my example, I modded the tutorial I posted, and another one too.
Here are my 2 classes. Do you know of a (quick) way to fix the flickering when resizing the window?
Because it look really awful, and I don’t want to force a fixed size.
Here’s my whole Eclipse project.
BTW I guess I also need to interrupt the thread when the frame closes, but fixing the resize takes priority now
package sandbox;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Transparency;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyGame {
private static final int FRAME_DELAY = 20; // 20ms. implies 50fps (1000/20) = 50
public MyGame() {
System.out.println(this.getClass().getClassLoader().getResource("resources/smile.gif"));
JFrame frame = new JFrame();
JPanel myPanel = new JPanel();
Canvas myCanvas = new Canvas();
// myCanvas.setIgnoreRepaint(true); // Doesn't seem to work at all
BorderLayout myBLayout = new BorderLayout();
myPanel.setLayout(myBLayout);
myPanel.add(myCanvas, BorderLayout.CENTER);
myPanel.add(new JButton("Clicky"), BorderLayout.EAST);
frame.setContentPane(myPanel);
frame.setSize(500, 500);
frame.setVisible(true); // start AWT painting.
Thread gameThread = new Thread(new GameLoop(myCanvas));
gameThread.setPriority(Thread.MIN_PRIORITY);
gameThread.start(); // start Game processing.
}
public static void main(String[] args) {
MyGame myGame = new MyGame();
}
private static class GameLoop implements Runnable {
boolean isRunning;
Canvas myCanvas;
long cycleTime;
int moveX = 0;
int moveY = 0;
ImageLoader myImageLoader = new ImageLoader(null);
BufferedImage myImage = myImageLoader.loadImage("resources/smile.gif", Transparency.TRANSLUCENT);
public GameLoop(Canvas canvas) {
myCanvas = canvas;
isRunning = true;
}
public void run() {
cycleTime = System.currentTimeMillis();
myCanvas.createBufferStrategy(2);
BufferStrategy strategy = myCanvas.getBufferStrategy();
// Game Loop
while (isRunning) {
updateGameState();
updatemyCanvas(strategy);
synchFramerate();
}
}
private void updateGameState() {
// Nothing to do right now
}
private void updatemyCanvas(BufferStrategy strategy) {
Graphics g = strategy.getDrawGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, myCanvas.getWidth(), myCanvas.getHeight());
g.setColor(Color.BLACK);
moveX = (moveX + 8)%myCanvas.getWidth();
moveY = (moveY + 8)%myCanvas.getHeight();
for(int i = 0; i < 5; ++i) {
g.drawImage(myImage, (int) (i*5 + moveX), (int) (i*5 + moveY), null);
}
g.dispose();
strategy.show();
}
private void synchFramerate() {
cycleTime = cycleTime + FRAME_DELAY;
long difference = cycleTime - System.currentTimeMillis();
try {
Thread.sleep(Math.max(0, difference));
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
package sandbox;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Hashtable;
import javax.imageio.ImageIO;
public class ImageLoader {
final GraphicsConfiguration gc;
private Hashtable<String, BufferedImage> chache = new Hashtable<String, BufferedImage>();
public ImageLoader(GraphicsConfiguration gc) {
if (gc == null) {
gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
}
this.gc = gc;
}
public BufferedImage loadImage(String resource, int transparencyType) {
try {
if (chache.get(resource) != null) {
return chache.get(resource);
} else {
URL imageURL = this.getClass().getClassLoader().getResource(resource);
BufferedImage src = ImageIO.read(imageURL);
// Images returned from ImageIO are NOT managedImages
// Therefore, we copy it into a ManagedImage
BufferedImage dst = gc.createCompatibleImage(src.getWidth(),
src.getHeight(), transparencyType);
Graphics2D g2d = dst.createGraphics();
g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(src, 0, 0, null);
g2d.dispose();
chache.put(resource, dst);
return dst;
}
} catch (java.io.IOException e) {
System.err.println("Cannot find resource " + resource);
return null;
}
}
}