Why is my Java Applet flickering? - java

I am working on a small Java Applet that has 3 balls bounce around on screen. When they collide with the walls they make one sound and when they collide with eachother they make another sound.
My issue is that the balls flicker for some reason and I am not sure why. If someone can explain why they are flickering and how I can go about fixing the issue that would be greatly appreciated.
Here is my code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ass3 extends Applet implements Runnable {
int[] positionX = {0, 0, 0};
int[] positionY = {0, 0, 0};
int[] incrementX = {3, 7, 4};
int[] incrementY = {8, 4, 5};
Thread t;
AudioClip sound;
AudioClip collide;
public void init(){
sound = getAudioClip(getDocumentBase(), "Audio1.au");
collide = getAudioClip(getDocumentBase(), "Audio2.au");
}
public void start() {
t = new Thread(this);
t.start();
}
public void paint(Graphics g) {
g.fillOval(getWidth()-50-positionX[0], getHeight()-50-positionY[0], 50, 50);
g.fillOval(getWidth()-450-positionX[1], getHeight()-450-positionY[1], 50, 50);
g.fillOval(getWidth()-450-positionX[2], getHeight()-50-positionY[2], 50, 50);
}
public void run() {
while (true) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {}
for (int i=0; i < 3; i++) {
positionX[i] += incrementX[i];
positionY[i] += incrementY[i];
}
// ball 1
if (positionX[0] > 500 || positionX[0] < 0) {
incrementX[0] = -incrementX[0];
sound.play();
}
repaint();
if (positionY[0] > 500 || positionY[0] < 0) {
incrementY[0] = -incrementY[0];
sound.play();
}
repaint();
// ball 2
if (positionX[1] > 100 || positionX[1] < -400) {
incrementX[1] = -incrementX[1];
sound.play();
}
repaint();
if (positionY[1] > 100 || positionY[1] < -400) {
incrementY[1] = -incrementY[1];
sound.play();
}
repaint();
// ball 3
if (positionX[2] > 100 || positionX[2] < -400) {
incrementX[2] = -incrementX[2];
sound.play();
}
repaint();
if (positionY[2] > 500 || positionY[2] < 0) {
incrementY[2] = -incrementY[2];
sound.play();
}
repaint();
// collision
if (Math.sqrt(((getWidth()-50-positionX[0])-(getWidth()-450-positionX[1]))*((getWidth()-50-positionX[0])-(getWidth()-450-positionX[1])) + ((getHeight()-50-positionY[0])-(getHeight()-450-positionY[1]))*((getHeight()-50-positionY[0])-(getHeight()-450-positionY[1]))) <= 50) {
incrementX[0] = -incrementX[0];
incrementY[0] = -incrementY[0];
incrementX[1] = -incrementX[1];
incrementY[1] = -incrementY[1];
collide.play();
}
repaint();
if (Math.sqrt(((getWidth()-50-positionX[0])-(getWidth()-450-positionX[2]))*((getWidth()-50-positionX[0])-(getWidth()-450-positionX[2])) + ((getHeight()-50-positionY[0])-(getHeight()-50-positionY[2]))*((getHeight()-50-positionY[0])-(getHeight()-50-positionY[2]))) <= 50) {
incrementX[0] = -incrementX[0];
incrementY[0] = -incrementY[0];
incrementX[2] = -incrementX[2];
incrementY[2] = -incrementY[2];
collide.play();
}
repaint();
if (Math.sqrt(((getWidth()-450-positionX[2])-(getWidth()-450-positionX[1]))*((getWidth()-450-positionX[2])-(getWidth()-450-positionX[1])) + ((getHeight()-50-positionY[2])-(getHeight()-450-positionY[1]))*((getHeight()-50-positionY[2])-(getHeight()-450-positionY[1]))) <= 50) {
incrementX[2] = -incrementX[2];
incrementY[2] = -incrementY[2];
incrementX[1] = -incrementX[1];
incrementY[1] = -incrementY[1];
collide.play();
}
repaint();
}
}
}

Simple, stop calling repaint multiple times. (You're calling it multiple times in your while loop).

Related

android 2d game in java lags the moving objects

So I am trying to make a 2d game using SurfaceView (It draws and moves the objects). The game is almost done but I am facing a major problem which is that my moving objects on the screen lag or get stuck (probably skipping frames). Maybe the canvas.drawBitmap() method is not drawing it fast enough. I have tried many things like accelerating hardware in manifests and using low quality images (which I'm converting to bitmap). And I DO NOT create bitmaps on each frame. I think the problem might be in the draw or update method. here are these methods:
UPDATE:
private void update() {
background1.x -= backGroundSpeed * screenRatioX;
background2.x -= backGroundSpeed * screenRatioX;
if (background1.x + background1.background.getWidth() < 0){
background1.x = screenX;
}
if (background2.x + background2.background.getWidth() < 0){
background2.x = screenX;
}
if (flight.isGoingUp){
flight.y -= flightSpeed * screenRatioY;
}else{
flight.y += flightSpeed * screenRatioY;
}
if (flight.y < 0){
flight.y = 0;
}
if (flight.y >= (screenY - flight.height)){
flight.y = screenY - flight.height;
}
for (Pig pig: pigs){
pig.x -= pig.speed;
if (pig.x + pig.normal_width < 0) {
int bound = (int) (13 * screenRatioX);
pig.speed = random.nextInt(bound);
if (pig.speed < (backGroundSpeed * screenRatioX)+1) {
pig.speed = (int) (backGroundSpeed * screenRatioX)+1;
}
pig.x = screenX*2;
pig.y = random.nextInt(screenY - pig.normal_height);
}
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
if (Rect.intersects(pig.getCollisionShape(), flight.getCollisionShape())){
score++;
pig.x = -500;
if (!prefs.getBoolean("isMute", false)){
soundPool.play(point_sound, 1, 1, 0, 0, 1);
}
}
}
}, 500);
}
redPig.x -= redPig.redSpeed;
if (redPig.x + redPig.red_width < 0) {
int bound = (int) (25 * screenRatioX);
redPig.redSpeed = random.nextInt(bound);
if (redPig.redSpeed < (12 * screenRatioX)) {
redPig.redSpeed = (int) (12 * screenRatioX);
}
redPigDistance = random.nextInt(6);
if (redPigDistance < 2){
redPigDistance = 2;
}
redPig.x = screenX*redPigDistance;
redPig.y = random.nextInt(screenY - redPig.red_height);
}
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
if (Rect.intersects(redPig.getRedCollisionShape(), flight.getCollisionShape())){
isGameOver = true;
if (!prefs.getBoolean("isMute", false)){
soundPool.play(dead_sound, 1, 1, 0, 0, 1);
}
}
}
}, 500);
}
DRAW:
private void draw() {
if (getHolder().getSurface().isValid()){
Canvas canvas = getHolder().lockCanvas();
canvas.drawBitmap(background1.background, background1.x, background1.y, paint);
canvas.drawBitmap(background2.background, background2.x, background2.y, paint);
if (isGameOver){
isPlaying = false;
canvas.drawBitmap(flight.getDead(), flight.x, flight.y, paint);
getHolder().unlockCanvasAndPost(canvas);
saveIfHighScore();
waitBeforeExiting();
return;
}
for (Pig pig : pigs) {
canvas.drawBitmap(pig.getPig(), pig.x, pig.y, paint);
}
canvas.drawBitmap(redPig.getRedPig(), redPig.x, redPig.y, paint);
canvas.drawText("" + score, screenX/2f, (70*screenRatioY), paint);
canvas.drawBitmap(flight.getFlight(), flight.x, flight.y, paint);
getHolder().unlockCanvasAndPost(canvas);
}
}

Keylistener and actionlistener does not work, when calling it into main.

