Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Problem Centering Canvas on JFrame on: 2012-07-03 09:47:54
Anyway, there are no LayoutManagers to do the vertical alignment you want. FlowLayout can do the horizontal alignment, but obviously that's not enough.

hmm .. I'm using

1  
2  
frame.setLayout(new GridBagLayout());
frame.add(canvas);


Does that not do what the OP is looking for?

Incidentally, that does fail to work in FSEM on Windows with the D3D pipeline enabled - no idea why, and as I'm either using software rendering or LWJGL I switched D3D off.

Yeah that works i think ill go with that... Saves space Tongue

Thanks
2  Game Development / Newbie & Debugging Questions / Re: Problem Centering Canvas on JFrame on: 2012-07-03 09:13:48
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
panel.setLayout(new LayoutManager() {
   public void layoutContainer(Container parent) {
      Component child = parent.getComponents()[0];
      Dimension prefSize = child.getPreferredSize();

      int x = (parent.getWidth() - prefSize.width) / 2;
      int y = (parent.getHeight() - prefSize.height) / 2;
      child.setBounds(x, y, prefSize.width, prefSize.height);
   }

   // AFAIK you can leave the other methods of LayoutManager empty, in this specific case.
});


Thank you very much Riven and 65K, ra4king, SkyAphid, StumpyStrust

Works!!!
3  Game Development / Newbie & Debugging Questions / Re: Problem Centering Canvas on JFrame on: 2012-07-03 09:02:38
Try this:
1  
frame.setUndecorated(true);


You need to consider the decoration inset stuff and adjust draw positions.

What if i still want the title bar?


Actually I read from another thread that on some occasions if you don't set the preferred size and the size, it won't work :S
Don't believe everything you read.

Anyway, there are no LayoutManagers to do the vertical alignment you want. FlowLayout can do the horizontal alignment, but obviously that's not enough.

You can either write your own LayoutManager, or calculate the coordinates of the component yourself:
1  
2  
int x = (panel.getWidth() - canvas.getWidth()) / 2;
int y = (panel.getHeight() - canvas.getHeight()) / 2;

You must update these coordinates everytime the parent (panel) is resized.

What would i do with the x, y coords?
4  Game Development / Newbie & Debugging Questions / Re: Problem Centering Canvas on JFrame on: 2012-07-03 08:40:02
Set the same values for the size, preferred size, minimum size, and maximum size on the Canvas, then call JFrame.pack()

I did that... (updated method below) but still same problem. Any ideas.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
 public Game(){      
     Dimension size = new Dimension(WIDTH, HEIGHT);

      frame = new JFrame(NAME);
      JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      panel.setLayout(null);
      panel.setFocusable(true);
      canvas = new Canvas();
      canvas.setBounds(0, 0, WIDTH, HEIGHT);
      canvas.setIgnoreRepaint(true);    
      panel.add(canvas, BorderLayout.CENTER);
     
      canvas.setSize(size);
      canvas.setPreferredSize(size);
      canvas.setMinimumSize(size);
      canvas.setMaximumSize(size);
     
     
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);      
     
     
      canvas.createBufferStrategy(2);
      bufferStrategy = canvas.getBufferStrategy();    
      canvas.requestFocus();

      canvas.addKeyListener(new KeyListener() {               //KEYLISTENER
          public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_UP){
               Game.up = true;
            }
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
               Game.down = true;
            }
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
               Game.left = true;
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
               Game.right = true;
            }
           }

           public void keyReleased(KeyEvent e) {
              if(e.getKeyCode() == KeyEvent.VK_UP){
               Game.up = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
               Game.down = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
               Game.left = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
               Game.right = false;
            }
             
           }
         public void keyTyped(KeyEvent arg0) {
         }
         });
   
}

Lucas
5  Game Development / Newbie & Debugging Questions / Re: Problem Centering Canvas on JFrame on: 2012-07-03 08:15:29
So, how can this be done, all i want is to be able to paint everywhere and such. Have you run that code and seen what i mean?

it just isnt drawing to the bottom right corner...

Screenshots:

Draws to top left fine:


But not to bottom right:



Help appreciated,

Lucas
6  Game Development / Newbie & Debugging Questions / Re: Problem Centering Canvas on JFrame on: 2012-07-03 07:55:13
You explicitly remove the panel's layout manager.
But with ignored repaint the built in layout stuff probably doesn't work anyway.

I am unsure as to what you mean could you please give me an example
7  Game Development / Newbie & Debugging Questions / Re: Problem Centering Canvas on JFrame on: 2012-07-03 06:22:49
i dont mean the square... but if you run that code and move the square to the bottom right you see what i mean

Cheers
8  Game Development / Newbie & Debugging Questions / Problem Centering Canvas on JFrame on: 2012-07-03 05:39:24
Hello Guys,

Hope you can help me out on this one...

My code is here... http://pastebin.java-gaming.org/54a6b1d441c

The problem is that the canvas or JPanel (not sure which) isnt centered in the frame. It seems to start at the top left (0,0) but ends 10 - 20 pixels before the end of the frame. Is there any way i can center it or make it fill the whole frame?

Cheers, Lucas
9  Discussions / Miscellaneous Topics / Re: Preferred OS? on: 2012-06-24 22:29:25
Linux,

Just my 2 cents Smiley
10  Game Development / Newbie & Debugging Questions / Re: Quick Question about a basic game template on: 2012-06-24 22:15:22
Thanks,

Seems to be working now
Cheers for the help.

Lucas Grin
11  Game Development / Newbie & Debugging Questions / Quick Question about a basic game template on: 2012-06-24 09:38:59
Hello all i have two questions about a game template that i quickly wrote up... I am used to writing applets but i need to make a standalone game this time.

1-  How do i load an image? "getImage(getCodeBase(), "res/image.png");" Isn't working...
2-  How do you use a keyListener. The usual way for applets isn't working.

Help much appreciated Smiley

Here is the code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
package com.ion.rpgnoname;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable, KeyListener{
   
   private static final long serialVersionUID = 1L;
   
   private static final int SCALE = 4;
   static int WIDTH = 125;
   static int HEIGHT = 125;
   static String NAME = "GAME UNNAMED" ;

   Image guy;
   
   int x = 0;
   int y = 0;
   
   Image dbImage;
   Graphics dbg;

   public void init () {
      setSize  (WIDTH, HEIGHT);
      setFocusable(true);
      setBackground(Color.BLACK);  
      addKeyListener(this);
                guy = getImage(getCodeBase(), "res/image.png");

   }
   
   public void start() {
      Thread th = new Thread(this);
      th.start();
   }

   public void run() {
      Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
      while(true){
         repaint();
         try {
            Thread.sleep(10);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }    
         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
      }  
   }

   public void paint(Graphics g) {
      g.setColor(Color.BLACK);
      g.fillOval(x, y, 12, 12);
                g.drawImage(guy, 40, 40, this);
   }
   
   public void update(Graphics g){
      if (dbImage == null)
       {
           dbImage = createImage (this.getSize().width, this.getSize().height);
           dbg = dbImage.getGraphics ();
       }

       dbg.setColor (getBackground ());
       dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

       dbg.setColor (getForeground());
       paint (dbg);

       g.drawImage (dbImage, 0, 0, this);
   }
   
   public static void main(String[] args){
      Game game = new Game();
      game.setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
      game.setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
      game.setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
     
      JFrame frame = new JFrame(Game.NAME);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(new BorderLayout());
      frame.add(game);
      frame.pack();
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
     
      game.start();
   }

   public void keyPressed(KeyEvent e) {
      if(e.getKeyCode() == KeyEvent.VK_SPACE){
         x++;
         y++;
      }
   }
   public void keyReleased(KeyEvent e) {  
   }
   public void keyTyped(KeyEvent e) {  
   }
}
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (15 views)
2014-09-12 09:08:26

Norakomi (45 views)
2014-09-10 13:57:51

TehJavaDev (57 views)
2014-09-10 06:39:09

Tekkerue (26 views)
2014-09-09 02:24:56

mitcheeb (49 views)
2014-09-08 06:06:29

BurntPizza (33 views)
2014-09-07 01:13:42

Longarmx (19 views)
2014-09-07 01:12:14

Longarmx (21 views)
2014-09-07 01:11:22

Longarmx (20 views)
2014-09-07 01:10:19

mitcheeb (30 views)
2014-09-04 23:08:59
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!