Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
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  
  Displaying buttons at startup  (Read 1611 times)
0 Members and 1 Guest are viewing this topic.
Offline King_of_Men

Senior Newbie




EUII player


« Posted 2004-06-10 02:14:25 »

I am trying to make a startup screen for my game, with a background picture and some buttons. However, when I call my Buttons' paint(Graphics) method, they do not appear on the screen. This code :

1  
2  
3  
4  
   public void paint(Graphics g) {
      g.drawImage(frontpic, 0, 0, Color.white, null);
      quitButton.paint(g);
   }

produces only my background image called frontpic. Why is this? Here is the initialisation I did for my Buttons :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
      quitButton = new JButton(QUITGAME);
      quitButton.setActionCommand(QUITGAME);
      quitButton.addActionListener(this);

      getContentPane().setLayout(null);
      getContentPane().add(quitButton);

      getContentPane().setBackground(Color.white);

      // TODO : Set according to resolution.

      quitButton.setBounds(new Rectangle(850, 30, 120, 20));

      show();

To win one hundred victories, in one hundred battles, is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Offline z.e.r.o

Junior Devvie




Java games rock!


« Reply #1 - Posted 2004-06-10 11:01:19 »

Calling paint on a component will repaint the component but I guess is badly placed.
Try to use the update method to trigger components repaint, this way you should be more coherent with the Swing gfx pipeline.

1  
2  
3  
4  
public void update (Graphics g) {
   this.paint(g);
   quitButton.paint(g);
}

Matteo Anelli
.brain - http://www.dot-brain.com
Offline King_of_Men

Senior Newbie




EUII player


« Reply #2 - Posted 2004-06-10 20:17:42 »

Well, something similar. The code works if I call quitButton's repaint() method instead of paint(Graphics). Why this should be, I have no idea. If anyone cares to enlighten me, I would be most grateful.

To win one hundred victories, in one hundred battles, is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #3 - Posted 2004-06-10 21:54:22 »

http://java.sun.com/products/jfc/tsc/articles/painting/

Offline King_of_Men

Senior Newbie




EUII player


« Reply #4 - Posted 2004-06-10 23:09:51 »

Yes, that's where I got the idea of using repaint. But as far as I could see, it doesn't explain why this is what works. OTOH, maybe I just missed it skimming through and trying things one after another.  Smiley

To win one hundred victories, in one hundred battles, is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #5 - Posted 2004-06-10 23:52:13 »

You are using Swing components and are bypassing the RepaintManager.

The article states:
"Swing Painting Guidelines

Swing programs should understand these guidelines when writing paint code:

For Swing components, paint() is always invoked as a result of both system-triggered and app-triggered paint requests;update() is never invoked on Swing components.

Programs may trigger a future call to paint() by invoking repaint(), but shouldn't call paint() directly..."


The Swing double buffering may be related to why calling paint() directly wasn't working.

Offline King_of_Men

Senior Newbie




EUII player


« Reply #6 - Posted 2004-06-11 01:00:48 »

OK, I think I understand that. It is only moderately un-obvious after being pointed out  Wink. Maybe you can understand why this doesn't work?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
      scenarionames = new String[]{"blah", "bleh"};

      scenList   = new JList(scenarionames);
      scenViewer = new JScrollPane();
      scenViewer.setViewportView(scenList);
      scenList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      scenList.setVisibleRowCount(-1);
      scenViewer.setAlignmentX(LEFT_ALIGNMENT);

      getContentPane().setLayout(null);
      getContentPane().add(scenViewer);
      getContentPane().add(scenList);

      scenViewer.setBounds(new Rectangle(400, 100, 200, 100));
      scenList.setBounds(new Rectangle(100, 100, 200, 500));
      show();


The way it doesn't work is that scenViewer is displayed, a nice gray rectangle, but with no list inside it. I display the raw list beside it as a test; that comes out fine - one blah, one bleh, both selectable. I've looked through the examples, and I cannot figure out what I am doing differently from what they do. I have no painting code this time, I leave everything to the builtin paint method, which evidently calls paintComponents just as it is supposed to. Why isn't the JScrollPane painting its contents?

