Results 1 to 6 of 6
Thread: help with drawString
- 01-15-2010, 12:51 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 9
- Rep Power
- 0
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.
-
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).
-
Edit: yes, that's your problem, that and making your JFrame size smaller than what's needed to show the String.
- 01-15-2010, 01:32 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 9
- Rep Power
- 0
thanks for the help I'll try this out later and tell you if it worked
- 01-16-2010, 02:21 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 9
- Rep Power
- 0
It worked but you were right, using JLabel with the setFont method is easier
-
Similar Threads
-
AWT drawString doesn't work on Linux
By dishab in forum Advanced JavaReplies: 7Last Post: 12-15-2010, 09:43 PM -
printing an array of String through drawString
By kaemonsaionji in forum New To JavaReplies: 1Last Post: 02-23-2009, 04:38 PM -
drawString with Chinese Characters
By vaskarbasak in forum Advanced JavaReplies: 1Last Post: 06-10-2008, 07:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks