Results 1 to 6 of 6
  1. #1
    h3nch is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default help with drawString

    I'm having a little problem with getting this to work properly, I extend the JComponent class when trying to create my own sort of large text (used for a welcome message) class. So what I want to do is just create an instance of this class and add it to my frame. But the text just doesn't get displayed. Here are the 2 classes I am using:

    Java Code:
    public class GrdSizeFrame
    {
    	public GrdSizeFrame()
    	{
    		final JFrame frame = new JFrame();
    		final int FRAME_WIDTH = 200;
    		final int FRAME_HEIGHT = 250;
    		final int FRAME_X = 500;
    		final int FRAME_Y = 150;
    
    		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		frame.setTitle("Grid Size");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLocation(FRAME_X, FRAME_Y);
    
    		JPanel panel4 = new JPanel();
                    // I've tried a variation of sizes (none worked)
    		WelcomeHeader wHead = new WelcomeHeader(200,250);
    		panel4.add(wHead);
    		frame.add(panel4);
    		
    		frame.setVisible(true);
    	}
    }
    Java Code:
    public class WelcomeHeader extends JComponent
    {
    	private int x;
    	private int y;
    
    	public WelcomeHeader(int X, int Y)
    	{
    		x = X;
    		y = Y;
    	}
    
    	public void paintComponent(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawString("Welcome!", x, y);
    	}
    }
    Last edited by h3nch; 01-15-2010 at 01:02 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Myself, I'd use a JLabel for this sort of thing and simply set its text. I don't have a Java compiler handy, but I'm wondering if you're running into a preferredSize problem -- that your JComponent's preferredSize may be 0, 0. What if you simply add the JComponent to the JFrame (since JFrame uses BorderLayout by default) and not add it to a JPanel? Also you should call pack() on the JFrame before setVisible(true).

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Edit: yes, that's your problem, that and making your JFrame size smaller than what's needed to show the String.

  4. #4
    h3nch is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    thanks for the help I'll try this out later and tell you if it worked

  5. #5
    h3nch is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    It worked but you were right, using JLabel with the setFont method is easier

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. AWT drawString doesn't work on Linux
    By dishab in forum Advanced Java
    Replies: 7
    Last Post: 12-15-2010, 09:43 PM
  2. printing an array of String through drawString
    By kaemonsaionji in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 04:38 PM
  3. drawString with Chinese Characters
    By vaskarbasak in forum Advanced Java
    Replies: 1
    Last Post: 06-10-2008, 07:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •