Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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  
  drawing with BufferStrategy in windowed mode  (Read 6380 times)
0 Members and 1 Guest are viewing this topic.
Offline nanobomber

Junior Newbie





« Posted 2006-10-12 00:48:39 »

Hi,

I'm developing a 2D game and I'm creating a JFrame with a JPanel inside.

For drawing, I create a double buffer in the JFrame. The game actually is a subclass of JPanel, so I add that JPanel into the JFrame's content pane.

The problem I have is that for drawing the JPanel, I get a Graphics object from the BufferStrategy, but this Graphics object is aligned to the JFrame, not the internal JPanel, so the first row of 2D map tiles are drawed BEHIND the window's title bar, I guess because the (0,0) coordinate is relative to the JFrame, not the JPanel.

I could guess too that in full-screen mode this would be the right behavior, but I want my game to be windowed.

So the question is how I can draw my first row of tiles from the (0,0) of the JPanel so that they are not behind the window's title bar?

TIA,
Gabriel
Offline CommanderKeith
« Reply #1 - Posted 2006-10-12 04:15:54 »

I recently had this problem & this fixed it:

BufferStrategy strategy = frame.getBufferStrategy();
Graphics2D g2D = (Graphics2D)strategy.getGraphics();
Point frameLoc = Main.getFrame().getLocationOnScreen();
Point viewLoc = this.getLocationOnScreen();
g2D.translate(viewLoc.x - frameLoc.x, viewLoc.y - frameLoc.y);

The above was called within my JPanel, so 'this' is the JPanel.

Keith

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #2 - Posted 2006-10-12 04:50:04 »

What I (and most others do) is using a Canvas

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CommanderKeith
« Reply #3 - Posted 2006-10-12 05:00:22 »

What I (and most others do) is using a Canvas

That's a really good idea.  I should try it.  It may be trouble trying to paint other components on it though since Canvas isn't a Container & I've got Swing for the menus.

Offline SluX

Junior Devvie





« Reply #4 - Posted 2006-10-12 13:35:47 »

Err....why not simply directly draw onto the JFrame?  Huh

"Intelligence is the most beautiful gift and the greatest temptation which one life can receive from the gods."Me Cheesy
Play strategic football
Offline CommanderKeith
« Reply #5 - Posted 2006-10-12 14:12:17 »

JFrame decorations are on top of the BufferStrategy.

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #6 - Posted 2006-10-12 15:09:52 »

"Java Application Window", too. So you cant really know how much drawing area you actually have. If you use a Canvas instead its all yours.

弾幕 ☆ @mahonnaiseblog
Offline fletchergames

Senior Devvie





« Reply #7 - Posted 2006-10-12 18:24:07 »

You can override JPanel (or any other Component) and draw your graphics within the paintComponent method.
Offline CommanderKeith
« Reply #8 - Posted 2006-10-13 04:20:50 »

But what Graphics object do you give paintComponent(Graphics g)?

You have to give it the BufferStrategy's Graphics which is set at 0,0, under the decorations of the JFrame.

Keith

Offline fletchergames

Senior Devvie





« Reply #9 - Posted 2006-10-13 15:39:57 »

If you're drawing behind the title bar, all you have to do is add the size of the title bar to the coordinates you're using.  Java has a way to do this that covers the menu bar and any other borders as well.  Do the following:

1  
2  
3  
4  
5  
Insets insets = myJFrame.insets();
int xLeft = insets.left;
int xRight = myJFrame.getWidth() - insets.left - insets.right;
int yTop = insets.top;
int yBottom = myJFrame.getHeight() - insets.top - insets.bottom;


That determines the boundaries of the coordinates you can use.  Then you just have to add xLeft to all your x-coordinates and add yTop to all your y-coordinates.

You can translate your Graphics object like CommanderKeith did to avoid having to add the coordinates each time, but you may have to translate it back at the end of the paintComponent method to avoid screwing up other paint methods (I'm not entirely sure about this).

Using paintComponent(Graphics g) is much better though.  You won't have to translate coordinates if you do what I describe in the rest of this post.

You don't call paintComponent(Graphics g).  Swing calls it.  You just write the method.  You should be doing the drawing within the paintComponent(Graphics g) method of your JPanel, not by getting the Graphics from the BufferStrategy.  Here is all my BufferStrategy code from my current program:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
//create the BufferStrategy - the EventQueue avoids potential deadlock in some
//versions of Java (such as 1.4.1_02)
try {
   EventQueue.invokeAndWait(new Runnable() {
      public void run() {mainFrame.createBufferStrategy(2);}
   });
} catch(InterruptedException exception) {
   //ignore
} catch(InvocationTargetException exception) {
   //ignore
}


I should note that if you disabled the RepaintManager, Swing won't draw anything for you.  If you called "myJFrame.setIgnoreRepaint(true)", all you have to do is call "myJFrame.repaint()" within your main loop wherever you want to draw the screen.  You don't have to mess around with getting the Graphics object out of the BufferStrategy.

You should do the following when setting up your JPanel:

1  
2  
3  
4  
5  
6  
7  
8  
9  
myJFrame.getContentPane().setLayout(null);

myJFrame.add(myJPanel);
myJPanel.setLocation(0, 0);

Insets insets = myJFrame.insets();

//set the size of the JPanel to the part of the JFrame that doesn't include the title bar
myJPanel.setSize(myJFrame.getWidth() - insets.left - insets.right, myJFrame.getHeight() - insets.top - insets.bottom);


You still need to add the JPanel to the JFrame and do all the other stuff you're doing now.

This puts the JPanel right under the title bar.  You would think that it would have to be "myJPanel.setLocation(insets.left, insets.top)", but that is not the case.  When setting the location, the insets are taken into to account automatically.

If you do that, you shouldn't have to adjust the coordinates within the JPanel.

Setting the layout to null makes it so that you can just set the coordinates of your Components instead of having a LayoutManager do it for you.  You do not necessarily have to set the layout to null - you can just set up your Components and choose an appropriate LayoutManager so that the Components get put at the right spot.

In the program I'm working on now, my main frame has a layout of CardLayout so that I can switch between screens easily.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nanobomber

Junior Newbie





« Reply #10 - Posted 2006-10-14 21:49:44 »

Thank you all for your answers!

I tried CommanderKeith's recomendation and worked just fine.

Regards,
Gabriel
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (150 views)
2017-09-30 02:57:34

theagentd (225 views)
2017-09-26 18:23:31

cybrmynd (304 views)
2017-08-02 12:28:51

cybrmynd (291 views)
2017-08-02 12:19:43

cybrmynd (300 views)
2017-08-02 12:18:09

Sralse (292 views)
2017-07-25 17:13:48

Archive (980 views)
2017-04-27 17:45:51

buddyBro (1106 views)
2017-04-05 03:38:00

CopyableCougar4 (1685 views)
2017-03-24 15:39:42

theagentd (1433 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!