Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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  
  How do you paint a content pane?  (Read 1192 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Posted 2012-08-26 08:11:55 »

I'm laying the groundwork for an applet, but for now I'm testing it out as an application. Anyway, I originally had it such that I called repaint() at the standard 60 times a second. But I found that this flickered a lot and was not very good. I looked into it and found that I should be using "paintComponent()" to paint on a contentPane() instead of overriding the regular paint method.

So I have an class called "GamePanel" which extends a JPanel, and here is it's paint component method:
1  
2  
3  
4  
5  
6  
7  
@Override
   public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g;
      Rectangle rect = new Rectangle(100, 100, 100, 100);
      g2d.fill(rect);
   }

All it does is draw a black square

And here's how I set the panel up at the start of the program:
1  
2  
3  
4  
5  
6  
7  
GamePanel gamePanel = new GamePanel();
GameComponent gameComponenet = new GameComponent();
JFrame frame = new JFrame("Video Game");

gamePanel.add(gameComponenet, BorderLayout.CENTER);

frame.setContentPane(gamePanel);


Now I'm just kinda confused... How do I access the "paintComponent()" method in the GamePanel class WITHOUT putting it in the overridden paint() method? I found that when I did the following in conjunction with the 60fps repaint(), It just flickered anyway, so that can't be the answer
1  
2  
3  
4  
@Override
   public void paint(Graphics g) {
      gamePanel.paintComponent(g);
   }


Thanks
Offline keldon85

Senior Devvie


Medals: 1



« Reply #1 - Posted 2012-08-26 08:20:18 »

You need to render to an off screen image, then draw that to the Graphics object passed to you in paint.

Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Reply #2 - Posted 2012-08-26 08:21:22 »

You need to render to an off screen image, then draw that to the Graphics object passed to you in paint.

Er... How do you do that?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2012-08-26 09:01:00 »

Most of Swing is double buffered by default.

I would use canvas and a bufferstrategy so you will have control on when everything is rendered. EG: active rendering

Here are some useful links to help you get started on properly double buffer and active render.

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418

http://gpsnippets.blogspot.com/2010/10/how-to-use-bufferstrategy-in-java.html


Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Reply #4 - Posted 2012-08-26 20:17:04 »

Ah, thanks so much for those tutorials! I found the first one very useful. I now have a much smoother rendering system. Thank you
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.

trollwarrior1 (29 views)
2014-11-22 12:13:56

xFryIx (71 views)
2014-11-13 12:34:49

digdugdiggy (50 views)
2014-11-12 21:11:50

digdugdiggy (44 views)
2014-11-12 21:10:15

digdugdiggy (38 views)
2014-11-12 21:09:33

kovacsa (62 views)
2014-11-07 19:57:14

TehJavaDev (67 views)
2014-11-03 22:04:50

BurntPizza (64 views)
2014-11-03 18:54:52

moogie (80 views)
2014-11-03 06:22:04

CopyableCougar4 (80 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!