I am trying to make a little mini game, where u can move around. I have written a code, which did work. But when I tried to shorten my code and make it looks nicer, I stumble upon a error I could not solve by myself.
Main:
package eksamenstest;
import javax.swing.JFrame;
public class Eksamenstest extends JFrame {
public Eksamenstest() throws InterruptedException
{
JFrame SOJ = new JFrame("Sword Of Justice");
SOJ.pack();
SOJ.setSize(1000,700);
SOJ.setVisible(true);
SOJ.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SOJ.add(new Grafik());
}
public static void main(String[] args) throws InterruptedException
{
new Eksamenstest();
new Musik();
new SpillerOne();
}
}
Graphics:
package eksamenstest;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class Grafik extends JPanel
{
int Menu = 1;
int Player1Liv = 3;
int Player2Liv = 3;
int DødP1 = 1;
int DødP2 = 1;
int WeaponON2;
int WeaponON1;
int Skjold;
int Skjold1;
int x = 100;
int y = 360;
int x_1 = 820;
int y_1 = 360;
public Grafik(){}
public void paint(Graphics g)
{
//Grafikken af banen
Image Bane2 = Toolkit.getDefaultToolkit().getImage("Bane2.png");
g.drawImage(Bane2, 0, 0, 1000, 800, this);
Image Plank = Toolkit.getDefaultToolkit().getImage("Plank.jpg");
g.drawImage(Plank, 100, 500, 800, 10, this);
Image lava = Toolkit.getDefaultToolkit().getImage("lava.png");
g.drawImage(lava, 0, 520, 1000, 260, this);
if(Menu == 1)
{
Image Menu2 = Toolkit.getDefaultToolkit().getImage("Menu.png");
g.drawImage(Menu2, 100, -30, 900, 700, this);
}
//Player 1 - Grafikken der viser hvordan spilleren står.
if(WeaponON1 == 0){
Image PlayerStå = Toolkit.getDefaultToolkit().getImage("Stå.png");
g.drawImage(PlayerStå, x, y, 80, 140, this);
}
if(WeaponON1 == 1){
Image PlayerSværdOP = Toolkit.getDefaultToolkit().getImage("OP.png");
g.drawImage(PlayerSværdOP, x, y, 80, 140, this);
}
if(WeaponON1 == 2){
Image PlayerSværdFrem = Toolkit.getDefaultToolkit().getImage("Frem.png");
g.drawImage(PlayerSværdFrem, x, y, 80, 140, this);
}
if(Skjold == 1){
Image Player1Skjold = Toolkit.getDefaultToolkit().getImage("Player1Skjold.png");
g.drawImage(Player1Skjold, x, y, 80, 140, this);
}
if(Player1Liv == 0){
Image DødP = Toolkit.getDefaultToolkit().getImage("DødP.png");
g.drawImage(DødP, x, y, 80, 140, this);
}
if(Player2Liv == 0){
Image Jubel = Toolkit.getDefaultToolkit().getImage("Jubel.png");
g.drawImage(Jubel, x, y, 80, 140, this);
}
//Player 2 - Grafikken der viser hvordan spilleren står.
if(WeaponON2 == 0){
Image PlayerStå1 = Toolkit.getDefaultToolkit().getImage("Stå1.png");
g.drawImage(PlayerStå1, x_1, y_1, 80, 140, this);
}
if(WeaponON2 == 1){
Image PlayerSværdOP1 = Toolkit.getDefaultToolkit().getImage("OP1.png");
g.drawImage(PlayerSværdOP1, x_1, y_1, 80, 140, this);
}
if(WeaponON2 == 2){
Image PlayerSværdFrem1 = Toolkit.getDefaultToolkit().getImage("Frem1.png");
g.drawImage(PlayerSværdFrem1, x_1, y_1, 80, 140, this);
}
if(Skjold1 == 1){
Image Player2Skjold = Toolkit.getDefaultToolkit().getImage("Player2Skjold.png");
g.drawImage(Player2Skjold, x_1, y_1, 80, 140, this);
}
if(Player2Liv == 0){
Image DødPA = Toolkit.getDefaultToolkit().getImage("DødP.png");
g.drawImage(DødPA, x_1, y_1, 80, 140, this);
}
if(Player1Liv == 0){
Image Jubel = Toolkit.getDefaultToolkit().getImage("Jubel.png");
g.drawImage(Jubel, x_1, y_1, 80, 140, this);
}
//Health Bars / Stamina / Navne / Win
//Player 1
Image PlayerNavn = Toolkit.getDefaultToolkit().getImage("Player1Navn.png");
g.drawImage(PlayerNavn, 30, 50, 70, 30, this);
if(Player1Liv == 3){
Image Liv100B = Toolkit.getDefaultToolkit().getImage("Liv100B.png");
g.drawImage(Liv100B, 30, 80, 120, 40, this);
}
if(Player1Liv == 2){
Image Liv75B = Toolkit.getDefaultToolkit().getImage("Liv75B.png");
g.drawImage(Liv75B, 30, 80, 120, 40, this);
}
if(Player1Liv == 1){
Image Liv50B = Toolkit.getDefaultToolkit().getImage("Liv25B.png");
g.drawImage(Liv50B, 30, 80, 120, 40, this);
}
if(Player1Liv == 0){
Image Liv0B = Toolkit.getDefaultToolkit().getImage("Liv0B.png");
g.drawImage(Liv0B, 30, 80, 120, 40, this);
DødP1 = 0;
Image Player2Win = Toolkit.getDefaultToolkit().getImage("Player2Wins.png");
g.drawImage(Player2Win, 350, 80, 350, 110, this);
}
// Player 2
Image PlayerNavn1 = Toolkit.getDefaultToolkit().getImage("Player2Navn.png");
g.drawImage(PlayerNavn1, 900, 50, 70, 30,this);
if(Player2Liv == 3){
Image Liv100R = Toolkit.getDefaultToolkit().getImage("Liv100R.png");
g.drawImage(Liv100R, 850, 80, 120, 40, this);
}
if(Player2Liv == 2){
Image Liv75R = Toolkit.getDefaultToolkit().getImage("Liv75R.png");
g.drawImage(Liv75R, 850, 80, 120, 40, this);
}
if(Player2Liv == 1){
Image Liv50R = Toolkit.getDefaultToolkit().getImage("Liv25R.png");
g.drawImage(Liv50R, 850, 80, 120, 40, this);
}
if(Player2Liv == 0){
Image Liv0R = Toolkit.getDefaultToolkit().getImage("Liv0R.png");
g.drawImage(Liv0R, 850, 80, 120, 40, this);
DødP2 = 0;
Image Player1Win = Toolkit.getDefaultToolkit().getImage("Player1Wins.png");
g.drawImage(Player1Win, 350, 80, 350, 110, this);
}
}
}
Player Movement
package eksamenstest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class SpillerOne extends JFrame implements ActionListener, KeyListener {
int x = 100;
int y = 360;
int xHøjre;
int xVenstre;
int DødP1;
int Player1Liv;
int iLava;
int x_1;
int Player2Liv;
int Skjold1;
int WeaponON1;
int Menu = 1;
int SværdTid;
int Sværd;
int Skjold;
public SpillerOne()
{
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
#Override
public void actionPerformed(ActionEvent e)
{
PlayerOneMove();
repaint();
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
//Menu
if(c == KeyEvent.VK_O){
Menu = 1;
}
if(c == KeyEvent.VK_P){
Menu = 0;
}
// Player
if(DødP1 == 1){
if(c == KeyEvent.VK_A){
xVenstre = 1;
WeaponON1 = 0;
Sværd = 0;
}
if(c == KeyEvent.VK_D){
xHøjre = 1;
WeaponON1 = 0;
}
if(WeaponON1 == 1){
if(SværdTid < 3){
if(c == KeyEvent.VK_W){
WeaponON1 = 2;
SværdTid++;
}
}
}
if(c == KeyEvent.VK_S){
WeaponON1 = 1;
Sværd = 1;
}
if(c == KeyEvent.VK_Q){
Skjold = 1;
SværdTid++;
}
}
if(c == KeyEvent.VK_E){
SværdTid--;
WeaponON1 = 0;
}
}
#Override
public void keyReleased(KeyEvent e)
{
int c = e.getKeyCode();
// Spiller 1
if(c == KeyEvent.VK_A){
xVenstre = 0;
}
if(c == KeyEvent.VK_D){
xHøjre = 0;
}
if(c == KeyEvent.VK_Q){
Skjold = 0;
SværdTid = SværdTid;
}
if(c == KeyEvent.VK_W){
if(Sværd == 1)
WeaponON1 = 1;
}
if(c == KeyEvent.VK_E){
SværdTid = SværdTid;
WeaponON1 = 0;
}
}
public void PlayerOneMove()
{
// Spiller et Bevægelse
x = x + xHøjre; //Højre
x = x - xVenstre; //Venstre
if(WeaponON1 == 2){ //Tjekker om spilleren har våbnet fremme og hvis ja, så tjekker den om modspilleren har skjold på, hvis ikke, mister person 2 liv.
if(Skjold1 == 0){
if(x > x_1-80 && x < x_1 + 80){
x = 100;
x_1 = 820;
Player2Liv--;
}
}
if(WeaponON1 == 2){
if(Skjold1 == 1){
if(x > x_1-80 && x < x_1 + 80){
x_1 = x_1 + 60;
}
}
}
}
// Falder ned i lava
if(x < 100 && 100 > x + 80){ //Tjekker om player 1 er i lava ved venstre side.
DødP1 = 0;
Player1Liv = 0;
iLava = 1;
if(y < 500){
y++;
}
}
if(x > 900){ //Tjekker om player 1 er i lava ved højre side.
DødP1 = 0;
Player1Liv = 0;
iLava = 1;
if(y < 500){
y++;
}
}
}
}
I have a feeling that this error is caused because I have not repainted somewhere.
So, a list of issues...
First
There are two JFrames - which one is actually been used for what? As far as I can tell, SpillerOne is never displayed, so there is no possible way for it to be able to react to key events. Equally, your ActionListener doesn't seem to be attached to anything which can actually generate actions.
This also raises a bunch of questions about the relationship between SpillerOne and Grafik. They seem to be sharing variable names, but there is no way for them to be sharing state, so even if the KeyListener worked, Grafik would never reflect the changes in SpillerOnes state
Second
You've implemented your custom painting the wrong way. It's highly discouraged to override paint, instead, you should be overriding paintComponent AND calling super.paintComponent to ensure that requirements of the paint chain are upheld
See Performing Custom Painting and Painting in AWT and Swing for more details
You also seem to be putting a lot of state logic into your Grafik class, this is going to become exponentially more difficult to maintain as the complexity increases. Instead, each distinct operation should be it's own class, focused on performing as few dedicated operations as possible
Thirdly
KeyListener is a poor choice for monitoring for key events. While there are "hacks" that attempt to "solve" the focus related issues with KeyListener, none of them can achieve a reliable result.
If you do any research into KeyListener related issues, you will quickly find that the Key bindings API is often sighted as the most reliable solution to the problem
Overall
You code makes no sense. Why are there two frames? What is the ActionListener for and how is it attached to something that generates actions? Why doesn't the Grafik act as the key/action listener?
I think you have some significant thinking and redesigning to do

JLabel and JLayeredPane - How to display an image over another image?

I try to create a little game in java but I'm in trouble.
When I draw a map, I'm not able to display the characters without overwrite the titlesets of the squares.
My goal is to be able to display many pictures on the same square (like the titleset of the grass, the character and a tree), so I have to deal with the transparency of my pictures (this is not the problem) and the layer (it is the problem).
So how can I display an image on another image?
How can I explain to java that I need to display this image on or under another image?
This is my source code. I don't know is that can help you. Your help can be really helpfull for me if you give me a clue or a function who is able to manage the layers. That is useless to rewrite all the code for me x)
This program is not complete, I use it only for test my program right now. I know that he refresh two times the map so he overwrite the square of the character (and he have many others littles glitchs), but that is not the purpose of my question. I try to done my game by step!
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Window extends Thread
{
private static JFrame window = new JFrame("game");
public void run()
{
Map map = new Map();
Characters characters = new Characters();
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setSize(Settings.sizeX, Settings.sizeY);
window.setLocationRelativeTo(null);
window.setResizable(false);
window.setVisible(true);
map.start();
characters.start();
}
private static void reload() throws Exception
{
SwingUtilities.updateComponentTreeUI(window);
}
private static class Map extends Thread
{
private int numberSquareX = Settings.sizeX / 20 + 1;
private int numberSquareY = Settings.sizeY / 20 + 1;
private JLabel square[][] = new JLabel[numberSquareX][numberSquareY];
public void run()
{
for (int x = 0, y = 0; y < numberSquareY; x++)
{
square[x][y] = new JLabel(new ImageIcon("grass_1.png"));
square[x][y].setBounds(x * 20, y * 20, 20, 20);
window.add(square[x][y]);
if (x == numberSquareX - 1)
{
y++;
x = -1;
}
}
square[numberSquareX - 1][numberSquareY - 1] = new JLabel(new ImageIcon("grass_1.png"));
square[numberSquareX - 1][numberSquareY - 1].setBounds(numberSquareX * 20, numberSquareY * 20, 20, 20);
window.add(square[numberSquareX - 1][numberSquareY - 1]);
try
{
reload();
}
catch (Exception e)
{
}
return;
}
}
private class Characters extends Thread
{
private JLabel square[][] = new JLabel[1][1];
public void run()
{
square[0][0] = new JLabel(new ImageIcon("character_1.png"));
square[0][0].setBounds(Test.posX, Test.posX, 20, 20);
window.add(square[0][0]);
try
{
reload();
}
catch (Exception e)
{
}
return;
}
}
}
I have already find this subjects: How to use JLayered Pane to display an image on top of another image? and this one Best practice for creating a composite image output for Java Swing but they haven't really help me...
I continue to search the answer. If I find it, I will come back for post it here.
Solved.
Thanks to MadProgrammer for his comments.
Render the title map to a BufferedImage, either in it's entirety or based on the available viewable area, which ever is more efficient. Paint this to the screen, then paint your character on top it – MadProgrammer
In 15+ years of professional Java/Swing development, I've never found a need to use SwingUtilities.updateComponentTreeUI(window);, instead, simply call repaint on the component which is responsible for renderer the output, I'm pretty sure, you'll find this more efficient. – MadProgrammer
Swing is also a single threaded environment AND is not thread safe, you should NOT be update the UI from outside of the context of the Event Dispatching Thread, as this will setup a race condition and could result in unwanted and difficult to resolve graphical issues. – MadProgrammer
Hint. JLabel is a descendent of Container, which means it can contain other components ;) – MadProgrammer
Thanks a lot MadProgrammer! So I have replace SwingUtilities.updateComponentTreeUI(window) by window.repaint(). You had right for the Thread safe, my map had some bugs but I wasn't able to find where they was from. And what about the BufferedImage? If I create two BufferedImage, the last one can be automatically on the top of the first one? Or I just want to render the title map to a BufferedImage (so I am limited by 2 layers)? – Celine
It will depend on what it is you want to achieve. Using BufferedImages gives you complete control over the placement of the images and yes, one can be rendered over the other, painting is like a artists canvas, as you add things to it, they are added on top of what is already there, BUT, you might find it easier to add a JLabel to another JLabel - just remember, JLabel doesn't have a layout manager by default – MadProgrammer
Code example:
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Window extends Thread
{
private static JFrame window = new JFrame("game");
private int numberSquareX = Settings.sizeX / 20 + 1;
private int numberSquareY = Settings.sizeY / 20 + 1;
private JLabel titlesetLayer1[][] = new JLabel[numberSquareX][numberSquareY];
private JLabel titlesetLayer2[] = new JLabel[1];
private JLabel titlesetLayer3[] = new JLabel[1];
private JLabel titlesetLayer4[] = new JLabel[0];
private JLabel titlesetLayer5[] = new JLabel[0];
private JLabel characters[] = new JLabel[2];
public void run()
{
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setSize(Settings.sizeX, Settings.sizeY);
window.setLocationRelativeTo(null);
window.setResizable(false);
// draw layer5 (on the layer4)
// draw layer4 (on the layer3)
// draw layer3 (on the characters)
titlesetLayer3[0] = new JLabel(new ImageIcon("tree_1.png"));
titlesetLayer3[0].setBounds(130, 120, 126, 160);
window.add(titlesetLayer3[0]);
// draw the charaters
characters[1] = new JLabel(new ImageIcon("character_1.png"));
characters[1].setBounds(600, 500, 100, 100);
window.add(characters[1]);
characters[0] = new JLabel(new ImageIcon("character_1.png"));
characters[0].setBounds(100, 100, 100, 100);
window.add(characters[0]);
// draw layer2 (under the characters)
titlesetLayer2[0] = new JLabel(new ImageIcon("tree_1.png"));
titlesetLayer2[0].setBounds(570, 400, 126, 160);
window.add(titlesetLayer2[0]);
// draw layer1 (under the layer2)
for (int x = 0, y = 0; y < numberSquareY; x++)
{
titlesetLayer1[x][y] = new JLabel(new ImageIcon("grass_1.png"));
titlesetLayer1[x][y].setBounds(x * 20, y * 20, 20, 20);
window.add(titlesetLayer1[x][y]);
if (x == numberSquareX - 1)
{
y++;
x = -1;
}
}
titlesetLayer1[numberSquareX - 1][numberSquareY - 1] = new JLabel(new ImageIcon("grass_1.png"));
titlesetLayer1[numberSquareX - 1][numberSquareY - 1].setBounds(numberSquareX * 20, numberSquareY * 20, 20, 20);
window.add(titlesetLayer1[numberSquareX - 1][numberSquareY - 1]);
window.setVisible(true);
// window.repaint();
}
}
Screen capture:
1
Another solution is to use JLayeredPane!
JLayeredPane layers = new JLayeredPane();
layers.add(tilesetsUnderCharacter, 0); // Layer 0
layers.add(character, 1); // Layer 1
layers.add(tilesetsOnCharacter, 2); // Layer 2
frame.setContentPane(layers);
Code example:
private void init()
{
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setSize(Settings.getX(), Settings.getY());
frame.setResizable(false);
frame.setLocationRelativeTo(null);
for (int i = 0; i < y ; i++)
{
for (int j = 0; j < x; j++)
{
for (int k = 0; k < tilesetsOnCharactersSize; k++)
{
tilesetsOnCharacters[i][j][k] = new javax.swing.JLabel(new javax.swing.ImageIcon(Resources.getTileset(Maps.getMapTileset(mapNumber, 1, k, i, j))));
tilesetsOnCharacters[i][j][k].setBounds(j * tilesetX, i * tilesetY, tilesetX, tilesetY);
map.add(tilesetsOnCharacters[i][j][k], 4);
}
for (int k = 0; k < tilesetsUnderCharactersSize; k++)
{
tilesetsUnderCharacters[i][j][k] = new javax.swing.JLabel(new javax.swing.ImageIcon(Resources.getTileset(Maps.getMapTileset(mapNumber, 0, k, i, j))));
tilesetsUnderCharacters[i][j][k].setBounds(j * tilesetX, i * tilesetY, tilesetX, tilesetY);
map.add(tilesetsUnderCharacters[i][j][k], 0);
}
for (int k = 0; k < mapAttributeSize; k++)
{
if (Maps.getMapTileset(mapNumber, 2, k, i, j) == 1)
{
blocked[i][j] = true;
}
}
}
}
for (int i = 0; i < charactersNumber; i++)
{
characters[i] = new Character(0, 0, 64, 64, 0, 0, 0, 0, 5);
tilesetsCharacters[i] = new javax.swing.JLabel(new javax.swing.ImageIcon(Characters.getCharacter(characters[i].getCharacterSkin(), characters[i].getDirection())));
tilesetsCharacters[i].setBounds(characters[i].getX(), characters[i].getY(), characters[i].getSizeX(), characters[i].getSizeY());
map.add(tilesetsCharacters[i], 1);
charactersRender[i] = false;
}
frame.addKeyListener(new java.awt.event.KeyAdapter()
{
#Override
public void keyTyped(java.awt.event.KeyEvent keyEvent)
{
}
#Override
public void keyPressed(java.awt.event.KeyEvent keyEvent)
{
if((keyEventInt = keyEvent.getKeyCode()) == java.awt.event.KeyEvent.VK_F)
{
right = true;
}
else if(keyEventInt == java.awt.event.KeyEvent.VK_S)
{
left = true;
}
else if(keyEventInt == java.awt.event.KeyEvent.VK_E)
{
up = true;
}
else if(keyEventInt == java.awt.event.KeyEvent.VK_D)
{
down = true;
}
}
#Override
public void keyReleased(java.awt.event.KeyEvent keyEvent)
{
if((keyEventInt = keyEvent.getKeyCode()) == java.awt.event.KeyEvent.VK_F)
{
right = false;
}
else if(keyEventInt == java.awt.event.KeyEvent.VK_S)
{
left = false;
}
else if(keyEventInt == java.awt.event.KeyEvent.VK_E)
{
up = false;
}
else if(keyEventInt == java.awt.event.KeyEvent.VK_D)
{
down = false;
}
}
});
frame.setContentPane(map);
frame.setVisible(true);
}
private void update()
{
if (exit && characters[0].getX() < x * tilesetX - characters[0].getSizeX() - characters[0].getMovementSpeed() && characters[0].getX() > 0 && characters[0].getY() > 0 && characters[0].getY() < y * tilesetY - characters[0].getSizeY() - characters[0].getMovementSpeed())
{
exit = false;
}
if (right && (exit || (characters[0].getX() < x * tilesetX - characters[0].getSizeX() - characters[0].getMovementSpeed() && !blocked[characters[0].getY() / tilesetY][(characters[0].getX() + characters[0].getSizeX() + characters[0].getMovementSpeed()) / tilesetX] && !blocked[(characters[0].getY() + characters[0].getSizeY()) / tilesetY][(characters[0].getX() + characters[0].getSizeX() + characters[0].getMovementSpeed()) / tilesetX])))
{
characters[0].right();
characters[0].setScaleX(5);
if (allowExitRight && characters[0].getX() > x * tilesetX - characters[0].getSizeX() - characters[0].getMovementSpeed() - 1)
exit = true;
charactersRender[0] = true;
}
if (left && (exit || (characters[0].getX() > 0 && !blocked[characters[0].getY() / tilesetY][(characters[0].getX() - characters[0].getMovementSpeed()) / tilesetX] && !blocked[(characters[0].getY() + characters[0].getSizeY()) / tilesetY][(characters[0].getX() - characters[0].getMovementSpeed()) / tilesetX])))
{
characters[0].left();
characters[0].setScaleX(-3);
if (allowExitLeft && characters[0].getX() <= 0)
exit = true;
charactersRender[0] = true;
}
if (jumped || up && (exit || (characters[0].getY() > 0 && !blocked[(characters[0].getY() - characters[0].getMovementSpeed()) / tilesetY][characters[0].getX() / tilesetX] && !blocked[(characters[0].getY() - characters[0].getMovementSpeed()) / tilesetY][(characters[0].getX() + characters[0].getSizeX()) / tilesetX])))
{
if (!jump)
{
characters[0].up();
characters[0].setScaleY(-3);
if (allowExitUp && characters[0].getY() <= 0)
exit = true;
charactersRender[0] = true;
}
else if (!jumped && !falling)
{
jumpCurrentDuration = jumpDuration;
jumped = true;
}
else if (--jumpCurrentDuration > 0)
{
if (exit || (characters[0].getY() > 0 && !blocked[(characters[0].getY() - characters[0].getMovementSpeed()) / tilesetY][characters[0].getX() / tilesetX] && !blocked[(characters[0].getY() - characters[0].getMovementSpeed()) / tilesetY][(characters[0].getX() + characters[0].getSizeX()) / tilesetX]))
{
characters[0].up();
characters[0].setScaleY(-3);
if (allowExitUp && characters[0].getY() <= 0)
exit = true;
charactersRender[0] = true;
}
}
else
{
jumped = false;
}
}
if (((down && !jumped) || (gravity && !jumped)) && (exit || (characters[0].getY() < y * tilesetY - characters[0].getSizeY() - characters[0].getMovementSpeed() && !blocked[(characters[0].getY() + characters[0].getSizeY() + characters[0].getMovementSpeed()) / tilesetX][characters[0].getX() / tilesetX] && !blocked[(characters[0].getY() + characters[0].getSizeY() + characters[0].getMovementSpeed()) / tilesetY][(characters[0].getX() + characters[0].getSizeX()) / tilesetX])))
{
characters[0].down();
characters[0].setScaleY(5);
if (allowExitDown && characters[0].getY() > y * tilesetY - characters[0].getSizeY() - characters[0].getMovementSpeed())
exit = true;
if (jump)
falling = true;
charactersRender[0] = true;
}
else if (jump)
falling = false;
}
private void render()
{
for (int i = 0; i < charactersNumber; i++)
{
if (charactersRender[i])
{
tilesetsCharacters[i].setIcon(new javax.swing.ImageIcon(Characters.getCharacter(characters[i].getCharacterSkin(), characters[i].getDirection())));
tilesetsCharacters[i].setBounds(characters[i].getX() + characters[i].getScaleX(), characters[i].getY() + characters[i].getScaleY(), characters[i].getSizeX(), characters[i].getSizeY());
charactersRender[i] = false;
}
}
}
Screen Capture:
Edit: I also found a library called Slick2D who work with TiledMapEditor:
http://slick.ninjacave.com/
http://www.mapeditor.org/
How to setup Slick2D: How to install Slick2d?
How to use Slick2D and TiledMapEditor: Slick2D + Tiled cant load map
Where started: https://thejavablog.wordpress.com/2008/06/08/using-slick-2d-to-write-a-game/

