Results 1 to 6 of 6
  1. #1
    ManyProblems is offline Hello
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Displaying graphics on JFrame

    I just finished my first java book (Java for Dummies by Barry Burd), and I decided to try my own project.
    What this program is supposed to do is create an ball when you press the button.
    Anyway, I really screwed up and I don't know how to fix it.

    Problems:

    1. The ball shows up initially without my ever calling the, uh, method I think is what it's called.
    2. When I press the button, the compiler gives some funky error message. "NullPointerException"




    So here's the program:

    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;


    public class DBall extends JFrame implements ActionListener{
    private static final long serialVersionUID=1L;

    Graphics g;

    public void paint(Graphics g) {
    g.drawOval(80,140, 80, 80);
    }




    JButton createButton = new JButton("Create a ball.");


    public DBall (){


    setTitle("Disappearing Ball");
    pack();
    setSize(400,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());


    add(createButton);
    createButton.addActionListener(this);
    setVisible(true);


    }

    @Override
    public void actionPerformed(ActionEvent e) {

    paint(g);



    }
    }


    And the JFrame creator:


    public class DisplayDTest {


    public static void main(String[] args) {

    new DBall();

    }

    }




    If anyone knows the solution help is greatly appreciated!
    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: Displaying graphics on JFrame

    The NullPointerException (NPE) is occurring because you trying to use a variable that is null. You should inspect the line that is throwing the NPE to see what variables are used on that line to see which could be null. My guess it's this bit:

    Java Code:
    public void actionPerformed(ActionEvent e) {
      paint(g);
    }
    because the Graphics object, g, is likely null.

    Most importantly, you'll want to read up on how to do Swing Graphics because to do it correctly, you have to throw out old assumptions and study what the tutorials suggest should be done. Here are two good tutorials that should get you started:



    The first tutorial is for the very basics and the second is a bit more advanced. Best of luck!

  3. #3
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: Displaying graphics on JFrame

    instead of using paint(g) you must use repaint().

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

    Default Re: Displaying graphics on JFrame

    Quote Originally Posted by HelloWorld1234 View Post
    instead of using paint(g) you must use repaint().
    And also he should draw in a JPanel's paintComponent method, he should call the super's method first. He may wish to use an if block that checks a boolean variable inside of his paintComponent method to help it decide if it will draw the shape or not, and then have the boolean variable changed by his actionPerformed method before calling repaint,...

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

    Default Re: Displaying graphics on JFrame

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

  6. #6
    ManyProblems is offline Hello
    Join Date
    Jul 2012
    Posts
    6
    Rep Power
    0

    Default Re: Displaying graphics on JFrame

    Thanks for your suggestions.

    I realized I'm just going to try to do the tutorial Funerable suggested, and trash this.

    But thanks anyway.

Similar Threads

  1. Question on Graphics/JFrame/KeyEvents
    By loopsnhoops in forum New To Java
    Replies: 4
    Last Post: 02-10-2011, 11:22 PM
  2. Need Help with graphics / Jframe
    By loopsnhoops in forum AWT / Swing
    Replies: 1
    Last Post: 02-10-2011, 11:15 PM
  3. problem with jframe&jmenu&graphics
    By beni.vd in forum AWT / Swing
    Replies: 1
    Last Post: 12-31-2010, 07:07 PM
  4. Jpanel and displaying graphics
    By jdsflash in forum New To Java
    Replies: 6
    Last Post: 11-21-2009, 01:14 AM
  5. Graphics with JFrame
    By kandt in forum New To Java
    Replies: 3
    Last Post: 12-02-2008, 12:51 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
  •