To win one hundred victories, in one hundred battles, is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #7 - Posted 2004-06-11 02:22:28 »

A component can only be in one container.  You have added scenList to the JScrollPane AND the content pane..  

Don't add the list to the content pane and don't call setBounds on the list.

btw.  I usually just use
1  
new JScrollPane(scenList);
 instead of the two step process to construct then add to the view port view.

Offline King_of_Men

Senior Newbie




EUII player


« Reply #8 - Posted 2004-06-11 19:39:58 »

Ah, doesn't that look lovely, now?  Cheesy  Thank you. I'm not sure whether to feel stupid for not seeing that, or pleased because it works at last.  :-/ About the one-step, I used that too at first, then changed it do see if it made a difference.

To win one hundred victories, in one hundred battles, is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Offline King_of_Men

Senior Newbie




EUII player


« Reply #9 - Posted 2004-06-11 22:18:01 »

Aggh! That is, the code works if I put it in the constructor of my game-initialiser object. However, I would like for the list to pop up when the player presses the "New Game" button, which means I would like to have the list waiting in the wings, then update in the actionPerformed method. As far as I understand the thread issues, this is safe, right?

However, if I do things this way, I get that grey rectangle again, instead of the list within a viewer that I get if the code is in the constructor. I've tried putting the initialisation code in the constructor and just adding it in actionPerformed, like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
  constructor () {
      (...)
      scenList   = new JList(scenarionames);
      scenViewer = new JScrollPane(scenList);
      scenList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      scenList.setVisibleRowCount(-1);
      scenViewer.setBounds(new Rectangle(-1000, -1000, 200, 500));
   }

   actionPerformed {
      getContentPane().add(scenViewer);
      repaint();
   }

with the same result. Next I tried initialising scenViewer with width and height both zero, like so :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
  constructor () {
      (...)
      scenList   = new JList(scenarionames);
      scenViewer = new JScrollPane(scenList);
      scenList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      scenList.setVisibleRowCount(-1);
      scenViewer.setBounds(new Rectangle(100, 100, 0, 0));
      getContentPane().add(scenViewer);      
   }
    actionPerformed {
      scenViewer.setBounds(new Rectangle(100, 100, 200, 500));
      repaint();
   }

again with the same result. Finally - success at last! - I initialised it to be off screen, then moved it back in when actionPerformed was called, thus :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
  constructor () {
      (...)
      scenList   = new JList(scenarionames);
      scenViewer = new JScrollPane(scenList);
      scenList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      scenList.setVisibleRowCount(-1);
      scenViewer.setBounds(new Rectangle(-1000, -1000, 200, 500));
      getContentPane().add(scenViewer);      
   }

    actionPerformed {
      scenViewer.setBounds(new Rectangle(100, 100, 200, 500));
      repaint();
   }

This did have the side effect of suddenly blanking (making white) half my screen, but I was able to fix that in paint(). Could you explain to me why these apparently similar procedures give such different results? You've been very helpful so far.

To win one hundred victories, in one hundred battles, is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #10 - Posted 2004-06-11 23:05:36 »

Chances are that the container needed more info - a revalidate() call perhaps?  What you can try is setting a break point in the parent containers paint method and then stepping through to see what is happening.  

But I would add the scroll pane in the beginning and do a setVisible(false) on it to hide it.  The setVisible(true) when your button is pressed.

Offline King_of_Men

Senior Newbie




EUII player


« Reply #11 - Posted 2004-06-11 23:34:14 »

Ah, that works and is so much more elegant. Thanks.

To win one hundred victories, in one hundred battles, is not the acme of skill. To subdue the enemy without fighting is the acme of skill.
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 (34 views)
2014-11-22 12:13:56

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

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

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

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

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

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

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

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

CopyableCougar4 (82 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!