Results 1 to 3 of 3
  1. #1
    PeterPan is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Red face Paint method, need help

    Hello Everyone,

    My problem is that I don't understand why the paint method works, (display the string on the frame) although
    it is not called in the Main method or in the constructor.

    Please see the code below:


    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;

    public class JavaGame extends JFrame
    {


    public JavaGame()
    {
    setTitle("Java Game");
    setSize(550,550);
    setResizable(true);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void paint (Graphics g) // THIS METHOD SHOULD NOT RUN, without being called in the custructor
    {
    g.drawString("This is my first game", 75, 75);
    g.drawLine(30, 30, 80, 80);


    }



    public static void main(String[] args)
    {
    new JavaGame();
    }
    }

    Thank you in advance for your help!
    Peter.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Paint method, need help

    Painting in AWT and Swing

    And you're doing it wrong. Don't try to do custom painting in a paint(...) override in a top level window. Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Moving this to the AWT/Swing section.

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

  3. #3
    PeterPan is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Paint method, need help

    Thank you Darry, I will read the lesson, hopefully I will clear my ideas.
    Regards.
    Peter.

Similar Threads

  1. paint method
    By larry_d1990 in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2011, 05:45 PM
  2. Using Graphics outside of a paint method
    By Catfish1 in forum New To Java
    Replies: 6
    Last Post: 10-26-2010, 08:57 PM
  3. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 07:07 AM
  4. Java Paint Method?
    By leapinlizard in forum Java 2D
    Replies: 2
    Last Post: 02-11-2010, 07:01 PM
  5. an error in paint method
    By hopey in forum Java 2D
    Replies: 7
    Last Post: 04-24-2009, 10:12 PM

Tags for this Thread

Posting Permissions

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