• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 2D method + GTK

 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In an introductory book on Java, a simple method for doing Java 2D graphics is described in these steps:
1. Extend JPanel
2. override void paintComponent(Graphics g)
3. cast (Graphics g) to (Graphics2D)
4. use the J2D API from here.

Is this the only way, or was this just a simple introduction? The reason I ask is that there are some faults with it. The two main problems I have are:
1. The drawing flickers every time i move the JFrame containing it.
2. It won't seem to compile with gij under linux! i get the error

java.lang.ClassCastException: gnu.java.awt.peer.gtk.GdkGraphics cannot be cast to java.awt.Graphics2D

Chris
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gcj is wrong here in not using Graphics2D objects. That's par for the course, though; Gcj doesn't conform to every little wrinkle of Sun's Java implementation. You can use (and frankly, should be using) Sun's JDK on Linux, and then of course this problem will go away.

The flickering shouldn't happen; are you calling super.paintComponent() in your implementation?
 
Christopher Arthur
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Now that I've downloaded the Sun JVM, I'm not getting exceptions anymore.

No, I'm not calling super.paintComponent() anywhere. When should that be done? Actually the flickering seems to happen every time I click on the frame.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should pretty much always call super.paintComponent() as the first line of a paintComponent() implementation.

A JPanel should be double-buffered by default, which should eliminate any flickering; are you, by chance, using one of the constructors that lets you turn this feature off?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic