Results 1 to 5 of 5
  1. #1
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default Trying to understand the whole 2D thing.

    Hello,
    I'm trying to figure out how to draw stuff in java. I'm building an application, not an applet. I have a class dedicated to drawing lines etc (awt.geom figures). The parameters for the class I'm using , paint(Graphics g), are of the type Graphics... how do I call this method from the Main class's main method? Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    how do I call this method from the Main class's main method?
    You don't call the paint method directly. You call repaint which asks the JVM to call the paint method ASAP on one of the JVM's threads with the Graphics object.

    There are exceptions for special situations like printing and image generation where you get a Graphics object and call paint directly, but I don't think you are doing that.

  3. #3
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default

    ok, I understand a bit more having heard that, and I may have made some progress. Here's my code for my class for drawing stuff. At this point I just want it to draw a simple line.

    Java Code:
    // imports... omitted to save space in forum
    public class draw  extends JApplet implements Runnable {
        
     public draw()// constructor
        {}
    
    public void run()
        {this.repaint();}
    
        public void paint(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
    
            g2.setColor(Color.red);
             g2.draw(new Line2D.Float(1,1,100,100));
    
        }
    }
    I then create an instance of draw in the main method and call the method run of that instance. As far as I know that should be enough to draw the line. Do you have any ideas?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Do a Search here for sample code using the paintComponent method.
    Normally paint is down on a JPanel that is added to a JFrame.

    The code you posted doesn't go with the words you posted.
    The posted code is an applet and does not use the main method. Applets run in a browser.
    To run the code as a desktop app, the class should extend the JFrame class or be added to a JFrame.

  5. #5
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default

    Ok... I managed to figure it out. THanks, Norm.

    Just for the record: amazing tutorial here:

    Java Tutorial: Introduction to the Java 2D API

Similar Threads

  1. How to do a simple class+object thing
    By ilop12 in forum New To Java
    Replies: 8
    Last Post: 06-03-2010, 04:08 PM
  2. please i need help with a simple thing
    By zohdi in forum New To Java
    Replies: 28
    Last Post: 05-30-2010, 01:41 AM
  3. A new thing arrives...
    By ewomack in forum Introductions
    Replies: 3
    Last Post: 10-09-2009, 09:16 PM
  4. What did i do wrong on thing method?
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 08:37 AM
  5. PLz i really need help on this final thing
    By jason27131 in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 02:31 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
  •