The Artima Developer Community
Sponsored Link

Java Answers Forum
Flicker with my gif image

2 replies on 1 page. Most recent reply: May 23, 2003 10:46 PM by roar

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
roar

Posts: 4
Nickname: roar
Registered: May, 2003

Flicker with my gif image Posted: May 23, 2003 7:55 AM
Reply to this message Reply
Advertisement
hello i hope anyone will help me in this, i get flicker when adding an gif to a background jpg. Can anyone tell me why r there a flicker an how can i correct it the following is the code

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Testing extends JFrame implements KeyListener
{
JFrame frame;
Image im;
Image panda;
Toolkit tk = Toolkit.getDefaultToolkit();
int pandaX = 100;
int pandaY = 100;

public Testing()
{
setTitle("An Image");
setSize(800,600);
getContentPane().setLayout(null);
addKeyListener(this);

}
public void paint(Graphics g)
{
im = tk.getImage("myImage.jpg");
g.drawImage(im,0,0,this);
panda = tk.getImage("panda.gif");
g.drawImage(panda,pandaX,pandaY,this);

}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode()==37) //left
{
pandaX -= 10;
if (pandaX < 0)
{
pandaX = 0;
}
repaint();
}
else if (e.getKeyCode()==38) //up
{
pandaY -= 10;
if (pandaY < 0)
{
pandaY = 0;
}
repaint();
}
else if (e.getKeyCode()==39) //right
{
pandaX += 10;
if (pandaX > 700)
{
pandaX = 700;
}
repaint();
}
else if (e.getKeyCode()==40) //down
{
pandaY += 10;
if (pandaY > 500)
{
pandaY = 500;
}
repaint();
}

}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public static void main(String args[])
{
Testing test = new Testing();
test.setVisible(true);
}
}


Kamal

Posts: 3
Nickname: kamal079
Registered: May, 2003

Re: Flicker with my gif image Posted: May 23, 2003 11:22 AM
Reply to this message Reply
Hi,
Whenever you call the repaint(), it inturns calls the paint(). So, according to your code, each type you press the arrow keys, the myImage.jpg image gets drawn each time at the specified co-ordinates.

public void paint(Graphics g)
{
im = tk.getImage("myImage.jpg");
g.drawImage(im,0,0,this);
panda = tk.getImage("panda.gif");
g.drawImage(panda,pandaX,pandaY,this);
}

roar

Posts: 4
Nickname: roar
Registered: May, 2003

Re: Flicker with my gif image Posted: May 23, 2003 10:46 PM
Reply to this message Reply
ya, myImage.jpg is my background and panda.gif is the gif tat move according to the direction key on the keyboard, if i did not put the background image , the panda.giff wouldn't flicker.but when i put the background, it does

Flat View: This topic has 2 replies on 1 page
Topic: Java basics Previous Topic   Next Topic Topic: how do i resize applet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use