Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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  
  paint() in swing  (Read 1246 times)
0 Members and 1 Guest are viewing this topic.
Offline Kova

Senior Member





« Posted 2006-02-14 20:10:07 »

Hi.
I overrided paint() to draw start menu for one game that I'm working on. I work with .png pictures by drawing background picture and then tekst picture on it, dependable where I am in start menu (at begining, in options submenu or somewhere else). When I click on some of the buttons, variable that remembers where I am in start menu changes accordingly and I call repaint(). In overriden paint() that is done with switch statement for different cases depending where I am.

So at begining of game start manu is drawn (start game, options, quit ....), if I click on let's say options then where_am_i variable changes to flag that I'm in options submenu and in paint() I use switch (where_am_i) to draw adequate picture for options submenu. Think sort of works, but not entirely...

When I click anywhere on button insted of previous picture of menu to disappear and new one to appear, the new one appears over the previous one. When I minimize and restore game it's drawn as it shuold, only one picture (right one). Clearly I don't know how (re)paint() works... I don't understand why when I call repaint() the new picture is drawn on top of everything else instead that application draws everything from square?
One more thing, is there a better or simpler way to do start menus for games? The game itself is run in seperate thread and has active rendering, and I don't want to do that on something so simple as start menu.
Thanks everybody.
Offline Jeff

JGO Coder




Got any cats?


« Reply #1 - Posted 2006-02-14 20:51:29 »

What sort of game is this?

In general games dont use paint() and repaint().  They use active rendering instead for lots of reasons having to do with controllability and performance.

If you are stuck woth Swing for some reason, then you dont override paint() like you do in AWT, there is a different method to override.  Its in the docs.   Look at JFrame and JComponent.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #2 - Posted 2006-02-14 20:54:21 »

If you are stuck woth Swing for some reason, then you dont override paint() like you do in AWT, there is a different method to override.  Its in the docs.   Look at JFrame and JComponent.

paintComponent(Graphics g);

Java Game Console Project
Last Journal Entry: 12/17/04
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kova

Senior Member





« Reply #3 - Posted 2006-02-14 22:16:13 »

It's a simple 2D soccer game, no AI, just 2 players over network. I'm just using it for example to learn basics of network use and developing a game overall. So I see, active rendering for all... preferably OpenGL as I see from reading forums. Well since I started it I'll try to finish it this way. I found official tutorial on painting in AWT and Swing, http://java.sun.com/products/jfc/tsc/articles/painting/, I'll post after I'm finish reading it.
Offline Metal

Junior Newbie





« Reply #4 - Posted 2006-02-15 17:30:06 »

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if(dbImage != null){
            g.drawImage(dbImage, 0, 0, null);
        }
    }

if you override the paintComponent method in a JPanel implementing class like i do above,
why is 'super.paintComponent(g);'  necessary? i saw it like this in a book, but it works fine without that line...

btw jeff, what do you mean with active rendering? i don't see how you can animate something without repaint()...
or do you mean you should have a separate render function?

could you please tell me what's the general idea behind active rendering?
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #5 - Posted 2006-02-15 18:54:25 »

The super.paintComponent(...) call ensures that there are no visual artifacts.  Also it draws any borders or other basic elements.

Active rendering means that you run a game loop and request the graphics object to draw with every iteration through the loop.  You also disable event painting.  Calling repaint() only schedules a call to paint(...).  This could delay a paint call while your loop continues to loop around a few times before the graphics are drawn.

Offline Metal

Junior Newbie





« Reply #6 - Posted 2006-02-15 21:38:04 »

that was very helpfull, thanks a lot!
Offline Jeff

JGO Coder




Got any cats?


« Reply #7 - Posted 2006-02-17 00:27:54 »

active rendering:

http://java.sun.com/docs/books/tutorial/extra/fullscreen/rendering.html

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Kova

Senior Member





« Reply #8 - Posted 2006-02-21 18:42:35 »

btw. as for main problem itself, I did workaround with clearRect(...) before every painting
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.

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

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

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

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

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

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

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

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

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
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!