Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: String won't display in my window

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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:

    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);
    	}
     
    }
    Last edited by JamEngulfer221; November 24th, 2011 at 11:50 AM.


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String won't display in my window

    any help would be appreciated

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String won't display in my window

    Oh, ok. Sorry. Posting code now.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,798
    Thanks
    55
    Thanked 2,343 Times in 2,314 Posts

    Default 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.
    Last edited by Norm; November 24th, 2011 at 04:08 PM.

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    37
    My Mood
    Where
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String won't display in my window

    Quote Originally Posted by Norm View Post
    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

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,798
    Thanks
    55
    Thanked 2,343 Times in 2,314 Posts

    Default 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)

Similar Threads

  1. [SOLVED] Painting Clock, Overlaps the current String display
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 5th, 2011, 08:54 AM
  2. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 09:16 AM
  3. Replies: 18
    Last Post: March 2nd, 2011, 10:52 AM
  4. Craating non active window ,and inputing to the non active window.
    By java-beginner in forum Java Theory & Questions
    Replies: 0
    Last Post: February 25th, 2011, 10:13 PM
  5. cmd.exe DOS Window
    By Curious in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 1st, 2010, 10:30 AM