Results 1 to 9 of 9
  1. #1
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

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

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

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

  3. #3
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Incorrect information?

    Can you show me an example? =) That one does not work for me ... Thanks =)

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

    Default Re: Incorrect information?

    Quote Originally Posted by santa View Post
    Can you show me an example? =) That one does not work for me ... Thanks =)
    I'm sorry, an example specifically of what? Of drawing in a JPanel that is loaded in a JFrame's contentPane?

  5. #5
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Incorrect information?

    Quote Originally Posted by Fubarable View Post
    I'm sorry, an example specifically of what? Of drawing in a JPanel that is loaded in a JFrame's contentPane?
    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 =)

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

    Default Re: Incorrect information?

    What I'm saying is true -- you can draw directly in a JFrame, but you shouldn't do it. For instance,

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

  7. #7
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

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

    Java 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();
    
        }
    }
    Thanks

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

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

  9. #9
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default Re: Incorrect information?

    Quote Originally Posted by Fubarable View Post
    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.
    haha It's to late man ... she thinks i'm a loser now and probably files a divorce as we speak. If you guys need me I'll be living in a paper box by the river.

    no i'm just kidding I'm 22 ;) but still that was pretty embarrassing stuff...

Similar Threads

  1. Incorrect formulas
    By kprofgold in forum New To Java
    Replies: 46
    Last Post: 01-31-2012, 04:27 PM
  2. getting incorrect output nested for loop
    By leoshiner in forum New To Java
    Replies: 2
    Last Post: 05-02-2011, 11:29 PM
  3. Replies: 0
    Last Post: 12-06-2010, 04:36 PM
  4. What in my function incorrect?
    By artemff in forum New To Java
    Replies: 5
    Last Post: 01-02-2010, 05:25 PM
  5. Incorrect Package? Help!
    By chaits86 in forum NetBeans
    Replies: 10
    Last Post: 12-17-2008, 04:08 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
  •