Getting Rid of White Line

Hey,I'm making a 2D application and whenever I run it and use input such as the keyboard, a white line goes down the screen and flashes. Any ideas how to stop this?
[179 byte] By [GilGalvantia] at [2007-11-27 7:16:51]
# 1
That's not a whole lot of info to go on. Can you describe the problem in more detail, and post a small amount of code that demonstrates it?
hunter9000a at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 2

Sorry, yeah, here's a code snippet that doesn't use any media. If you move around with the arrow keys you will see a white line flashing around.

import java.awt.*;

import java.applet.*;

import java.awt.MediaTracker;

public class Testing extends Applet

{

MediaTracker tracker = new MediaTracker(this);

Player[] player = new Player[10];

int[] order = new int[10];

House[] house = new House[10];

Shop[] shop = new Shop[10];

Tree[] tree = new Tree[10];

HouseInside[] houseInside = new HouseInside[10];

boolean hasPreLooped = false;

int currentPlayer1 = 0;

int currentPlayer2 = 1;

int topPlayer = 0;

int speed = 10;

int playersInHouse;

int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;

int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;

public static void main( String args[] )

{

}

public void init( Graphics g )

{

}

public void paint( Graphics g )

{

if( hasPreLooped == false )

{

init( g );

preLoop( g );

hasPreLooped = true;

}

drawBackground( g );

checkInside();

//g.drawImage( img, 10, 10, 100, 100, this );

for( int drawHouses = 0; drawHouses <= 1; drawHouses++ )

{

if( house[drawHouses].isInside() == false )

{

house[drawHouses].drawBuilding( g );

}

if( house[drawHouses].isInside() == true )

{

houseInside[drawHouses].drawInside( g );

}

}

shop[0].drawBuilding( g );

for( int drawPlayers = 0; drawPlayers <= 1; drawPlayers++ )

{

if( player[drawPlayers].isInside() == false )

{

player[drawPlayers].drawPlayer( g );

}else{

player[drawPlayers].drawPlayerDown( g );

}

}

for( int drawTrees = 0; drawTrees <= 2; drawTrees++ )

{

tree[drawTrees].drawTree( g );

}

printStatements();

}

public void printStatements()

{

System.out.println( "p1 inside = " + player[0].isInside() );

System.out.println( "p2 inside = " + player[1].isInside() );

System.out.println( "h1 inside = " + house[0].isInside() );

System.out.println( "h2 inside = " + house[1].isInside() );

System.out.println( "house[0].numInside() = " + house[0].numInside() );

System.out.println( "house[1].numInside() = " + house[1].numInside() );

}

public void checkInside()

{

for( int resetHouses = 0; resetHouses <= 1; resetHouses++ )

{

house[resetHouses].setNumInside( 0 );

}

for( int resetPlayers = 0; resetPlayers <= 1; resetPlayers++ )

{

player[resetPlayers].setNumInside( 0 );

}

for( int checkPlayers = 0; checkPlayers <= 1; checkPlayers++ )

{

for( int checkHouses = 0; checkHouses <= 1; checkHouses++ )

{

if( player[checkPlayers].getX() >= house[checkHouses].getX() && player[checkPlayers].getX() <= house[checkHouses].getX() + house[checkHouses].getWidth() && player[checkPlayers].getY() >= house[checkHouses].getY() && player[checkPlayers].getY() <= house[checkHouses].getY() + house[checkHouses].getHeight() )

{

player[checkPlayers].setNumInside( player[checkPlayers].numInside() + 1 );

house[checkHouses].setNumInside( house[checkHouses].numInside() + 1 );

}

}

}

for( int setHouses = 0; setHouses <= 1; setHouses++ )

{

if( house[setHouses].numInside() > 0 )

{

house[setHouses].setIsInside( true );

}else

{

house[setHouses].setIsInside( false );

}

}

for( int setPlayers = 0; setPlayers <= 1; setPlayers++ )

{

if( player[setPlayers].numInside() > 0 )

{

player[setPlayers].setIsInside( true );

}else

{

player[setPlayers].setIsInside( false );

}

}

}

public void preLoop( Graphics g )

{

player[0] = new Player( g, screenWidth / 4 * 1, screenHeight / 2 + 200 );

player[0].setShirt( 0, 0, 200 );

player[0].setPants( 0, 200, 200 );

player[0].setSkin( 250, 250, 200 );

player[0].drawPlayer( g );

player[1] = new Player( g, screenWidth / 4 * 3, screenHeight / 2 );

player[1].setShirt( 200, 0, 0 );

player[1].setPants( 0, 100, 200 );

player[1].setSkin( 200, 200, 100 );

player[1].drawPlayer( g );

house[0] = new House( g, 500, 300, 200, 150 );

house[1] = new House( g, 700, 500, 200, 150 );

tree[0] = new Tree( g, 700, 370 );

tree[1] = new Tree( g, 50, 600 );

tree[2] = new Tree( g, 600, 600 );

shop[0] = new Shop( g, 0, 300, 300, 200 );

houseInside[0] = new HouseInside( g, 500, 300, 200, 150 );

houseInside[1] = new HouseInside( g, 700, 500, 200, 150 );

}

public void drawBackground( Graphics g )

{

g.setColor( Color.green );

g.fillRect( 0, screenHeight / 3, screenWidth, screenHeight / 3 * 2 );

g.setColor( new Color( 220, 255, 255 ) );

g.fillRect( 0, 0, screenWidth, screenHeight / 3 );

}

public boolean mouseDown( Event e, int x, int y )

{

/*

for( int searchSelected = 0; searchSelected <= 1; searchSelected++ )

{

if( x >= player[searchSelected].getX() && x <= player[searchSelected].getX() + 20 )

{

currentPlayer = searchSelected;

}

}

*/

System.out.println( "x = " + x );

System.out.println( "y = " + y );

return true;

}

public boolean keyDown( Event e, int k )

{

if( k == 1007 && player[currentPlayer1].getX() + 20 < screenWidth )

{

player[currentPlayer1].setX( player[currentPlayer1].getX() + speed );

}

if( k == 1004 && player[currentPlayer1].getY() > screenHeight / 3 )

{

player[currentPlayer1].setY( player[currentPlayer1].getY() - speed );

}

if( k == 1005 && player[currentPlayer1].getY() + 120 < screenHeight )

{

player[currentPlayer1].setY( player[currentPlayer1].getY() + speed );

}

if( k == 1006 && player[currentPlayer1].getX() > 0 )

{

player[currentPlayer1].setX( player[currentPlayer1].getX() - speed );

}

if( k == 100 && player[currentPlayer2].getX() + 20 < screenWidth )

{

player[currentPlayer2].setX( player[currentPlayer2].getX() + speed );

}

if( k == 119 && player[currentPlayer2].getY() > screenHeight / 3 )

{

player[currentPlayer2].setY( player[currentPlayer2].getY() - speed );

}

if( k == 115 && player[currentPlayer2].getY() + 120 < screenHeight )

{

player[currentPlayer2].setY( player[currentPlayer2].getY() + speed );

}

if( k == 97 && player[currentPlayer2].getX() > 0 )

{

player[currentPlayer2].setX( player[currentPlayer2].getX() - speed );

}

System.out.println( k );

repaint();

return true;

}

}

class Player

{

int xPos;

int yPos;

int redP;

int greenP;

int blueP;

int redS;

int greenS;

int blueS;

int redH;

int greenH;

int blueH;

int numberInside;

boolean inside;

public Player( Graphics g, int x, int y )

{

xPos = x;

yPos = y;

drawPlayer( g );

}

public void setX( int x )

{

xPos = x;

}

public void setY( int y )

{

yPos = y;

}

public void setPants( int red, int green, int blue )

{

redP = red;

greenP = green;

blueP = blue;

}

public void setShirt( int red, int green, int blue )

{

redS = red;

greenS = green;

blueS = blue;

}

public void setSkin( int red, int green, int blue )

{

redH = red;

greenH = green;

blueH = blue;

}

public int getX()

{

return xPos;

}

public int getY()

{

return yPos;

}

public void drawPlayer( Graphics g )

{

g.setColor( new Color( redP, greenP, blueP ) );

g.fillRect( xPos, yPos, 20, 20 );

g.setColor( new Color( redS, greenS, blueS ) );

g.fillRect( xPos, yPos - 20, 20, 20 );

g.setColor( new Color( redH, greenH, blueH ) );

g.fillRect( xPos, yPos - 40, 20, 20 );

}

public void drawPlayerDown( Graphics g )

{

g.setColor( new Color( redH, greenH, blueH ) );

g.fillRect( xPos, yPos - 40, 20, 20 );

}

public void setIsInside( boolean set )

{

inside = set;

}

public boolean isInside()

{

return inside;

}

public void setNumInside( int set )

{

numberInside = set;

}

public int numInside()

{

return numberInside;

}

}

class House extends Building

{

int bXPos, bYPos, bWidth, bHeight;

boolean someoneInside = false;

int numberInside;

public House( Graphics g, int x, int y, int w, int h )

{

super( g, x, y, w, h );

bXPos = x;

bYPos = y;

bWidth = w;

bHeight = h;

drawBuilding( g );

}

public void drawBuilding( Graphics g )

{

drawFoundation( g );

drawRoof( g, 75, bWidth );

drawDoor( g );

drawWindows( g );

}

public void drawRoof( Graphics g, int height, int width )

{

g.setColor( new Color( 100, 75, 10 ) );

Polygon roof = new Polygon();

roof.addPoint( bXPos, bYPos );

roof.addPoint( bXPos + ( width / 2 ), bYPos - height );

roof.addPoint( bXPos + width, bYPos );

g.fillPolygon( roof );

}

public void drawDoor( Graphics g )

{

g.setColor( new Color( 50, 30, 0 ) );

g.fillRect( bXPos + ( bWidth / 2 ) - 20, bYPos + ( bHeight ) - 80, 40, 80 );

}

public void drawWindows( Graphics g )

{

g.setColor( new Color( 200, 175, 35 ) );

g.fillRect( bXPos + ( bWidth / 2 ) - ( bWidth / 6 ) - 40, bYPos + ( bHeight ) - 80, 40, 40 );

g.fillRect( bXPos + ( bWidth / 2 ) + ( bWidth / 6 ), bYPos + ( bHeight ) - 80, 40, 40 );

}

public void setIsInside( boolean set )

{

someoneInside = set;

}

public boolean isInside()

{

return someoneInside;

}

public void setNumInside( int set )

{

numberInside = set;

}

public int numInside()

{

return numberInside;

}

}

class Shop extends Building

{

int bXPos, bYPos, bWidth, bHeight;

public Shop( Graphics g, int x, int y, int w, int h )

{

super( g, x, y, w, h );

bXPos = x;

bYPos = y;

bWidth = w;

bHeight = h;

drawBuilding( g );

}

public void drawBuilding( Graphics g )

{

drawFoundation( g );

drawWindows( g );

drawDoor( g );

drawRoof( g, 20, bWidth );

}

public void drawWindows( Graphics g )

{

g.setColor( new Color( 200, 175, 35 ) );

g.fillRect( bXPos + ( bWidth / 2 ) - ( bWidth / 4 ) - 40, bYPos + ( bHeight ) - 80, 40, 40 );

g.fillRect( bXPos + ( bWidth / 2 ) + ( bWidth / 4 ), bYPos + ( bHeight ) - 80, 40, 40 );

g.fillRect( bXPos + ( bWidth / 2 ) - ( bWidth / 4 ) - 40, bYPos + 40, 40, 40 );

g.fillRect( bXPos + ( bWidth / 2 ) + ( bWidth / 4 ), bYPos + 40, 40, 40 );

g.fillRect( bXPos + ( bWidth / 2 ) - 20, bYPos + 40, 40, 40 );

}

public void drawDoor( Graphics g )

{

g.setColor( Color.black );

g.drawString( "SHOP", bXPos + ( bWidth / 2 ) - 15, bYPos + bHeight - 90 );

g.setColor( new Color( 50, 30, 0 ) );

g.fillRect( bXPos + ( bWidth / 2 ) - 20, bYPos + ( bHeight ) - 80, 40, 80 );

}

public void drawRoof( Graphics g, int height, int width )

{

g.setColor( new Color( 100, 75, 10 ) );

Polygon roof = new Polygon();

roof.addPoint( bXPos, bYPos );

roof.addPoint( bXPos + ( width / 2 ), bYPos - height );

roof.addPoint( bXPos + width, bYPos );

g.fillPolygon( roof );

}

}

class HouseInside