Tearing/Artifacting in 2D tile map

first of all thanks for taking the time to read my question!
I'm developing a 2D java game, with a tile based map. When my character moves around,
everthing is fine, although when he moves left, vertical white lines, also know as artifacting/tearing, appear on the screen. Same thing happens when he moves up, though the lines are horizontal and much smaller in width. Oddly enough, this doesn't happen when I move right or down. I have searched around on internet to find a solution, though I haven't encountered anything that fit my problem.
Here's the code, although I've heavily downsized and simplified it for the sake of testing. It can therefore be run without any images. Thank you for any answer you provide!
package adriana;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
/**
*
* #author Christophe
*/
public class Main extends JFrame implements Runnable{
public Image dbImage;
public Graphics dbGraphics;
//Image + Array size
final static int listWidth = 500, listHeight = 500;
//Move Variables
int playerX = 320, playerY = 240, xDirection, yDirection;
//Sprites
BufferedImage spriteSheet;
//Lists for sprite sheet: 1 = STILL; 2 = MOVING_1; 3 = MOVING_2
BufferedImage[] ARCHER_NORTH = new BufferedImage[4];
BufferedImage[] ARCHER_SOUTH = new BufferedImage[4];
BufferedImage[] ARCHER_EAST = new BufferedImage[4];
BufferedImage[] ARCHER_WEST = new BufferedImage[4];
Image[] TILE = new Image[12];
//Animation Variables
int currentFrame = 0, framePeriod = 150;
long frameTicker = 0l;
Boolean still = true;
Boolean MOVING_NORTH = false, MOVING_SOUTH = false, MOVING_EAST = false, MOVING_WEST = false;
BufferedImage player;
//World Tile Variables
//20 X 15 = 300 tiles
Rectangle[][] blocks = new Rectangle[listWidth][listHeight];
int tileX = 250, tileY = 250;
Rectangle playerRect = new Rectangle(playerX + 4,playerY+20,32,20);
//Map Navigation
static final byte PAN_UP = 0, PAN_DOWN = 1, PAN_LEFT = 2, PAN_RIGHT = 3;
final int speed = 8;
public Main(){
this.setTitle("JAVA4K");
this.setSize(640,505);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
addKeyListener(new AL());
for(int y = 0; y < listHeight; y++){
for(int x = 0; x < listWidth; x++){
blocks[x][y] = new Rectangle(x*32-8000, y*32-8000, 32, 32);
}
}
}
//Key Listener
public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e){
int keyInput = e.getKeyCode();
still = false;
if(keyInput == e.VK_LEFT){
navigateMap(PAN_RIGHT);
}if(keyInput == e.VK_RIGHT){
navigateMap(PAN_LEFT);
}if(keyInput == e.VK_UP){
navigateMap(PAN_DOWN);
}if(keyInput == e.VK_DOWN){
navigateMap(PAN_UP);
}
}
public void keyReleased(KeyEvent e){
int keyInput = e.getKeyCode();
setYDirection(0);
setXDirection(0);
if(keyInput == e.VK_LEFT){
}if(keyInput == e.VK_RIGHT){
}if(keyInput == e.VK_UP){
}if(keyInput == e.VK_DOWN){
}
}
}
public void moveMap(){
for(int a = 0; a < 500; a++){
for(int b = 0; b < 500; b++){
if(blocks[a][b] != null){
blocks[a][b].x += xDirection;
blocks[a][b].y += yDirection;
}
}
}
}
public void navigateMap(byte pan){
switch(pan){
default:
System.out.println("Unrecognized pan!");
break;
case PAN_UP:
setYDirection(-1 * speed);
break;
case PAN_DOWN:
setYDirection(+1 * speed);
break;
case PAN_LEFT:
setXDirection(-1 * speed);
break;
case PAN_RIGHT:
setXDirection(+1 * speed);
break;
}
}
public void setXDirection(int xdir){
xDirection = xdir;
if(blocks[0][0] != null) tileX = ((playerRect.x - blocks[0][0].x) / 32)-1;
}
public void setYDirection(int ydir){
yDirection = ydir;
if(blocks[0][0] != null) tileY = ((playerRect.y - blocks[0][0].y) / 32)-1;
}
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbGraphics = dbImage.getGraphics();
paintComponent(dbGraphics);
g.drawImage(dbImage, 0, 25, this);
}
public void paintComponent(Graphics g){
requestFocus();
//Draws tiles and rectangular boundaries for debugging
for(int a = tileX - 18; a < tileX + 20; a++){
for(int b = tileY - 15; b < tileY + 17; b++){
g.setColor(Color.RED);
g.fillRect(blocks[a][b].x, blocks[a][b].y, 32, 32);
g.setColor(Color.BLACK);
g.drawRect(blocks[a][b].x, blocks[a][b].y, 32, 32);
}
}
//Draw player
g.drawRect(playerX, playerY, 40, 40);
repaint();
}
public void run(){
try{
System.out.println("Running");
while(true){
moveMap();
Thread.sleep(13);
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Main main = new Main();
//Threads
Thread thread1 = new Thread(main);
thread1.start();
}
}
You have no synchronization between the thread updating the map and the drawing. So the map can be in inconsistent state during drawing.
A quick fix would be wrapping the map updating and drawing in a synchronized block:
synchronized(blocks) {
// drawing or modification here
}
(or just making the whole methods synchronized)
Also, the other fields (like those modified in the key listener) are also susceptible being in inconsistent state.
There are other problems too:
Don't override paint() of a frame. Instead override paintComponent() of a JPanel. There's no need to create an image at every redraw, the swing painting mechanism is by default double buffered. See custom painting in swing.
Use KeyBindings instead of a KeyListener
Swing components must be accessed (and created) only in the event dispatch thread.

Multithreading in java

I want to use 4 threads in my applet and used Runnable interface wants to move all the threads around required position.
When in my applet, clouds image walks from o to 750 at y axis and the helicopter starts walking when clouds comes at 150 in y axis and helicopter walks upto it reaches to the 350 and then this thread stops.
And then when my helicopter reaches to the 200 then a man image comes out and walks to the x axis, it will stop when it has walked 5 to 10 milliseconds.
following is my code:
import java.applet.* ;
package com.pack;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class HelicopterScene extends Applet {
Image a, b, c;
int i, j, h, p;
public void init() {
i = 20;
j = 750;
h = 0;
a = getImage(getCodeBase(), "HelicopterAttack.jpg");
b = getImage(getCodeBase(), "pppp.png");
c = getImage(getCodeBase(), "helicopter1.png");
}
public void paint(Graphics g) {
showStatus(" Helicopter Scene Applet is started.....");
g.drawImage(a, 0, 0, this);
if (i <= 750 && j >= 20) {
if (i >= 150) {
g.drawImage(c, h, 255, 150, 35, this);
h++;
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException w) {
}
}
g.drawImage(b, j, 120, 90, 70, this);
g.drawImage(b, i, 180, 120, 70, this);
i++;
j--;
repaint();
try {
Thread.sleep(10);
if (i == 750 && j == 20) {
p = h;
g.drawImage(c, p, 255, 150, 35, this);
h++;
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException w) {
}
i = 20;
j = 750;
}
} catch (InterruptedException e) {
}
}
}
}
First of all, you never want to sleep on the UI thread. Second, you never want to paint off the UI thread. You should investigate SwingUtilities.invokeLater().

Categories

Resources