Results 1 to 9 of 9
Thread: Incorrect information?
- 02-27-2012, 12:48 AM #1
Incorrect information?
Is this example wrong? Drawing Graphics - Using paint() on Frame
I was trying to paint on a JFrame and I remembered that you cant paint on a JFrame but you can paint on its contentPane like JPanel ?
I think that you can paint on a Frame however?
Just got confused by this example =) Thanks...
-
Re: Incorrect information?
You can draw directly on a JFrame but you really shouldn't. Much better is to draw in a JComponent such as a JPanel and add that to the JFrame, or make it its contentPane.
- 02-27-2012, 12:53 AM #3
Re: Incorrect information?
Can you show me an example? =) That one does not work for me ... Thanks =)
-
- 02-27-2012, 12:59 AM #5
Re: Incorrect information?
Well I searched on it now in the forum JFrame help and you should correct this post if what you're saying is true.
Just a paint example on the JFrame ... can't seem to get mine to work =P must be doing something wrong ... Thanks again =)
-
Re: Incorrect information?
What I'm saying is true -- you can draw directly in a JFrame, but you shouldn't do it. For instance,
But much better is to draw in a JPanel and add it to the JFrame's contentPane:Java Code:import java.awt.*; import javax.swing.*; public class FrameDraw extends JFrame { public FrameDraw(String title) { super(title); } @Override public Dimension getPreferredSize() { return new Dimension(400, 400); } @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.blue); g.drawRect(50, 50, 300, 300); } private static void createAndShowGui() { JFrame frame = new FrameDraw("FrameDraw"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
Java Code:import java.awt.*; import javax.swing.*; public class PanelDraw extends JPanel { @Override public Dimension getPreferredSize() { return new Dimension(400, 400); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.blue); g.drawRect(50, 50, 300, 300); } private static void createAndShowGui() { PanelDraw mainPanel = new PanelDraw(); JFrame frame = new JFrame("PanelDraw"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } }
- 02-27-2012, 01:18 AM #7
Re: Incorrect information?
It appears you are correct ... =) I know that you should not do it ... I was trying to impress my girlfriend and it failed =P
What did i mess up?
ThanksJava Code:public class Frame extends JFrame{ public Frame(){ setTitle("Sofie"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(800, 600); setResizable(true); setLocationRelativeTo(null); setVisible(true); } @Override public void paint(Graphics g){ super.paint(g); g.drawString("Hej", 20, 20); } public static void main(String[] args) { Frame f = new Frame(); } }
-
Re: Incorrect information?
You code is fine except for where you are drawing your text. Note that when you draw directly on the JFrame that includes the region of the title bar (and this isn't the case for drawing to a JPanel). Also, I believe that the x and y values used by drawString are the lower left corner of the text String. So change those x and y values so that the text is visible and your lady love should be very impressed with you.
- 02-27-2012, 01:35 AM #9
Similar Threads
-
Incorrect formulas
By kprofgold in forum New To JavaReplies: 46Last Post: 01-31-2012, 03:27 PM -
getting incorrect output nested for loop
By leoshiner in forum New To JavaReplies: 2Last Post: 05-02-2011, 10:29 PM -
lucene3.0.2: getting incorrect no. of occurrence in file
By ranjitots in forum LuceneReplies: 0Last Post: 12-06-2010, 03:36 PM -
What in my function incorrect?
By artemff in forum New To JavaReplies: 5Last Post: 01-02-2010, 04:25 PM -
Incorrect Package? Help!
By chaits86 in forum NetBeansReplies: 10Last Post: 12-17-2008, 03:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks