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

Thread: How overloaded paint() works?

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default How overloaded paint() works?

    Hi, I've just recently started studying Java. While I've been studying c++ for the past few years, there's a project I want to do that I feel Java best suits my needs because all the built in GUI features. I've followed a few tutorials on painting a string to a JFrame and I tried doing this on my own and I apparently didn't learn anything.

    I don't understand what is required for drawing a string to a window or jframe anyway. I believe I need to create a container of some sort and draw to that because I believe I read somewhere that you can't draw directly to a JFrame object. If someone could explain the logic behind how it works that would be nice.


    package rago;
     
    import java.awt.*;
    import javax.swing.*;
     
    class rago extends JFrame
    {
        public rago()
        {
            JFrame f = new JFrame("rago");
     
            f.setSize(600, 600);
     
            f.setVisible(true);
        }
     
        public void paint(Graphics g)
        {
            g.setFont(new Font("Courier New", Font.PLAIN, 25));
            g.drawString("Hello World", 250, 250);
        }
    }
     
    public class Main
    {
        public static void main(String[] args)
        {
            rago r = new rago();
        }
    }


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: How overloaded paint() works?

    Hi maikeru .

    you have made some mistakes that the Frame is a container ,you can't directly put something non-component in it.
    For your sample,you should first create a panel object that hava override the paint method in order to draw anything you want.I will show you the code i 've tested in my PC
    import java.awt.*;
    import javax.swing.*;
     
    class MyPanel extends JPanel{
     
    public void paint(Graphics g){
    g.setFont(new Font("Courier New", Font.PLAIN, 25));
    g.drawString("Hello World", 250, 250);
    }
    }
    public class rago extends JFrame{
     
    MyPanel panel;
    public rago(){
    super("rago");
    panel=new MyPanel();
    this.getContentPane().add(panel,BorderLayout.CENTER);
    this.setSize(600,600);
    this.setVisible(true);
    }
     
    public static void main(String args[]){
    rago sample=new rago();
    }
    }

  3. The Following User Says Thank You to lewvan00 For This Useful Post:

    maikeru (December 21st, 2009)

  4. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: How overloaded paint() works?

    I should apologize for you.I am so dazed

    Actually ,you can directly draw in the Frame object.
    the code has been amended
    import java.awt.*;
    import javax.swing.*;
     
    public class rago extends JFrame{
     
    public rago(){
    super("rago");
    this.setSize(600,600);
    this.setVisible(true);
    }
    public void paint(Graphics g){
     g.setFont(new Font("Courier New", Font.PLAIN, 25));
     g.drawString("Hello World", 250, 250);
    }
     
    public static void main(String s[]){
    new rago();
    }
    }

  5. The Following User Says Thank You to lewvan00 For This Useful Post:

    maikeru (December 21st, 2009)

  6. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: How overloaded paint() works?

    As pointed out you can draw directly to a JFrame, but its more proper to draw to a JPanel and add it to the JFrame (makes the code not only more readable but more importantly more adaptable if you are adding components to the JFrame). Something else to note when drawing in swing, its better practice to do your custom painting in the paintComponent method rather than the paint method. However you do your painting though, you should ALWAYS call super on the parent function

    public void paintComponent(Graphics g){
     super.paintComponent(g);
     g.setFont(new Font("Courier New", Font.PLAIN, 25));
     g.drawString("Hello World", 250, 250);
    }

    As for the logic: to draw graphics in swing, all you really need is to over-ride the paintComponent method, call the super method, and then utilize the graphics object passed to it to do the drawing. But if you want more nitty gritty details, see Painting in AWT and Swing

  7. The Following User Says Thank You to copeg For This Useful Post:

    maikeru (December 21st, 2009)

  8. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: How overloaded paint() works?

    Thank you everyone. I finally got it working.
    Last edited by maikeru; December 21st, 2009 at 12:08 PM.

  9. #6
    Junior Member
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: How overloaded paint() works?

    Well, I don't know what I'm doing wrong, but it seems no matter what, I can't do anything from scratch after reading on something. I can't even get a simple image to draw again. You don't have to bother explaining, I just find it odd and I apparently have no understanding whatsoever of how Java works. I think it's because so much stuff is hidden in Java as compared to c++. I followed the tutorial given by Sun themselves and it won't work. I'll probably take a vent break and start over as if I know nothing. Thanks for your help anyway, though.

    *edit*

    I think it has something to do with Windows 7. I switched over to my Vista installation and the code works fine.
    Last edited by maikeru; December 21st, 2009 at 06:12 PM.

Similar Threads

  1. How database connection pooling works in a application
    By JayVirk in forum JDBC & Databases
    Replies: 0
    Last Post: October 10th, 2009, 07:14 AM