Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Split code from Gui  (Read 1475 times)
0 Members and 1 Guest are viewing this topic.
Offline stef569

Junior Duke





« Posted 2007-09-02 09:23:54 »

Hi, I would like to split code and gui
Then So i've read it works for an applet and a frame and webstart. So I created 2 new classes one for a Frame and one for an applet.
I have never worked with applets before, but for now I would like to use the frame.
What would go in that TetrisFrame class? the creation of the frame, keylisteners, buffer strategy?
I used extends canvas in my Game class and then add it to the frame.

To makes things even more (complicated)  Grin I use a Game_Manager that creates all the game objects, starts a thread, and should start a Frame or Applet.
I alsoo want to say that I use Awt.

So this is what i got now:
1. I start in Game_Manager create a Game_Manager Object from there
2. I start my thread and all the objects i need to start a game
3. I start the Frame or applet Object , passing the Game var.
4. It creates a Frame and adds a game Canvas inside it
5. The game starts.

what happens to the keylisteners in what class should they go?
and where does the buffer strategy goes?
Game = a canvas object ?
is there a tutorial for this

at the moment the buffer is in the Game Constructor method and is called before it is added to the frame. Giving the following error:
1  
2  
Exception in thread "main" java.lang.IllegalStateException: Component must have a valid peer
   at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3362)

I understand why but don't know how to fix it, because when I create a frame I have to add the game!
Help appreciated Smiley

Game_Management code snipet of starting a game.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
  public Game_Management() {
    bFact = new BlockFactory();
    board = new Board(20,10,Block.TILE_SIZE);
    game = new Game(this,board,bFact);
    if (CREATE_FRAME) {
      new TetrisFrame(game);
    }
    else {
      new TetrisApplet(game);
    }
    startGame();
  }

  public void startGame() {
    /* Initialize the game thread. */
    GameThread = new GameThread();
    GameThread.start();
  }

  private class GameThread extends Thread {
    public void run() {
      game.run();
    }
  }
Offline keldon85

Senior Duke


Medals: 1



« Reply #1 - Posted 2007-09-02 13:33:25 »

For Tetris, look at the stroll Tetris clone source code to see my approach. Unfortunately the Tetris part is not well commented, but you will see no AWT references in its code.

Offline stef569

Junior Duke





« Reply #2 - Posted 2007-09-02 14:29:18 »

Hi Keldon, I'm using Swing now.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class TestFrame {
   public static void main ( String argv[] ) {
      ScreenCanvas screenCanvas = new ScreenCanvas ();
      Frame frame = new Frame ("Stroll: test wave");
      frame.addWindowListener(new WindowAdapter(){
         public void windowClosing ( WindowEvent e ){
            System.exit(0);
         }
      });
      frame.add(screenCanvas);
      frame.setVisible(true);
      frame.pack();
     
      // start test
      new Test().start();
   }
}


that's excactly what I got, but I can't find the class where you use 'BufferStrategy' do you use that?

I have one line more at the end
frame.createBufferStrategy(2);
but I don't know how I would read that into screenCanvas.

I tried to put this code
   
1  
2  
createBufferStrategy(2);
    strategy = getBufferStrategy();

directly in the constructor but it sais peer = null
I then added addnotify but i get:
Component must have a valid peer
puling my hair out..........
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline keldon85

Senior Duke


Medals: 1



« Reply #3 - Posted 2007-09-02 15:26:45 »

Inside gamelib I have two main packages of interest; the one that extends Panel is org.konelabs.gamelib.gba1api.ScreenCanvas. I didn't use buffer strategy since I am already buffering my images due to my GBA graphics emulation that requires I do full screen redraws. Of course some properties of Java I still get wrong, so that might be my mistake - although I have smooth action on my comp.

Offline keldon85

Senior Duke


Medals: 1



« Reply #4 - Posted 2007-09-02 20:49:55 »

Just to let you know, I am updating the code - for example all code is commented in the gamelib. The methods themselves feature just about the same amount of commenting, and classes such as BGLayer are really optimized in case you were wondering what made it ticked - so it might be harder to understand without looking at gbatek

But how it is all handled (since I didn't really say) is that my initial graphics classes (Screen and such) write to an integer array, Screen updates it each frame and then calls screenUpdated() (from org.konelabs.gamelib.graphics.IScreen). When this is called the ScreenCanvas responds by updating its Image and calling repaint. I do think there are more sophisticated methods than this, however I am having no troubles at the moment speed wise.

Offline stef569

Junior Duke





« Reply #5 - Posted 2007-09-03 11:24:57 »

Srry Keldon,

I hardly understand what you are doing, I would like to keep things a simple as possible that's why I use bufferstrategy.
I did some testing and this came up:

When I create my frame +  createBufferStrategy(2); strategy = getBufferStrategy();
in the GameCanvas constructor it works fine.

But what I want to do seems not to be possible =(
The problem is these 2 line where do I put these 2 lines when I create a frame in a seperate class then the canvas I will be adding to the frame.
    createBufferStrategy(2);
    strategy = getBufferStrategy();
I read that IT should be placed in the Object that uses the buffer. So that's the canvas right,?
Offline keldon85

Senior Duke


Medals: 1



« Reply #6 - Posted 2007-09-03 18:43:51 »

You would want to do it with your canvas, as far as I remember there is some sort of flip or commit method in the buffer strategy that you call when you've finished drawing, and you create the buffer strategy either in the constructor or directly after.

Offline broumbroum

Junior Duke





« Reply #7 - Posted 2007-09-04 00:12:45 »

That must be in the addNotify() you rewrite from a Canvas since this object does require a peer which is not the case for lightWeight objects..  Wink
Srry Keldon,

I hardly understand what you are doing, I would like to keep things a simple as possible that's why I use bufferstrategy.
I did some testing and this came up:

When I create my frame +  createBufferStrategy(2); strategy = getBufferStrategy();
in the GameCanvas constructor it works fine.

But what I want to do seems not to be possible =(
The problem is these 2 line where do I put these 2 lines when I create a frame in a seperate class then the canvas I will be adding to the frame.
    createBufferStrategy(2);
    strategy = getBufferStrategy();
I read that IT should be placed in the Object that uses the buffer. So that's the canvas right,?

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline stef569

Junior Duke





« Reply #8 - Posted 2007-09-04 10:32:01 »

That must be in the addNotify() you rewrite from a Canvas since this object does require a peer which is not the case for lightWeight objects..  Wink

Thx for the reply broumbroum,

I searched for lightWeight objects and got 2 things:
Objects that are created when you need them
AWT Objects

I alsoo searched for peer  Grin :
and that's the platform independant code that is
for swing the hole canvas
for awt every component

So what are you saying  Undecided
I'm still confused  Sad
Offline broumbroum

Junior Duke





« Reply #9 - Posted 2007-09-05 03:27:26 »

What I explained is that any Canvas or so called "highweight Components" become displayable as they can refer to one known peer, which is as you noticed it, a screen device or more actual the window component you're displaying.  Roll Eyes
As far as you can imagine, one peer is coming available only when it has begun to display itself, and therefore the child components, as would be in that case the Canvas youre using, should use some notify method to get implied when it's to be displayed.
That's quite easy to figure out once you impl. the addNotify() within your Canvas extension code Smiley.  Is that becoming clearier?

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
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!