Problem with Repaint()

Hi, I am reletively new to Java programming. I am trying too make a game with a few people, and we have a minor problem. When I run this java applet, I get a horrible white line in our images when I repaint. I know there has to be a way to fix this, because it makes it look so choppy. Is there any way that you could tell me what I need to do? I would appreciate that so much.

import java.awt.*;

import java.applet.Applet;

import java.awt.event.*;

public class Picture extends Applet implements KeyListener

{

private Image sonic[] = new Image[22];

private int X = 0, Y = 300; /* Initializing the variables for the X, and Y coordinates

of the images*/

int i = 0;

int s = 1;

int t = 2;

int facing = 0; // 0 is facing right, and 1 is facing left.

public void init()

{

sonic[0] = getImage(getDocumentBase(), "Sonic_Static_Right.gif");

sonic[1] = getImage(getDocumentBase(), "Sonic_Static_Left.gif");

sonic[2] = getImage(getDocumentBase(), "Sonic_Run_Right_1.gif");

sonic[3] = getImage(getDocumentBase(), "Sonic_Run_Right_2.gif");

sonic[4] = getImage(getDocumentBase(), "Sonic_Run_Right_3.gif");

sonic[5] = getImage(getDocumentBase(), "Sonic_Run_Right_4.gif");

sonic[6] = getImage(getDocumentBase(), "Sonic_Run_Right_5.gif");

sonic[7] = getImage(getDocumentBase(), "Sonic_Run_Right_6.gif");

sonic[8] = getImage(getDocumentBase(), "Sonic_Run_Right_7.gif");

sonic[9] = getImage(getDocumentBase(), "Sonic_Run_Right_8.gif");

sonic[10] = getImage(getDocumentBase(), "Sonic_Run_Left_1.gif");

sonic[11] = getImage(getDocumentBase(), "Sonic_Run_Left_2.gif");

sonic[12] = getImage(getDocumentBase(), "Sonic_Run_Left_3.gif");

sonic[13] = getImage(getDocumentBase(), "Sonic_Run_Left_4.gif");

sonic[14] = getImage(getDocumentBase(), "Sonic_Run_Left_5.gif");

sonic[15] = getImage(getDocumentBase(), "Sonic_Run_Left_6.gif");

sonic[16] = getImage(getDocumentBase(), "Sonic_Run_Left_7.gif");

sonic[17] = getImage(getDocumentBase(), "Sonic_Run_Left_8.gif");

sonic[18] = getImage(getDocumentBase(), "Sonic_Right_Duck.gif");

sonic[19] = getImage(getDocumentBase(), "Sonic_Left_Duck.gif");

sonic[20] = getImage(getDocumentBase(), "Sonic_Right_Look.gif");

sonic[21] = getImage(getDocumentBase(), "Sonic_Left_Look.gif");

addKeyListener(this);

requestFocus();

}

public void keyTyped(KeyEvent e)

{

}

public void keyPressed(KeyEvent e)

{

if(e.getKeyCode() == KeyEvent.VK_LEFT)

{

X = X - 5;

if(s == 1)

{

i = 10;

i++;

repaint();

}

if(t % 2 == 0)

{

if(i == 17)

{

i = 10;

}

s++;

i++;

}

t++;

}

if(e.getKeyCode() == KeyEvent.VK_RIGHT)

{

X = X + 5;

if(s == 1)

{

i = 2;

i++;

repaint();

}

if(t % 2 == 0)

{

if(i == 9)

{

i = 2;

}

s++;

i++;

}

t++;

}

if(e.getKeyCode() == KeyEvent.VK_DOWN)

{

if (facing == 0)

i = 18;

if (facing == 1)

i = 19;

}

if(e.getKeyCode() == KeyEvent.VK_UP)

{

if (facing == 0)

i = 20;

if (facing == 1)

i = 21;

}

repaint();

}

public void keyReleased(KeyEvent e)

{

if(e.getKeyCode() == KeyEvent.VK_RIGHT)

{

i = 0;

t = 2;

s = 1;

facing = 0;

repaint();

}

if(e.getKeyCode() == KeyEvent.VK_LEFT)

{

i = 1;

t = 2;

s = 1;

facing = 1;

repaint();

}

if(e.getKeyCode() == KeyEvent.VK_UP)

{

if (facing == 0)

i = 0;

if (facing == 1)

i = 1;

repaint();

}

if(e.getKeyCode() == KeyEvent.VK_DOWN)

{

if (facing == 0)

i = 0;

if (facing == 1)

i = 1;

repaint();

}

}

public void paint(Graphics g)

{

g.drawImage(sonic, X, Y, 40, 50, this);

}

}

