Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Drawing images in Jpanel, JFrame and game loop  (Read 635 times)
0 Members and 1 Guest are viewing this topic.
Offline Muteking

Senior Newbie





« Posted 2013-07-18 09:19:16 »

Guys, my first post, here.
I'm learning to program a game. I just want to create a Runnable thread, putting inside the run method a static image.
I just want to understand the little steps that I'm thinking about. I'll deal with fps,doublebuffering and keylistener later.
The problem is that now when I run the program, nothing is displayed.
I have another class, which is just a jframe calling this other class and putting everything inside with setContentPane.
I've also tried with paintcomponent but nothing changes.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
public class myPanel extends JPanel implements Runnable{

//FIELDS
public static int WIDTH = 1024;
public static int HEIGHT = WIDTH / 16 * 9;
private BufferedImage bg;
private BufferedImage charac;
private boolean running;
private Thread t1;
private int startposX = WIDTH / 2;
private int startposY = HEIGHT / 2;
private int cordX = startposX;
private int cordY = startposY;
int speed = 50;


//METHODS  
public synchronized void start (){
    running = true;
    t1 = new Thread (this);
    t1.start();
}

public synchronized void stop (){
    running  = false;
    try {
        t1.join();
        System.out.println("The game stopped");
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
       e.printStackTrace();
              }
 }

 //INIT
public myPanel(){

   setPreferredSize(new Dimension(WIDTH, HEIGHT));
   setFocusable(true);
   requestFocus();
    }

 //MAIN RUN METHOD

 public void run(){
           while (running){
           load();
           System.out.println("The game runs");
           repaint();
          }
 }


  //PAINT WITH GRAPHICS METHOD
 public void paint (Graphics g){
    super.paint(g);
    g.drawImage(bg, 0, 0, this);
    g.drawImage(charac, 110, 280, this);

}

//LOAD IMAGES IN MEMORY
public void load (){
            try {
        String path1 = "res/bg.png";
        bg = ImageIO.read(new File (path1));
        String path2 = "res/charac.png";
        charac = ImageIO.read(new File (path2));
    } catch (IOException e) {

        e.printStackTrace();
    }

}
Offline andybennett
« Reply #1 - Posted 2013-07-18 09:59:21 »

Took your code and tweaked it so that a frame displays with a rectangle displayed in the top left.

If your JFrame is not even appearing make sure you call setVisible(true);

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
public class Example extends JFrame implements Runnable {

    // FIELDS
   public static int WIDTH = 800;
    public static int HEIGHT = 600;

    private boolean running;
    private Thread t1;

    // METHODS
   public synchronized void start() {
        running = true;
        t1 = new Thread(this);
        t1.start();
    }

    public synchronized void stop() {
        running = false;
        try {
            t1.join();
            System.out.println("The game stopped");
        }
        catch (InterruptedException e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
        }
    }

    // INIT
   public Example() {
        setSize(new Dimension(WIDTH, HEIGHT));
        setFocusable(true);
        requestFocus();
       
        setVisible(true);
    }

    // MAIN RUN METHOD

    public void run() {
        while (running) {
            //load();
           System.out.println("The game runs");
            repaint();
        }
    }

    // PAINT WITH GRAPHICS METHOD
   public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, 100, 100);

    }

    // LOAD IMAGES IN MEMORY
//    public void load() {
//        try {
//            String path1 = "res/bg.png";
//            bg = ImageIO.read(new File(path1));
//            String path2 = "res/charac.png";
//            charac = ImageIO.read(new File(path2));
//        }
//        catch (IOException e) {
//
//            e.printStackTrace();
//        }
//
//    }
   
    @SuppressWarnings("unused")
    public static void main(String[] args) {
        Example example = new Example();
    }    
}
Offline Longarmx
« Reply #2 - Posted 2013-07-18 16:17:45 »

Did you make sure that you are adding the Jpanel to your JFrame?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Muteking

Senior Newbie





« Reply #3 - Posted 2013-07-18 22:52:47 »

Thanks Andy and Longarmx.
I did a stupid error in the other class. And more, now I've used paintComponent.
Let me ask you another thing. I don't see flickering in the movement.
Does that mean that I don't need to manually setup double buffering?
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 114
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-07-19 00:46:01 »

JPanel is already double buffered Pointing
Offline ralphchapin

Junior Member


Medals: 3
Projects: 1



« Reply #5 - Posted 2013-07-22 02:36:38 »

You should declare "running" with the "volatile" keyword.  It's the only variable used by both threads, so having done that you can remove "synchronized" from "start" and "stop".  The alternative would be to synchronize the check on "running" in the "run" method, which would be doable but a bit awkward; you don't want to synchronize the whole method or the "stop" method will be unable to execute.

In my experience, leaving out the "volatile" usually works okay.  But sometimes it takes a few tenths of a second before the watching thread notices the change, and your game can get annyoingly sluggish.  Much less often, the thread never sees the change at all and you're wondering why the stupid button won't work all of a sudden.  (Without "volatile" or without all references "synchronized", the JVM is under no obligations to do or not do anything, nor is it under any obligations to be fast or slow about it.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (18 views)
2014-04-15 18:08:23

BurntPizza (15 views)
2014-04-15 03:46:01

UprightPath (28 views)
2014-04-14 17:39:50

UprightPath (13 views)
2014-04-14 17:35:47

Porlus (29 views)
2014-04-14 15:48:38

tom_mai78101 (54 views)
2014-04-10 04:04:31

BurntPizza (111 views)
2014-04-08 23:06:04

tom_mai78101 (212 views)
2014-04-05 13:34:39

trollwarrior1 (181 views)
2014-04-04 12:06:45

CJLetsGame (187 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!