Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  ClassCastException and BufferStrategy  (Read 855 times)
0 Members and 1 Guest are viewing this topic.
Offline antisocial

Junior Newbie




Java games rock!


« Posted 2005-03-14 00:40:06 »

I just want to draw a black window! (sob.)

I was messing around with kevglass' space invaders tutorial, but couldn't get anything to draw to the window, due to the following exception:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
Exception in thread "main" java.lang.ClassCastException
   at apple.awt.StrategyBufferImage.<init>(StrategyBufferImage.java:26)
        at apple.awt.ContainerModel.createBuffers(ContainerModel.java:155)
        at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3111)
        at java.awt.Component$FlipBufferStrategy.<init>(Component.java:3080)
        at java.awt.Component.createBufferStrategy(Component.java:2989)
        at java.awt.Canvas.createBufferStrategy(Canvas.java:166)
        at java.awt.Component.createBufferStrategy(Component.java:2921)
        at java.awt.Canvas.createBufferStrategy(Canvas.java:141)
        at Game.<init>(Game.java:25)
        at Game.main(Game.java:42)

I'm new to this, so I wouldn't be surprised if it were due to some silly oversight on my part. Any suggestions, corrections or workarounds would be greatly appreciated. I tried to simplify as much as possible for troubleshooting purposes. Here's the 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  
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class Game extends Canvas {

      private boolean gameRunning = true;

      public Game() {
            JFrame container = new JFrame("Black Rectangle");
            JPanel panel = (JPanel) container.getContentPane();
            panel.setPreferredSize(new Dimension(800,600));
            panel.setLayout(null);
           
            setBounds(0,0,800,600);
            panel.add(this);
           
            setIgnoreRepaint(true);
           
            container.pack();
            container.setResizable(false);
            container.setVisible(true);
            requestFocus();
           
            createBufferStrategy(2);
            BufferStrategy strategy = getBufferStrategy();
     
            while (gameRunning) {
                 
                  Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
                  g.setColor(Color.black);
                  g.fillRect(0,0,800,600);
           
                  g.dispose();
                  strategy.show();
                 
                  try { Thread.sleep(10); } catch (Exception e) {}
            }
      }

      public static void main(String args[]) {
            new Game();
      }
     
}

I mean, this should work, right?

I'm running Mac OS X 10.3.8 with Java 1.42 and an nVidia GeForce FX 5200 video card.
Offline CaptainJester

JGO Knight


Medals: 10
Projects: 2


Make it work; make it better.


« Reply #1 - Posted 2005-03-14 10:29:55 »

Definitely something strange going on.  I copied the code exactly and it works fine for me.

Offline digitprop

Junior Member





« Reply #2 - Posted 2005-03-14 10:40:25 »

I may have overlooked something (a speciality of mine), but it seems ok to me.

Could be an Apple-specific problem. It compiles and runs fine on my system (Windows XP, Java 1.4.2).

M. Fischer . www.digitprop.com
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline antisocial

Junior Newbie




Java games rock!


« Reply #3 - Posted 2005-03-15 03:56:21 »

Well, good and bad news.

Bad: The problem definitely seems to be a problem with Apple's implementation of BufferStrategy.

Good: It seems to work fine when I use the BufferStrategy with a Frame instead of a subclass of Canvas.

Not an ideal fix to the problem, but I guess it will have to do.

Edit: Crap. That means I can't use accelerated graphics, doesn't it?
Crap.
Offline Valodim

Junior Member




*sigh*


« Reply #4 - Posted 2005-03-15 19:05:30 »

what? bufferstrategy from a frame can't be accelerated? Shocked
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #5 - Posted 2005-03-21 10:51:13 »

This has nothing to do with hardware acceleration - which is not implemented AT ALL on the Mac for Java 1.4.2.

It seems to be a quirk in the Apple implementation of BufferStrategies... perhaps a bug should be filed with Apple.

Thankfully, according to rumors, we are not far away from the release of Mac OS X 10.4, and with it Java 5 for the Mac, with faster graphics and nice stuff like that.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (67 views)
2013-05-17 21:29:12

alaslipknot (76 views)
2013-05-16 21:24:48

gouessej (109 views)
2013-05-16 00:53:38

gouessej (104 views)
2013-05-16 00:17:58

theagentd (115 views)
2013-05-15 15:01:13

theagentd (104 views)
2013-05-15 15:00:54

StreetDoggy (149 views)
2013-05-14 15:56:26

kutucuk (172 views)
2013-05-12 17:10:36

kutucuk (170 views)
2013-05-12 15:36:09

UnluckyDevil (179 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.162 seconds with 20 queries.