Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Help Me: I don't know whats wrong with my code  (Read 1618 times)
0 Members and 1 Guest are viewing this topic.
Offline pars

Senior Newbie




Java games rock!


« Posted 2003-02-02 18:22:01 »

This is the first time I am using threads.   In my code below, i've tried to use threads with double buffering, and although the code compiles, I see nothing on the screen, can you please help me.

Thanx


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  
76  
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Game extends JPanel implements ActionListener, Runnable
{
      int X = 40,Y = 40;
      private ImageIcon images[];
      private static final int TOTAL_IMAGES=7;
      private int currentImage = 0;
      boolean running = true;
      public static Image offscreen;
     
      public Game()
      {
              setVisible(true);
              setupAnimation();
              setIgnoreRepaint(true);      
      }
     
   
      public void setupAnimation()
      {
            this.setSize(this.getPreferredSize());
            this.images = new ImageIcon[TOTAL_IMAGES];
           
            for (int i = 0; i < images.length; i++)
            {
                  images[i] = new ImageIcon("pac"+i+".gif");
            }
      }
     
      public void run()
      {
            offscreen= createImage(400,400);
              Graphics buff = offscreen.getGraphics();
              buff.setColor(Color.black);
              buff.fillRect(0,0,400,400);
 
            try
            {
                  while (running)
                  {
                        buff.drawImage(images[currentImage].getImage(), X, Y, this);
                        images[currentImage].paintIcon(this, buff, X, Y);
                        currentImage = (++currentImage) % TOTAL_IMAGES;
                        paint(buff);
                        Thread.sleep(30);
                  }
            }
            catch (Exception e) {}
      }
     
      public void paint(Graphics g)
      {
            if (offscreen != null)
            {
                  g.drawImage(offscreen, 0, 0, this);
            }
      }
     
      public void actionPerformed(ActionEvent e){ repaint(); }
      public Dimension getMinimumSize() { return getPreferredSize(); }
      public Dimension getPreferredSize (){return new Dimension(400,400);}
     
       public static void main(String agrs[])
   {
        Game ji = new Game();
        JFrame main = new JFrame("PacMan");
        main.getContentPane().add(ji, BorderLayout.CENTER);
        main.pack();
        main.show();
        Thread t = new Thread(ji);
        t.start();
  }
}
Offline Stuart Still

Senior Newbie





« Reply #1 - Posted 2003-02-02 22:12:08 »

You aren't trying to draw to the JPanel, you are drawing the image to the buffer and not doing anything to the displayed JPanel.  To fix, try doing this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
public void run()
 {
  offscreen= createImage(400,400);
    Graphics buff = offscreen.getGraphics();
    buff.setColor(Color.black);
    buff.fillRect(0,0,400,400);

  try
  {
   while (running)
   {
    buff.drawImage(images[currentImage].getImage(), X, Y, this);
    images[currentImage].paintIcon(this, buff, X, Y);
    buff.drawString("TEST", 30, 30);
    currentImage = (++currentImage) % TOTAL_IMAGES;
    paint(this.getGraphics());
    Thread.sleep(30);
   }
  }
  catch (Exception e) {}
 }


This works because it gets the Graphics object for the JPanel, and therefore renders it to something the user can see, rather than the buffer.  I think that should work, sorry if it does't Smiley

Stuart
Offline pars

Senior Newbie




Java games rock!


« Reply #2 - Posted 2003-02-02 22:38:44 »

Yes it works, Thanks alot

p.s. I just realised that if I use repaint(); it will also work.  But I think I'll stick with what you said.

Thanks again   Smiley  
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Stuart Still

Senior Newbie





« Reply #3 - Posted 2003-02-02 22:40:12 »

repaint() shouldn't work because you are ignoring repaint calls (setIgnoreRepaint(true))!  Correct me if I am wrong Smiley

Stuart
Offline pars

Senior Newbie




Java games rock!


« Reply #4 - Posted 2003-02-02 23:04:10 »

It actually does work.  To be honest, I am also surprised about this. Huh  Could it be because I have put setIgnoreRepaint(true); in the wrong place?  If anyone does know why this happens, then please tell me.
Offline Stuart Still

Senior Newbie





« Reply #5 - Posted 2003-02-03 00:06:55 »

You set it in A correct place (you can call it anytime)!  I am just surprised that it hasn't disabled repaint(), maybe it allows the user to call repaint() manually

Stuart
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #6 - Posted 2003-02-03 17:35:14 »

Quote

public void setIgnoreRepaint(boolean ignoreRepaint)Sets whether or not paint messages received from the operating system should be ignored. This does not affect paint events generated in software by the AWT, unless they are an immediate response to an OS-level paint message.
This is useful, for example, if running under full-screen mode and better performance is desired, or if page-flipping is used as the buffer strategy.


isn't javadoc a wonderful thing Grin

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
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.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!