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 16 of 16

Thread: Something weird with Graphics and drawString...

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

    Default I can't get paint() to work

    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.paint(null);
     
    	}
     
    }

    ScreenHandler

    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Frame;
    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");
     
    	//frame.add(new paintText);
     
    	paintText pt = new paintText();
     
    	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);
    		frame.setForeground(Color.BLACK);
    	}
     
    }
     
    @SuppressWarnings("serial")
    class paintText extends Canvas{
     
    	public paintText(){
    		System.out.println("Loaded xtraclass");
    	}
     
    	public void paint(Graphics g){
    		//System.out.println("Loaded!");
    		g.setColor(getForeground());
    		g.drawString("Hey!", 200, 200);
    	}
     
    }

    I hope you can help, as any guidance would be greatly appreciated!
    Last edited by JamEngulfer221; December 11th, 2011 at 05:43 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Something weird with Graphics and drawString...

    some unexplained errors
    What errors? I recommend posting them in their entirety

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

    Default Re: Something weird with Graphics and drawString...

    Ok, sorry. By the way, the reason I did a duplicate thread (sorry, I was going to delete this one) was that I think it was more applicable to the "What's wrong with my code" forum than the AWT/Java Swing forum.

    The error:

    java.lang.NullPointerException
    at ScreenHandler.PaintWindowText(ScreenHandler.java:4 6)
    at MainGameClass.main(MainGameClass.java:12)

  4. #4
    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: Something weird with Graphics and drawString...

    Exception clearly mentions that there is something null with the statement at line 46 of ScreenHandler.java, and the root cause is line 12 of MainGameClass. So, now try debugging from line 12 of MainGameClass.java and see why is there null.

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

    Default Re: Something weird with Graphics and drawString...

    Ah, yes. That was on purpose, it was the only thing I could do that didn't make Eclipse throw out an error. (in the form of a red line)

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,384
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Something weird with Graphics and drawString...

    Quote Originally Posted by JamEngulfer221 View Post
    Ah, yes. That was on purpose, it was the only thing I could do that didn't make Eclipse throw out an error. (in the form of a red line)
    So is your problem solved now?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Something weird with Graphics and drawString...

    Quote Originally Posted by KevinWorkman View Post
    So is your problem solved now?
    No, It still throws up the NullPointerException. And I don't know what to do to solve it.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,384
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Something weird with Graphics and drawString...

    You're passing null into your PaintWindowText (methods should start with a lower-case letter, by the way). What did you expect to happen?

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    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: Something weird with Graphics and drawString...

    What do you expect to do this code?
    null.callSomeFunction(obj)

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

    Default Re: Something weird with Graphics and drawString...

    I changed it so that it uses paint(Graphics g){
    }

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

    Default Re: Something weird with Graphics and drawString...

    So, in that case, what do I actually do to get my paint() to run/work?

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

    Default Re: Something weird with Graphics and drawString...

    So, in that case, what do I actually do to get my paint() to work then?

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,384
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Something weird with Graphics and drawString...

    Quote Originally Posted by JamEngulfer221 View Post
    So, in that case, what do I actually do to get my paint() to work then?
    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: Something weird with Graphics and drawString...

    Who is supposed to call your paint() method? Your classes do not extend any java classes.
    To be sure your methods are overriding java class methods, add the @Override annotation just before the method definition.

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

    Default Re: Something weird with Graphics and drawString...

    I read the article, and I am still confused. I had a look around the code, changed some of mine and still nothing. I simply can't figure out how I get it to work. Is there something hugely obvious I am missing? (Also updated the code in the OP)

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: Something weird with Graphics and drawString...

    Your code is very confused. It looks like you have copied and pasted it from many different sources and a lot of it doesn't go together, it just lays around loose.

    It might be easier to start from the beginning and build a program step by step rather than try to make this one do anything.

Similar Threads

  1. drawString
    By frozen java in forum Java Theory & Questions
    Replies: 17
    Last Post: June 14th, 2011, 04:36 PM
  2. Graphics class NullPointerException Initialize Graphics Class??
    By bglueck in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 13th, 2011, 11:13 PM
  3. drawString and JTextField
    By that_guy in forum AWT / Java Swing
    Replies: 9
    Last Post: January 29th, 2011, 01:43 AM
  4. [SOLVED] font size and other attributes for Graphics.drawString()
    By gib65 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 30th, 2010, 02:31 PM
  5. Click to start and drawString fonts
    By Campos in forum Java Applets
    Replies: 3
    Last Post: July 24th, 2009, 02:24 PM