Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree6Likes

Thread: hi, how to add a graphic component , say a line2D, to a swing?

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default hi, how to add a graphic component , say a line2D, to a swing?

    hi, how to add a graphic component , say a line2D, to a swing?
    my graphic line method is ..
    Java Code:
    void paintComponent ()
    {
    Graphics2D g1 = new Graphics2D (g);
    g1.drawLine(1,1,10,10);
    }
    and I have initialized Graphics using "import java.awt.Graphics2D" and geometry using "import java.awt.geom.*"
    but where to call the method "paintComponent()" is it in the method "public static void main(String args[])" or in another method, and Does my codings show error in graphics initialization(Graphics2D g1 = new Graphics (g);) ?

    and I always get the error "your graphics2D object "g" has NULL value

    thx
    regards
    dhilip
    Last edited by noobplus; 03-11-2012 at 02:54 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    What is the code you posted supposed to do? It does not make sense.
    What is the datatype of the paintComponent arg that is passed on line 1?
    What is the method's name?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by Norm View Post
    What is the code you posted supposed to do? It does not make sense.
    What is the datatype of the paintComponent arg that is passed on line 1?
    What is the method's name?
    actually, my doubt is I don't know, where to use the method"paintComponent"
    regards
    dhilip

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    im sorry bro, i typed wrong codings.. now i updated it.
    Actually i typed "void (paintComponent g){" instead of "void paintComponent(){"

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by noobplus View Post
    im sorry bro, i typed wrong codings.. now i updated it.
    Actually i typed "void (paintComponent g){" instead of "void paintComponent(){"
    Too much trouble to actually go through the tutorial, huh?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by noobplus View Post
    actually, my doubt is I don't know, where to use the method"paintComponent"
    It's defined in JComponent, so you would override it in a class that extends JComponent.

    You never call it yourself. The Swing event thread does that for you.
    noobplus likes this.
    Get in the habit of using standard Java naming conventions!

  8. #8
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by kjkrum View Post
    It's defined in JComponent, so you would override it in a class that extends JComponent.

    You never call it yourself. The Swing event thread does that for you.
    thx, and how do we initialize graphics into the method "paintComponent()" ? that would help out my project. my project submission date is coming soon next week.

    upto my knowledge,
    Java Code:
    public class abc extends JComponent
    {
    void paintComponent(Graphics g) 
    {
    Graphics output = new Graphics (g);
    output.drawLine(1,1,10,10);
    }
    }
    // and one more doubt.. can the class that extends JComponent be a public class? (JComponent can work alone? or needs to be attached to a JFrame? If so, should it be initialized into the class extending JFrame? )
    regards
    dhilip
    Last edited by noobplus; 03-12-2012 at 02:39 PM. Reason: adding my signature

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Why try to create an unusable Graphics object (line 5)when there is good one passed to the method?

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Tolls likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by Norm View Post
    Why try to create an unusable Graphics object (line 5)when there is good one passed to the method?
    So, what about , shall i do this new code?
    Java Code:
    public class abc extends JComponent
    {
    void paintComponent(Graphics g)
    {
    //Graphics output = new Graphics (g);
    g.drawLine(1,1,10,10);  //output.drawLine changed to g.drawLine
    }
    }
    Last edited by noobplus; 03-12-2012 at 03:26 PM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Have you compiled and executed it? What happens?

  13. #13
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by Norm View Post
    Have you compiled and executed it? What happens?
    hi,
    I added a main method to it in eclipse. (like given below)

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    public class framecomp extends JComponent
    {
    protected void paintComponent(Graphics g)
    {
    //Graphics output = new Graphics (g);
    g.drawLine(1,1,10,10);  //output.drawLine changed to g.drawLine
    }
    public static void main(String args[])
    {
    	framecomp f = new framecomp();
    	f.paintComponent(null)// this line asks for a proper argument. and what arguments should i give?
    }
    }
    ive mentioned the comment in the main method.
    regards
    dhilip

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    What happens when you execute that code?

    Did you read the link posted in post#10? Especially this part:
    http://docs.oracle.com/javase/tutori...ing/index.html

    It looks like you are making code up instead of following the recommendations shown in the tutorial

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    As Darryl suggests, I think the OP needs to sit down and go through the whole Swing tutorial.
    Currently they're just making stuff up and hoping it works, with no idea at all how Swing fits together.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by Norm View Post
    What happens when you execute that code?

    Did you read the link posted in post#10? Especially this part:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    It looks like you are making code up instead of following the recommendations shown in the tutorial
    there was no class demonstrations.. and yet I found the title "Painting in Swing " paint() and repaint() are called in the class extending the Frame..
    Painting in AWT and Swing
    paint is in package "container" think so,..
    thx for the link,
    so should we initialize graphics in main() method?

    regards
    dhilip

  17. #17
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by Tolls View Post
    As Darryl suggests, I think the OP needs to sit down and go through the whole Swing tutorial.
    Currently they're just making stuff up and hoping it works, with no idea at all how Swing fits together.
    could u give me a demo java class drawing a line in swing and that would help my a lot plz
    regards
    dhilip

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    In the tutorial there is code: SwingPaintDemo2 that draws a String:
    g.drawString("This is my custom Panel!",10,20);
    replace that with:
    g.drawLine(1,1,100,100);

  19. #19
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by Norm View Post
    In the tutorial there is code: SwingPaintDemo2 that draws a String:
    g.drawString("This is my custom Panel!",10,20);
    replace that with:
    g.drawLine(1,1,100,100);
    working on that. thx
    regards
    dhilip

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: hi, how to add a graphic component , say a line2D, to a swing?

    Quote Originally Posted by DarrylBurke View Post
    Quote Originally Posted by noobplus View Post
    could u give me a demo java class drawing a line in swing and that would help my a lot plz
    regards
    dhilip
    No.
    Go through the link above provided by Darryl.
    You will have to do this if you want to work with Swing GUIs.
    You clearly don't know enough about it at the moment. Providing code will simply make things worse.
    Norm and noobplus like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Page 1 of 2 12 LastLast

Similar Threads

  1. embed Flex component in Java Swing
    By monireh in forum AWT / Swing
    Replies: 0
    Last Post: 02-24-2011, 03:28 PM
  2. Which swing component is suitable for TextEditor...?
    By Dinesh_rockz in forum AWT / Swing
    Replies: 1
    Last Post: 02-23-2011, 06:41 AM
  3. swing component
    By cahyadiakbar in forum AWT / Swing
    Replies: 0
    Last Post: 09-22-2010, 11:40 AM
  4. Require Links for free swing component
    By Gajesh Tripathi in forum Advanced Java
    Replies: 2
    Last Post: 08-11-2007, 11:24 PM
  5. Swing Graphic interface
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 06-28-2007, 05:39 PM

Posting Permissions

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