I used a CardLayout to put two JPanels on top of another for my application.
The first jpanel is of my game, which runs perfectly fine without slowdowns.
On the title screen, however, I am noticing some tearing and I can’t for the life of me figure out why.
The code (as far as I can see) is pretty much almost the same as the game’s in terms of structure.
The moving “FakeBlocks” tear occasionally and the animation is jerky (unlike in the game – which works perfectly).
Does it look like I’m missing an override, or as if I’m skipping / calling the wrong thing?
I’ve been looking at this code for a few hours now.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.*;
import java.io.*;
public class LevelSelect extends JPanel implements MouseMotionListener,MouseListener {
public static Image bufferImage;
public Image background = Toolkit.getDefaultToolkit().getImage("images/misc/background1.png");
public Game GamePanel = new Game();
public int ctr;
private ArrayList a = new ArrayList();
Random rnd = new Random();
FontMetrics fm;
Font cookies;
AnimUpdate update;
public LevelSelect() {
setDoubleBuffered(false);
setIgnoreRepaint(true);
rnd.setSeed(System.currentTimeMillis());
setLayout(new GridLayout());
addMouseListener(this);
addMouseMotionListener(this);
add(GamePanel);
GamePanel.setSize(this.getWidth(),this.getHeight());
GamePanel.setVisible(false);
cookies = GameScreens.cookies.deriveFont((float)24);
fm = getFontMetrics(cookies);
for (int i=0; i<1; i++) {
FakeBlock fb = new FakeBlock();
fb.blockType=rnd.nextInt(8)+1;
fb.speed=(rnd.nextInt(4)+2)*2;
fb.xPos=rnd.nextInt(481);
fb.yPos=15+i*65;//112+i*35;
fb.setImage();
a.add(fb);
}
update = new AnimUpdate();
update.start();
setVisible(true);
}
public void paintComponent(Graphics g) {
Graphics2D g2;
// Create bufferImage & set vars if it/they don't exist
if (bufferImage == null) {
bufferImage = createImage(this.getWidth(),this.getHeight());
g2 = (Graphics2D)bufferImage.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
}
g2 = (Graphics2D)bufferImage.getGraphics();
g2.setFont(cookies);
String str2 = "Click to Continue";
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawImage(GameScreens.background,0,0,null);
AlphaComposite ac;
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float).4);
g2.setComposite(ac);
for (int i=0; i<a.size(); i++) {
FakeBlock fb = (FakeBlock)a.get(i);
fb.move();
g2.drawImage(fb.myPicture,fb.xPos,fb.yPos,null);
}
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)1);
g2.setComposite(ac);
g2.drawImage(GameScreens.titleScr,0,0,null);
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)Math.abs(Math.sin(Math.toRadians(ctr%180))));
g2.setComposite(ac);
g2.drawString(str2,(int)((this.getWidth()-fm.stringWidth(str2))/2.0),320);
g.drawImage(bufferImage,0,0,null);
}
public void update(Graphics g) {
paintImmediately(0,0,480,400);
}
public void paint(Graphics g) {
paintComponent(g);
}
public void mouseDragged(MouseEvent e) {
} //end method mouseDragged
//for when the mouse is just moving
public void mouseMoved(MouseEvent e) {
} //end of method mouseMoved
//for when the mouse button is held down
public void mousePressed(MouseEvent e) {
} //end of method mousePressed
//for when the mouse button is released
public void mouseReleased(MouseEvent e) {
GameScreens.goGameScreen();
} //end of method mouseReleased
//some unused mouse methods
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e) { }
class FakeBlock {
public int blockType;
public AlphaComposite alpha;
public int xPos;
public int yPos;
public double speed;
public double alphaOffset;
public double alphaSpeed;
public Image myPicture;
public FakeBlock() {
}
public void setImage() {
if (blockType == 0) {
myPicture=GameScreens.one;
} else if(blockType ==1) {
myPicture=GameScreens.two;
} else if (blockType == 2) {
myPicture=GameScreens.thr;
} else if (blockType == 3) {
myPicture=GameScreens.fou;
} else if(blockType == 4) {
myPicture=GameScreens.fiv;
} else if (blockType == 5) {
myPicture=GameScreens.six;
} else if (blockType == 6) {
myPicture=GameScreens.sev;
} else if (blockType == 7) {
myPicture=GameScreens.eig;
} else if (blockType == 8) {
myPicture=GameScreens.nin;
}
}
public void paint(Graphics g) {
paintComponent(g);
}
public void move() {
xPos += (int)speed;
if (xPos > 481) {
blockType=rnd.nextInt(8)+1;
speed=(rnd.nextInt(4)+2)*2;
xPos=-32-rnd.nextInt(200);
setImage();
}
}
}
class AnimUpdate extends Thread {
//the method run when .start() is called
public void run() {
while(Thread.currentThread() == update) {
//paint & update the frame number
paintImmediately(0,0,480,400);
++ctr;
try { //sleep try-catch
Thread.sleep(16);
} catch(InterruptedException e) {
System.out.println("InterruptedException @ AnimUpdate");
} //end of try-catch block (sleep)
} //end of while loop
} //end of run() method
} //end of class AnimUpdate
}