[4222 byte] By [Kurasua] at [2007-10-2 11:11:39]
# 1

I have not built or run your code, but I think the problem is that you are drawing directly to the buffer that gets displayed to the screen. Read this:

http://java.sun.com/docs/books/tutorial/extra/fullscreen/doublebuf.html

I have never written an Applet before, and I mostly work with swing stuff, so I probably am not qualified to give you much advice beyond that.

logic_bomba at 2007-7-13 3:54:54 > top of Java-index,Other Topics,Java Game Development...
# 2

I have not worked with applets much either (usually I write my games directly into a Frame) but this is how I do it (for my Frame):

// class constructor, or when initialized, or whatever

public GameFrame()

{

//super... whatever

createBufferStrategy(2);

// everything else...

}

public void paint(Graphics g)

{

Graphics buffer = getBufferStrategy().getDrawGraphics();

// paint everything with buffer... buffer.drawImage(), etc.

buffer.show();

}

I believe thats what it is... also, I use "active" rendering rather than "passive" rendering; what that means is I actually never call "repaint()". Instead, I draw everything in a separate method that gets the Graphics object by using getBufferStrategy().getDrawGraphics() .

Just checked that API, and Applets do not use BufferStrategies... So, the previous technique can not directly be ported into your program, so you have to do this the old fashion way

BufferedImage buffer = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

//paint method of your app

public void paint(Graphics g)

{

Graphics buffer = this.buffer.createGraphics();

// render everything with buffer

g.drawImage(this.buffer,0,0,null);

}

That should handle it, thought not through VolatileImages, which are probably more efficient....

Anyway, what the two previous snippets of code do is render everything onto one image, then draw that image all at once, making the white streaks of line not run through as each element of the screen is drawn.

I did not compile the previous examples, so post if you can't get it to work =D

Alex

aRyan316a at 2007-7-13 3:54:54 > top of Java-index,Other Topics,Java Game Development...
# 3

Down below the line is what I originally wrote. However, just before sending it, I realized that you have done something misleading. Your paint(...) method is shown as calling

g.drawImage(sonic, X, Y, 40, 50, this);

sonic is an array. (Actually, a java.awt.Image[]).

However, Graphics.drawImage(...) is documented as taking an Image as its first parameter, NOT an array! The code that you presented does not compile, so what are you really doing?

=======================================================

1) In the future, please use code tags around your code. There should be a "code" button on the forum web page when you are typing your message to provide the tags.

2) Your applet is relatively simple and straightforward. I don't believe that you should be having this problem with the code that you presented.

3) What is your environment? That is:

What OS?

What version of Java?

What browser?

What video hardware?

4) Have you tried this on a machine with different hardware and/or different software?

5) Stupid question: I assume that you have checked your images and that they don't contain the horrible white line?

6) What happens if you replace

g.drawImage(sonic, X, Y, 40, 50, this);

with:

g.setColor(Color.RED);

g.fillRect( X, Y, 40, 50);

Ian_Shefa at 2007-7-13 3:54:54 > top of Java-index,Other Topics,Java Game Development...
# 4

Hello,

im also trying to make my first program and im having the same problem (amongst others, see my own thread) of the white streaks across the screen.

im not using any applet technology and i was already using bufferedimages draw the whole screen.

i found my problem was that when i call repaint, my screen gets automaticly completely cleared, without even starting to draw inside my buffer (which shouldnt affect that anyway, but you never know). i found this out by using

public void paint(Graphics g){

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

and this is followed by the rest of the code. im completely sure i dont call any clearscreen or clearrect myself.

anyone see the problem

thx anyway

Seahadesa at 2007-7-13 3:54:54 > top of Java-index,Other Topics,Java Game Development...
# 5
See my reply in this thread, http://forum.java.sun.com/thread.jspa?threadID=708859
itchyscratchya at 2007-7-13 3:54:54 > top of Java-index,Other Topics,Java Game Development...
# 6
thx a lot
Seahadesa at 2007-7-13 3:54:54 > top of Java-index,Other Topics,Java Game Development...