Why doesn't the panel update unless I drag the edges?
Hai!
Im like total newbie and I'v finaly got a feeling on how to import images =)
Weee, took me days to find a good example that didnt use 1 quadrilion lines to explain.
But anyways, I'v made this gui thing. Just a window and a picture.
Then I got a loop running.
This loop will change the X and Y values and repaint the picture.
But the problem is, you can see that the image is moving unless you resize the window, but the x andy is changing so if u wait a long time, then the picture (when drag the edges) will have moves far down.
So heres my code:
Code:
/**
* @(#)TestingArea.java
*
* TestingArea application
*
* @author
* @version 1.00 2009/11/2
*/
import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class TestingArea {
static Vector <Image> list = new Vector<Image>();
static int X = 0;
static int Y = 0;
public static void main(String[] args) {
// TODO, add your application code
gameFrame gf = new gameFrame();
while (true){
X += 10;
Y += 10;
gf.add(new paintIt());
try{
//do what you want to do before sleeping
Thread.currentThread().sleep(1000);//sleep for 1000 ms
//do what you want to do after sleeptig
} catch(Exception ie){
//If this thread was intrrupted by nother thread
}
}
}
}
class gameFrame extends JFrame{
public gameFrame(){
paintIt p = new paintIt();
add(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,300);
setTitle("Image test");
setVisible(true);
}
}
class paintIt extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
try{
BufferedImage pic = ImageIO.read(new File("C:\\Users\\Andreas\\Bilder\\CNDF.png"));
TestingArea.list.add(pic);
g.drawImage(pic,TestingArea.X,TestingArea.Y,null);
}catch (Exception e){
e.printStackTrace();
}
}
}
Suggestion on ways/patterns to use for making a 2d game is also appriciated =)