Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  [Solved] Canvas drawing problem!  (Read 3333 times)
0 Members and 1 Guest are viewing this topic.
Offline deadly72

Junior Member





« Posted 2011-09-10 03:17:25 »

Hi guys!
So I've been wanting to change the way my menu is going to be initialized and all - basically just remove all swing components, apart from maybe the jframe holding the canvas? Tongue  Anyway, I've run into a snag, that is the Graphics2D object won't draw whatever I want it to draw.  I think the problem lies in it only being painted once? - something that shouldn't matter?  Here is the code linked to my problem.

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  
     private void init() {
       GUI gui = new GUI();
        handler = new InputHandler(gui);
       
   gameFrame = new JFrame("ShootEm UP");
   gameFrame.setLayout(null);
   gameFrame.setPreferredSize(new Dimension(CONST_WIDTH, CONST_HEIGHT));
        gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gameFrame.setResizable(false);
        gameFrame.setIgnoreRepaint(true);
        designMenuBar();
         
        canvas = new Canvas();
        canvas.setBounds(0, 0, CONST_WIDTH, CONST_HEIGHT);
        canvas.setIgnoreRepaint(true);
       canvas.addMouseMotionListener(handler);
        canvas.addMouseListener(handler);
        canvas.addKeyListener(handler);
       
       
        gameFrame.add(canvas);
        gameFrame.pack();
       
        canvas.createBufferStrategy(2);
        canvas.requestFocus();

        render();
        gameFrame.setVisible(true);
    }

   public void render() {
       try {
       BufferStrategy bf = canvas.getBufferStrategy();
        Graphics2D g = (Graphics2D) bf.getDrawGraphics();
       
        test(g);
       
        g.dispose();
        bf.show();
        Toolkit.getDefaultToolkit().sync();
       }
       catch (Exception e) {
          e.printStackTrace();
       }
    }
   
    private void test(Graphics2D g) {
        g.setColor(Color.GREEN);
        g.drawString("LOL", 100, 100);
        g.fillRect(500, 500, 100, 100);
    }


It's safe to rule out the gui variable, the handler variable and designMenuBar() function. Anyone got any ideas?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-09-10 03:28:03 »

You call BufferStrategy.show() before the window is made visible? Of course it won't render Tongue

Also, usually if you will directly control rendering like that, the render()+show() method calls are usually in a loop.

EDIT: And there is no need to call Toolkit.sync() since BufferStrategy takes care of that for you.
EDIT2: canvas.createBufferStrategy(2) is also supposed to throw an IllegalStateException here because you cannot call it before the window is made visible.

Offline deadly72

Junior Member





« Reply #2 - Posted 2011-09-10 03:33:39 »

I just set the gameFrame.setVisible(true) before the render() call -> still doesn't draw Sad
For now I actually don't need it in a loop, I'm using the loop as a running animation*, this is drawn before the loop for animation is called.

About Toolkit.sync() didn't know that - I'll keep that in mind! Thanks! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-09-10 03:44:35 »

I ran your code and it does render for me. However, once the window goes out of view or is minimized, the rendering will disappear. Maybe something happens that causes your rendering to disappear. Putting "while(true) render();" at the end of init() should work.

One thing I noticed, calling JFrame.pack() will not cause Canvas.createBufferStrategy(2) to throw an exception. Once I remove it, I do get an exception. Really weird...

Offline deadly72

Junior Member





« Reply #4 - Posted 2011-09-10 03:53:03 »

You actually made me notice two things here.
1) The problem is intermittent on windows, I'm dev'ing on a linux box and here it doesn't show up. Only sometimes - which is a problem since I have no clue how to debug this!.  On windows, it successfully renders like 9/10 - but sometimes it fails.

2) I agree that the problem with the pack is really weird.  - I think this may be somehow linked to problem 1, because on windows after many restarts - the menubar seems to pack also - leaving a white bar for the remainder of the canvas (Not really worried about this, since this is what I will be changing, that is more towards games having a centered menu)

In addition, I don't see why I would need the loop, that is something that really stumps me.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-09-10 03:56:41 »

Well you can't really do anything about either. The thing to learn here is that you can't render once or else sometimes (or most of the time), something somewhere will cause your screen to want to refresh. Since there is nothing to draw, refreshing will set it to the default light gray background.

Offline deadly72

Junior Member





« Reply #6 - Posted 2011-09-10 04:10:39 »

I actually fixed it Wink - what you told me really set me off... was sad.
What triggered the solution?

The packing of the gameFrame.
I was directed here:
http://www.gamedev.net/topic/261754-javalangillegalstateexception-component-must-have-a-valid-peer/

which says I basically can't create the buffer before it's displayed (why didn't I think of that?).  Got me to set the visibility of the frame before the canvas created the buffer and i removed the packing.  Just set the size of the frame and voila, works like a charm Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
       ...
       
        gameFrame.add(canvas);        
        gameFrame.setVisible(true);
       
        canvas.createBufferStrategy(2);
        canvas.requestFocus();

        render();
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-09-10 04:18:04 »

Interesting......calling JFrame.pack(), then Canvas.createBufferStrategy(2), then JFrame.setVisible(true) still works for me, but doesn't for you. Oh Swing *facepalm*...

Offline Addictman

Senior Member


Medals: 3
Projects: 1


Java games rock!


« Reply #8 - Posted 2011-09-10 08:53:13 »

Hey.

createBufferStrategy(..) is an asynchronous operation, that's why it fails "now and then". (Sometimes it finishes before the frame is visible, sometimes not).

Try something like

1  
2  
3  
4  
5  
EventQueue.invokeAndWait(new Runnable() {
        public void run() {
            frame.createBufferStrategy(2);
        }
  });
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2011-09-10 08:59:53 »

There is no need for that, just call it after the frame is visible, that's all.

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

Senior Member


Medals: 3
Projects: 1


Java games rock!


« Reply #10 - Posted 2011-09-10 09:10:40 »

I would set the frame visible, and create the bufferstrategy inside the EventQueue.invokeAndWait() to avoid deadlocks on older java versions, and to make sure that on your first render loop, getBufferStrategy() doesn't return null.

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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (32 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!