Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 49

Thread: Analogue Clock Problem

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Analogue Clock Problem

    Hello!

    My first post! Woo!

    I´m making an analogue clock which consists of a bike wheel as "the clock" and three pointers which are images of different flowers!
    I´m having problem with displaying the flowers: rose, pink and weird on the frame.

    The project consists of three classes: Main, clock and pointer.

    The project is far from done but I really want to fix this before proceeding!

    Main:
    import javax.swing.JFrame;
     
    public class Main extends JFrame {
     
       public Main(){
          add(new Clock());
          setTitle("Klocka");
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setSize(400,400);
          setVisible(true);
          setLocationRelativeTo(null);
          setResizable(false);
       }
     
       public static void main(String[] args){
          new Main();
       }
    }

    Clock:

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
     
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
     
     
    public class Clock extends JPanel {
     
    	private Image pink;
    	private Image weird;
    	private Image rose;
     
     
    	private Image back;
     
    	public Pointer pPink;
    	public Pointer pWeird;
    	public Pointer pRose;
     
     
     
       public Clock(){
    	   pPink = new Pointer(3600,pink);
    	   pWeird = new Pointer(60,weird);
    	   pRose = new Pointer(1,rose);
     
       }
     
       public void paint(Graphics g) {
    	   if (pPink.img != null){
    		   g.drawImage(pPink.getImg(), 200, 200, null);
    		   }
     
    	   if (pWeird.img != null){
    		   g.drawImage(pWeird.getImg(), 200, 200, null);
    		   }
     
    	   if (pRose.img != null){
    		   g.drawImage(pRose.getImg(), 200, 200, null);
    		   }
     
     
    	   ImageIcon i = new ImageIcon(this.getClass().getResource("wheel.png"));
    	   back = i.getImage();
    	   g.drawImage(back,50,50, null);
     
       }
     
     
     
     
    }

    Pointer:

    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.ImageIcon;
     
     
    public class Pointer {
     
    	public int speed;
    	public Image img; 
     
       public Pointer(int speed, Image img) {
          this.speed = speed;
          this.img = img;
     
       }
     
     
       public Image getImg(){
    	   ImageIcon ii = new ImageIcon(this.getClass().getResource(img+".jpg"));
    	   img = ii.getImage();
    	   return img;
     
       }
     
     
     
     
    }

    Thankful for help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    Can you describe what you see when you execute the code?
    Are all the images found and loaded? Add some printlns to show their values after they have been loaded.

    Your code would be more efficient if the images were only loaded once in the Pointer class's constructor instead of loading them each time they are requested

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    I see the bike wheel (wheel.png) when I execute the code (which I want). When I insert a System.out.println("this is loaded"); in the getImg() method, nothing happens. So the problem is in the getImg() or in the Pointers constructor I believe.

    How would I structure it so that the images are loaded in the Pointer constructor? Remove the getImg()?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    Did you do this:
    Are all the images found and loaded?
    Add some printlns to show their values after they have been loaded.
    What values were printed out for the images?

    When I insert a System.out.println("this is loaded"); in the getImg() method, nothing happens.
    If the message did not print, then the code was not executed.

    Are there any error messages?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    How would I structure it so that the images are loaded in the Pointer constructor?
    Move this code to the constructor:
        ImageIcon ii = new ImageIcon(this.getClass().getResource(img+".jpg"));
       img = ii.getImage();

  6. #6

    Default Re: Analogue Clock Problem

    I am unsure as to why you have chosen to add:

    this.getClass().getResource(img+".jpg")
    Java's ImageIcon constructor can take an Image.

    ImageIcon imageIcon = new ImageIcon(this.img)

    and you can:

    return imageIcon.getImage()

    Maybe the error is in where you place the image on the clock. You can try removing the clock image for now and only displaying the flowers.
    Last edited by kenster421; September 1st, 2011 at 12:49 PM. Reason: Aha Moment
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    The constructor's args are messed up. He's mixing apples and oranges to build an image filename.

  8. #8
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    No there are no error messages. It compiles correct and I see a window with a bike wheel in it.

    I am unsure as to why you have chosen to add:

    this.getClass().getResource(img+".jpg")
    Java's ImageIcon constructor can take an Image.
    I did like they did in here: (scroll down a bit for the Image part).
    Basics

    What do I do wrong with the constructor args?

    Thanks for all the help so far!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    Add some printlns to print the values the images loaded in Pointer after they have been loaded.
    What is printed out???

    Also print out the full filename of the image before it is loaded

  10. #10
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    When I move this to the pointer-constructor: ImageIcon ii = new ImageIcon(this.getClass().getResource(img+".jpg")) ;
    img = ii.getImage();, i get error messages and it wont compile (I changed img to this.img the second try).

    Errors:
    Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at Pointer.<init>(Pointer.java:16)
    at Clock.<init>(Clock.java:27)
    at Main.<init>(Main.java:7)
    at Main.main(Main.java:17)

    Also, I don´t get what you meant with:
    Add some printlns to print the values the images loaded in Pointer after they have been loaded.
    What is printed out???

    Also print out the full filename of the image before it is loaded
    First - what?
    Second - full filename? and why full filename?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    What I'm trying to help you learn is how to debug your code. If you print out the values of the variables that you are using, you will see what some of the problems are with your code.
    First - what?
    Add some printlns to print the values the images For example:
    System.out.println("img=" + img);

    Second - full filename? and why full filename?
    Do you know what the argument for the getResource() method is?
    Print the full value of the argument to getResource() before calling the method.

  12. #12
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    Yes, I understand that you learn me debug tips, simply just didn´t understand what you wrote that i should do. However, the wheel picture is okay and everything, but the flower pics wont be shown. I´ve put printlns before loading the pics to try to understand where the problem is, but nothing happens so I guess the problem is how I´ve structured the code.

    When I input this println it´s not shown when compiled, and if I remove the if statement I get tons of errors.
    public void paint(Graphics g) {
    if (pPink.img != null){
    System.out.println("works?");
    g.drawImage(pPink.getImg(), 200, 200, null);
    }
    }
    Again, thankful for all the help I can get!

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    Do you know what the argument for the getResource() method is?
    Print the full value of the argument to getResource() before calling the method.
    What don't you understand about this request? In your code you call the getResource() method and pass it an argument. Print that out to see what it is.

    ImageIcon ii = new ImageIcon(this.getClass().getResource(img+".jpg"));

    Add this statement just before the above statement:
    System.out.println("args=" + img+".jpg"); // show args to getResource

  14. #14
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    When i insert that println nothing happens. Nothing happens when i insert a println before calling
    the getImg() method either. The imageIcon snippet is still in the getImg() method since there are several errors when placing the snippet in the pointer constructor.

    When i insert a println in the pointer constructor it's written 3 times when the code compiles. One time for each flower object.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    i insert a println in the pointer constructor it's written 3 times
    What is printed out? Copy and paste the print out here.

  16. #16
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    img=null.jpg
    img=null.jpg
    img=null.jpg

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    Do you understand what that print out is showing?
    Are there any images with that name?
    As a test, copy and rename one of your images and give it the name: null.jpg

  18. #18
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    That the img is not defined to an image and therefore is a null.jpg. Of course there are no images that is named null.jpg (as the code tells, there are wheel, pink, rose and weird).

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    What were you printing out? Look at the print statement again. Was it the filename of the image?
    I thought what you were printing was the filename of the image.
    Why is your image filename null?

    I asked you to put this statement in your program:
    System.out.println("args=" + img+".jpg"); // show args to getResource

    But the printout you show does NOT have "args=" it has "img=". Why is that?
    Did you do what I asked or is the print out from another place in your program?

  20. #20
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    Im using my android now so i cant check the code but i believe i put
    System.out.println("img=" + img + ".jpg"); into the program. But it doesnt matter what string u write since it's not a reference to anything?

    Why the filename is null is probably cause img has no image reference, yet

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    Why the filename is null
    The filename should be a String. The value of a String has NOTHING to do the the contents of a file.

    What are the filenames of the images you want to display? Why isn't that filename being passed to the class's constructor? Why is a null value being passed?

  22. #22
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    The filenames are: pink.jpg, weird.jpg and rose.jpg.
    Here´s the code with some changes. When compiled I see the wheel pic and the printlns: fileName=null.jpg, three times on separate lines.

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

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


    public class Clock extends JPanel {

    private String pink;
    private String weird;
    private String rose;


    private Image back;

    public Pointer pPink;
    public Pointer pWeird;
    public Pointer pRose;



    public Clock(){
    pPink = new Pointer(3600,pink);
    pWeird = new Pointer(60,weird);
    pRose = new Pointer(1,rose);

    }

    public void paint(Graphics g) {
    if (pPink.img != null){
    System.out.println("got through if statement pPink");
    g.drawImage(pPink.getImg(), 200, 200, null);
    }

    if (pWeird.img != null){
    System.out.println("got through if statement pWeird");
    g.drawImage(pWeird.getImg(), 250, 200, null);
    }

    if (pRose.img != null){
    System.out.println("got through if statement pRose");
    g.drawImage(pRose.getImg(), 300, 200, null);
    }



    ImageIcon i = new ImageIcon(this.getClass().getResource("wheel.png") );
    back = i.getImage();
    g.drawImage(back,50,50, null);

    }
    Pointer:

    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.ImageIcon;


    public class Pointer {

    public int speed;
    public String fileName;
    public Image img;

    public Pointer(int speed, String fileName) {
    this.speed = speed;
    this.fileName = fileName;
    System.out.println("fileName=" + this.fileName + ".jpg");

    }


    public Image getImg(){
    System.out.println("args=" + fileName + ".jpg");
    ImageIcon ii = new ImageIcon(this.getClass().getResource(fileName+".j pg"));
    img = ii.getImage();
    return img;

    }




    }

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    Where do you give a value to the filename arguments that are passed to the Pointer constructor?
    Why are they null?

  24. #24
    Member
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Analogue Clock Problem

    public Clock(){
    pPink = new Pointer(3600,pink);
    pWeird = new Pointer(60,weird);
    pRose = new Pointer(1,rose);

    }

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analogue Clock Problem

    What you posted does NOT give values to the pink, weird and rose variables.
    Where do you give them values?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 6
    Last Post: January 28th, 2011, 01:13 AM
  2. Making a clock
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 7th, 2011, 04:36 PM
  3. Need help in developing a clock application
    By sb.shiv in forum AWT / Java Swing
    Replies: 2
    Last Post: August 24th, 2010, 02:00 AM
  4. help with clock class
    By collinsislee in forum Object Oriented Programming
    Replies: 1
    Last Post: February 24th, 2010, 02:53 PM

Tags for this Thread