{

int bXPos, bYPos, bWidth, bHeight;

public HouseInside( Graphics g, int x, int y, int w, int h )

{

bXPos = x;

bYPos = y;

bWidth = w;

bHeight = h;

drawInside( g );

}

public void drawInside( Graphics g )

{

g.setColor( new Color( 150, 150, 50 ) );

g.fillRect( bXPos, bYPos, bWidth, bHeight );

g.setColor( new Color( 100, 100, 100 ));

g.fillRect( bXPos, bYPos, (int)(bWidth / 2.5), bHeight );

g.fillRect( bXPos + bWidth - (int)(bWidth / 2.5), bYPos, (int)(bWidth / 2.5), bHeight );

}

}

class Building

{

int bXPos, bYPos, bWidth, bHeight;

public Building( Graphics g, int x, int y, int w, int h )

{

bXPos = x;

bYPos = y;

bWidth = w;

bHeight = h;

drawFoundation( g );

}

public void drawFoundation( Graphics g )

{

g.setColor( new Color( 115, 100, 25 ) );

g.fillRect( bXPos, bYPos, bWidth, bHeight );

}

public int getX()

{

return bXPos;

}

public int getY()

{

return bYPos;

}

public int getWidth()

{

return bWidth;

}

public int getHeight()

{

return bHeight;

}

}

class Tree

{

int xPos, yPos;

public Tree( Graphics g, int x, int y )

{

xPos = x;

yPos = y;

drawTree( g );

}

public void drawTree( Graphics g )

{

g.setColor( new Color( 100, 50, 20 ) );

g.fillRect( xPos - 15, yPos, 30, 100 );

g.setColor( new Color( 0, 100, 10 ) );

g.fillRect( xPos - 60, yPos - 80, 120, 40 );

g.fillRect( xPos - 20, yPos - 120, 40, 120 );

g.fillRect( xPos - 40, yPos - 100, 80, 80 );

}

}

GilGalvantia at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 3

I believe this is correct advice, but I'm not an applet guy, so I may be wrong:

1) You should use the paintComponent method instead of paint.

2) You should call super.paintComponent as the first line of your paintComponent method.

3) Better yet, do your painting on a child component, like a JPanel, and add that as the context pane of your applet.

http://java.sun.com/docs/books/tutorial/uiswing/painting/problems.html

http://java.sun.com/docs/books/tutorial/uiswing/painting/concepts.html

hunter9000a at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 4
I don't do applets, either, but....Use Swing instead of the old AWT?
Hippolytea at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 5
> I don't do applets, either, but....> > Use Swing instead of the old AWT?Hehe, I didn't notice that he was using awt. You should definitely try to switch to swing, it's much better.
hunter9000a at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 6
Swing rocks. AWT balks.
Hippolytea at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 7
Thanks for the help, everything you're saying is like a different language to me though, lol. What's swing? I'll try looking at those tutorials.
GilGalvantia at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 8
> Thanks for the help, everything you're saying is like> a different language to me though, lol. What's swing?> I'll try looking at those tutorials.Swing is the best thing since sliced awt! http://java.sun.com/docs/books/tutorial/ui/index.html
cotton.ma at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 9
Beware of the SWT cult. It's like Amway and Jehovah's Witnesses rolled in one.
Hippolytea at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 10
Swing looks like it's more for Windows UI and applications, or does it do simple 2D as well?
GilGalvantia at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 11

> Swing looks like it's more for Windows UI and

> applications, or does it do simple 2D as well?

It does everything awt does, and more, and it does it better. After scanning through your code, it looks like the only thing you need to change (besides the things I already suggested) is Applet to JApplet. I don't see any other component in the code.

hunter9000a at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 12

> After scanning through your code, it looks

> like the only thing you need to change (besides the

> things I already suggested) is Applet to JApplet. I

> don't see any other component in the code.

In the fullness of time, one should put the drawing into a custom

component. JApplet is just a root pane container -- it shouldn't be

doing the drawing.

Hippolytea at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 13
Every so often, an old AWT coder emerges,like those Japanese soldiers that were ordered not to surrender,and lived in the Philippine jungles into the 1970's.
Hippolytea at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...
# 14
> Amway and Jehovah's Witnesses rolled in one.Amway's Witnesses? Sounds even worse than people banging on your door and trying to sell you long-term fixed-price natural gas contracts.
DrClapa at 2007-7-12 19:05:40 > top of Java-index,Java Essentials,New To Java...