Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: java tutorial help

  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default java tutorial help

    I am trying to figure out why a program won't animate a picture of a star correctly.

    here are the two classes:


    package star;

    import javax.swing.JFrame;

    public class Star extends JFrame {

    public Star() {

    add(new Board());

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(280, 240);
    setLocationRelativeTo(null);
    setTitle("Star");
    setResizable(false);
    setVisible(true);

    }

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



    ------------------------------------------------------


    package star;

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;

    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class Board extends JPanel implements ActionListener {

    Image star;
    Timer timer;
    int x, y;

    public Board() {
    setBackground(Color.BLACK);

    ImageIcon ii =
    new ImageIcon(this.getClass().getResource("star.png")) ;
    star = ii.getImage();

    setDoubleBuffered(true);

    x = y = 10;
    timer = new Timer(25, this);
    timer.start();
    }


    public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2d = (Graphics2D)g;
    g2d.drawImage(star, x, y, this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
    }


    public void actionPerformed(ActionEvent e) {

    x += 1;
    y += 1;

    if (y > 240) {
    y = -45;
    x = -45;
    }
    repaint();
    }
    }


    ---------------------------------------------------------------------

    This is what happens when I compile it:

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
    at Star.Board.<init>(Board.java:30)
    at Star.Star.<init>(Star.java:16)
    at Star.Star.main(Star.java:28)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)''


    -and nothing else happens. Using NetBeans

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

    Default Re: java tutorial help

    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:181)
    at Star.Board.<init>(Board.java:30)
    That's an execution error (not compiler).
    Look at line 30 in Board and see what variable has a null value. Is the path to the image file correct?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: java tutorial help

    Quote Originally Posted by Norm View Post
    That's an execution error (not compiler).
    Look at line 30 in Board and see what variable has a null value. Is the path to the image file correct?
    I think so. I downloaded a star image before I started and when I right click on the image and go to details it says the files name is "star.png".

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

    Default Re: java tutorial help

    To see if the program is finding the file, print out this: this.getClass().getResource("star.png")
    to see if it returns a valid URL.
    Last edited by Norm; 07-30-2012 at 06:25 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: java tutorial help

    Quote Originally Posted by Norm View Post
    To see if the program is finding the file, print out this: this.getClass().getResource("star.png")
    to see if it returns a valid URL.

    This is a very beginner question but to test small segments of code like the segment above do you need to create a new project. Could you give a step by step example?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: java tutorial help

    Store the star.png file in the same directory as your Board.class file and read how resources are found.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: java tutorial help

    Sorry, I don't use projects. I create .java files and use the javac and java programs.

    What I was asking you to try was to add ONE line of code to print out the URL returned by getResource(). Add the line just before the statement with the ImageIcon .
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: java tutorial help

    Awesome, that did it. Thank you so much.

  9. #9
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: java tutorial help

    Quote Originally Posted by JosAH View Post
    Store the star.png file in the same directory as your Board.class file and read how resources are found.

    kind regards,

    Jos
    JosAH, is this what you are talking about? --> Accessing Resources
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: java tutorial help

    Quote Originally Posted by awinston View Post
    JosAH, is this what you are talking about? --> Accessing Resources
    Yep, but there must be a newer version of that article (that version is fifteen years old).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: java tutorial help

    Here is one from 3 years ago (not from Oracle): Accessing a Resource in Java « Sandeep Verma
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

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

    Default Re: java tutorial help

    And here's where Oracle have hidden the stuff in their tutorials: How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

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

Similar Threads

  1. Replies: 0
    Last Post: 05-13-2012, 09:37 PM
  2. Replies: 0
    Last Post: 05-13-2012, 09:26 PM
  3. Replies: 0
    Last Post: 05-07-2012, 10:46 PM
  4. Replies: 0
    Last Post: 04-06-2012, 09:24 PM
  5. Replies: 0
    Last Post: 03-26-2012, 02:11 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
  •