String won't display in my window
I don't know what is causing this, but what happens, is that my window appears, but my drawString(); doesn't appear and show the text. No errors or anything.
Code :
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.naming.ldap.Rdn;
public class MainGameClass {
//Accesses the ScreenHandler class and makes it use 'sh' variable
static ScreenHandler sh = new ScreenHandler();
//Main thingy (duh)
public static void main(String[] args) {
//Makes ScreenHandler class activate DrawWindow method
sh.DrawWindow();
}
public void paint(Graphics g){
g.drawString("Hey!", 200, 200);
}
}
ScreenHandler class:
Code :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
//Referenced by MainGameClass
public class ScreenHandler {
//Draws the window the game is in
JFrame frame = new JFrame("Indev v0.01");
public void DrawWindow(){
//Initiates window drawing
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel emptyLabel = new JLabel("");
//Makes it 800 by 600
emptyLabel.setPreferredSize(new Dimension(1000, 600));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
frame.pack();
//Makes it visible
frame.setVisible(true);
//Activates the PaintWindow function below
PaintWindowColor();
}
public void PaintWindowColor(){
//Sets the background color to blue
//frame.getContentPane().setBackground(Color.BLUE);
}
}
Re: String won't display in my window
any help would be appreciated
Re: String won't display in my window
The provided code is not enough for us to help you. We don't know what are you trying to do in ScreenHandling class.
Re: String won't display in my window
Oh, ok. Sorry. Posting code now.
Re: String won't display in my window
Add a println next to the drawString method call to verify that the paint method is being called.
Where do you call the paint() method with the drawString?
If you think you are overriding a component method add the annotation: @Override just before paint() to have the compiler tell you if you are.
Re: String won't display in my window
Quote:
Originally Posted by
Norm
What he said
I added a println, and it wasn't being called.
How would you suggest I call it, I am new to stuff like this :)
Re: String won't display in my window
Please explain what you are trying to do. Neither of your classes extend any GUI classes.
If you want to make drawings you are going to need some GUI.
If you don't understand how to create a GUI application, try reading the tutorial:
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)