Basically, I am trying to drawString and I am getting some unexplained errors. Eclipse isn't redlining anything, but when I run the application, it gives an error and refuses to draw the test. Here is my source code:
MainGameClass:
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(); sh.PaintWindowText(null); } }
ScreenHandler
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); } public void PaintWindowText(Graphics g){ if(g instanceof Graphics2D){ } try{ g.drawString("Did this work?", 200, 200); } catch (Exception ex) { ex.printStackTrace(); } //Makes a new JLabel with text in it //JLabel label = new JLabel("Did this work?"); //Sets the font to plain //label.setFont(new Font("Sans-Serif",Font.PLAIN,140)); //Adds the text label to the screen //frame.add(label); } }
I hope you can help, as any guidance would be greatly appreciated!