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 (592)
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  
  Why is my JFrame See Through  (Read 2413 times)
0 Members and 1 Guest are viewing this topic.
Offline jpkev14

Senior Newbie





« Posted 2012-03-01 12:06:15 »

hi this is the code and whenever i run it i get a see through frame where i can see whats behind it and the text "Java Game" is also visible


package JavaGame;
import java.awt.*;
import javax.swing.JFrame;

public class JavaGame extends JFrame{
   
   public JavaGame(){
      setTitle("Java Game");
      setSize(250, 250);
      setResizable(false);
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   
   
   public void paint(Graphics g){
    g.drawString("Java Game", 100, 100);   
    repaint();
   }
   
   public static void main(String[] args){
      JavaGame j = new JavaGame();
   }


}
Offline Darkmatr

Innocent Bystander





« Reply #1 - Posted 2012-03-01 13:51:57 »

I tried running your code, and it worked fine for me the JFrame was not see through for me. not sure what would cause it to be see through for you. Undecided
Offline theagentd

« JGO Bitwise Duke »


Medals: 361
Projects: 2
Exp: 8 years



« Reply #2 - Posted 2012-03-01 16:34:25 »

It was a long time since I used Java2D, but don't you need a
1  
super.paint(g);

inside paint()? And you shouldn't put a repaint() in paint()...

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2012-03-01 17:06:09 »

It was a long time since I used Java2D, but don't you need a
1  
super.paint(g);

inside paint()? And you shouldn't put a repaint() in paint()...

nah.
super.paint(g); I have never seen
and repaint, well, by default awt/swing has its own repainting thing

either way, should really not be transparent... I hope its linux and not windows =D

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2012-03-01 17:26:27 »

nah.
super.paint(g); I have never seen
and repaint, well, by default awt/swing has its own repainting thing

either way, should really not be transparent... I hope its linux and not windows =D
Please don't advise people if you don't know enough about a specific topic Sad

If nothing else, yes, super.paint(g); must be added. Either that, or you must fill the entire area of the component yourself. Failing to do that, will lead to undefined results (like you're seeing now). The same thing occurs, for example, when you call setOpaque(true) and don't fully paint the component: it will lead to rendering artifacts, like views of 'raw' memory on your CPU/GPU, depending on the implementation.

Further, you are overriding paint(g) on a JFrame, which is not allowed, please only override paintComponent(g). But do you really want to paint over the entire frame, or just between the frame borders and under the caption bar? What you most likely are looking for is this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
JPanel canvas = new JPanel() {
   public void paintComponent(Graphics g) {
      g.drawString("hello world", 100, 100);
   }
}
canvas.setPreferredSize(new Dimension(w, h));

JFrame frame = new JavaGame();
frame.getContentPane().setLayout(BorderLayout.CENTER);
frame.getContentPane().add(canvas);
frame.pack();
frame.setVisible(true);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Cero
« Reply #5 - Posted 2012-03-01 17:34:25 »

nah.
super.paint(g); I have never seen
and repaint, well, by default awt/swing has its own repainting thing

either way, should really not be transparent... I hope its linux and not windows =D
Please don't advise people if you don't know enough about a specific topic Sad

If nothing else, yes, super.paint(g); must be added. Either that, or you must fill the entire area of the component yourself. Failing to do that, will lead to undefined results (like you're seeing now). The same thing occurs, for example, when you call setOpaque(true) and don't fully paint the component: it will lead to rendering artifacts, like views of 'raw' memory on your CPU/GPU, depending on the implementation.

Further, you are overriding paint(g) on a JFrame, which is not allowed, please only override paintComponent(g). But do you really want to paint over the entire frame, or just between the frame borders and under the caption bar? What you most likely are looking for is this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
JPanel canvas = new JPanel() {
   public void paintComponent(Graphics g) {
      g.drawString("hello world", 100, 100);
   }
}
canvas.setPreferredSize(new Dimension(w, h));

JFrame frame = new JavaGame();
frame.getContentPane().setLayout(BorderLayout.CENTER);
frame.getContentPane().add(canvas);
frame.pack();
frame.setVisible(true);


Before moving to OpenGL, our whole game was Java2D, which I still have.
I have never used Panels or canvas', always painted directly on a Frame or JFrame
I have always overridden paint never paint components

granted, in real games we used bufferstrategy of course... so you dont even use paint() anymore

Quote
fill the entire area of the component yourself
well, I have always done that without even thinking about it, just to be sure.

Offline cheekymonk3y

Senior Newbie





« Reply #6 - Posted 2012-03-01 20:32:22 »

I see you're doing the JavaHub tutorials like I mentioned  Grin

I had the exact same problem and sticking super.paint(g) fixes it but the preferred way of doing it I believe is sticking adding a Canvas or JPanel to the JFrame and having your game play in that. Unfortunately, the JavaHub dude doesn't cover doing that up till as far as I've gotten but maybe he'll do that later on.

Also, I recommend you try making a Pong game before watching that tutorial. You've got enough to on from the first 11 videos so it's very doable but might be difficult.

Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 832
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2012-03-01 20:39:50 »

Before moving to OpenGL, our whole game was Java2D, which I still have.
I have never used Panels or canvas', always painted directly on a Frame or JFrame
I have always overridden paint never paint components

granted, in real games we used bufferstrategy of course... so you dont even use paint() anymore

Quote
fill the entire area of the component yourself
well, I have always done that without even thinking about it, just to be sure.
Just because it worked for you, doesn't mean you should. Many previous Java4K entries were not properly making calls to AWT/Swing, and while it worked at the time, current Java runtimes either flicker a lot of render nothing at all. So, if you want reliable results, don't just look at what works, but read up on how it's supposed to be done.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Cero
« Reply #8 - Posted 2012-03-01 20:40:37 »

tbh honest if I would use Java2D I would use Canvas too
because
Quote
do you really want to paint over the entire frame, or just between the frame borders and under the caption bar?
its true of course.

However I always wanted to keep all components out, with exception of the Frame itself

Offline jpkev14

Senior Newbie





« Reply #9 - Posted 2012-03-02 00:45:27 »

yeah im doin the tuts really helpfull though the see through part only happened before i drew anything else like a ball it only happened when the only thing on the frame was text so its kinda fixed now.

Thx anyways u guys.
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.

SHC (23 views)
2014-11-25 12:00:59

SHC (21 views)
2014-11-25 11:53:45

Norakomi (19 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 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 (41 views)
2014-11-12 21:09:33

kovacsa (67 views)
2014-11-07 19:57:14
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!