This site requires JavaScript, please enable it in your browser!
Greenfoot back
Entity1037
Entity1037 wrote ...

2014/4/30

Method drawImage(image,x,y,ImageObserver); not working.

Entity1037 Entity1037

2014/4/30

#
The drawImage() method in the game I'm making using Eclipse isn't actually doing anything, and I have no idea why. It just ends up being a blank paint window with nothing being painted at all. Is there any reason for this? The problem is on line 106. Here's my GameEngine code:
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import java.util.List;
import java.util.ArrayList;
 
/**
 * Main class for the game
 */
public class GameEngine extends JFrame
{       
    boolean isRunning = true;
    int fps = 30;
    int windowWidth = 500;
    int windowHeight = 500;
     
    BufferedImage backBuffer;
    Insets insets;
    InputHandler input;
     
    int x=windowWidth/2;
    int y=windowHeight/2;
    int xmove=0;
    int ymove=0;
     
    public static void main(String[] args)
    {
            GameEngine game = new GameEngine();
            game.run();
            System.exit(0);
    }
     
    /**
     * This method starts the game and runs it in a loop
     */
    public void run()
    {
        initialize();
         
        while(isRunning)
        {
            long time = System.currentTimeMillis();
             
            update();
            draw();
             
            //  delay for each frame  -   time it took for one frame
            time = (1000 / fps) - (System.currentTimeMillis() - time);
             
            if (time > 0)
            {
                try{Thread.sleep(time);}catch(Exception e){}
            }
        }
                 
        setVisible(false);
    }
         
    /**
     * This method will set up everything need for the game to run
     */
    void initialize()
    {
        setTitle("Game Demo");
        setSize(windowWidth, windowHeight);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
         
        insets = getInsets();
        setSize(insets.left + windowWidth + insets.right,
        insets.top + windowHeight + insets.bottom);
 
        backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
        input = new InputHandler(this);
        objects.add(new Player(100,100));
    }
     
    List<Objects> objects = new ArrayList<Objects>(0); //List of all objects in the game
     
    /**
     * This method will check for input, move things
     * around and check for win conditions, etc
     */
    void update()
    {
        for (int i=0; i<objects.size(); i++){
            objects.get(i).act();
        }
    }
     
    /**
     * This method will draw everything
     */
    void draw()
    {              
        Graphics g = getGraphics();
         
        Graphics bbg = backBuffer.getGraphics();
         
        bbg.setColor(Color.WHITE);
        bbg.fillRect(0, 0, windowWidth, windowHeight);
         
        for (int i=0; i<objects.size(); i++){
            bbg.drawImage(objects.get(i).getImage(),objects.get(i).getX(),objects.get(i).getY(),this);
        }
         
        g.drawImage(backBuffer, insets.left, insets.top, this);
    }
}
bourne bourne

2014/4/30

#
Just making sure, a Player object does have a nonempty image? And what I have found, usually a JPanel will be used to draw to, instead of directly to the JFrame (or top level Window)
bourne bourne

2014/4/30

#
Or to work with what you have it may have to do with opaqueness of the pane/components of the JFrame. See http://stackoverflow.com/a/5501412
davmac davmac

2014/5/1

#
Also, it looks like you're painting on the wrong thread. The whole approach seems awkward to me; instead you should issue repaint requests (via the repaint() method) and override the paint(...) method (possibly override the paint method of the content pane rather than the JFrame itself).
Entity1037 Entity1037

2014/5/1

#
The GameEngine is mostly take from this tutorial actually: http://compsci.ca/v3/viewtopic.php?t=25991 I still don't know what a content pane is, or what it means to paint on the wrong thread. I'm just trying to build a fully functional basic game engine so that I can get better at it, and eventually be able to program whatever features I could ever need in a game.
davmac davmac

2014/5/1

#
Most Swing calls are only valid on the Swing event thread (also called the "event dispatch thread", EDT, and the event queue). See this tutorial. Be wary of other resources on the web; many people, who otherwise appear to know what they are doing, do not properly follow the Swing concurrency rules.
Entity1037 Entity1037

2014/5/2

#
I have no idea what swing is, or anything about it. I'm just trying to paint the images of the classes to a paint winow. I'm just trying to find out how to do that and why it works, not a giant tutorial that teaches me every little thing except the one thing I need to do that doesn't seem to be present anywhere in the tutoral. Seriously, the oracle tutorials in my opinion are horrible. I can't learn everything all at once from just page after page of direct fact explaining. Seriously, it doesn't break down anything. It just explains that this does this and you need to remember {insert 20 facts here}, and now this giant piece of code works because of an interaction that is never explained. Can someone just make a tutorial that actually explains the process of the code, and why everything is the way it is? I can't learn from just having a tutorial say that this does this and now this works. I need to understand the code by having the tutorial explain the steps and the process of what's happening and why!!!
davmac davmac

2014/5/3

#
"Swing", built on "AWT", is the user interface toolkit provided by Java. You're using it whenever you use classes from the javax.swing.* or java.awt.* packages. There's a whole trail of Swing tutorials here. I think part of the problem is that you think it should be simple, but it's not - there's a lot to learn. That's why many tutorials, such as the one you linked to, don't explain very much. They assume some basic knowledge. If you really want to learn this, you need to do a lot of reading. But the good news is that the material is there.
Entity1037 Entity1037

2014/5/4

#
Well...... OK then! TIME TO STUDY!!!!
You need to login to post a reply.