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 2 of 2 FirstFirst 12
Results 26 to 49 of 49

Thread: Analogue Clock Problem

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

    Default Re: Analogue Clock Problem

    My idea of structuring this was to create these three pointer objects with the variables speed and fileName. The fileName that then is either pink, weird or rose are used in this method to get the Image for the flower:
    public Image getImg(){
    System.out.println("args=" + fileName + ".jpg");
    ImageIcon ii = new ImageIcon(this.getClass().getResource(fileName+".j pg"));
    img = ii.getImage();
    return img;

    Then the pics are displayed through calls like this one: g.drawImage(pRose.getImg(), 300, 200, null);

  2. #27
    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

    You are missing my question.
    Where do you assign values to the image filename variables?
    You assign values to a variable by using an assignment statement like this:

    var = "value"; // assign a value to var

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

    Default Re: Analogue Clock Problem

    I donīt understand your question. I know how to assign a value to a var, but you got the code so I donīt see why you donīt just check it since itīs so short.

  4. #29
    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

    If you are not interested enough in making your code work to look at it and see where you assign a value to the filename variables, I don't know what else I can do to help you.

    Where do you assign values to the image filename variables?

    I give you the answer: You never assign a value.

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

    Default Re: Analogue Clock Problem

    I think nobody can blame me for not being interested in solving this issue since Iīve been watching the code over and over the last days. Iīm new to programming so itīs of course possible to not see the misstakes since I got quite little experience with working with code yet.

    pPink = new Pointer(3600,pink);
    I thought pink was assigned to fileName when running the code?

  6. #31
    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

    Let's start with a simple question: What are the filenames of the images that you want to view?
    The filenames are represented as Strings in your program.


    I thought pink was assigned to fileName when running the code?
    Where in your code is there an assignment statement that starts with this:
    pink =

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

    Default Re: Analogue Clock Problem

    The filenames are: pink, weird and rose

    I canīt answer your second question but to again tell you about the idea about how it works, since pink not = something, but becomes a part of the pink.jpg

    When pPink is created it is created with 3600 and pink as arguments for its constructor:
    pPink = new Pointer(3600,pink);

    The pointer constructor:
    public Pointer(int speed, String fileName)

    The arguments are passed in:
    public Pointer(3600, pink)
    this.fileName=fileName(pink)

    So fileName = pink

    Later the fileName(pink) is used as a part of the string that gets the pink.jpg image which becomes img and is returned.

  8. #33
    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 filenames are: pink, weird and rose
    I think you are wrong there.
    I think the image filenames are: "pink.jpg" "weird.jpg" and "rose.jpg"
    These are the names of the files on your disk.

    I think your problem is that you are using a variable named pink and not a String with the value: "pink".

    Where in your code do you assign a value to the variable pink. An assignment statement would start with this: pink =
    For example:
    pink = "A faint red"; // assign a value to the variable pink

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

    Default Re: Analogue Clock Problem

    Yes, the filenames on my computer are pink.jpg, rose.jpg and weird.jpg. But, the variable fileName may be just pink, rose or weird, so there is a slight confusion there.

    Your second paragraph: I get it, but I donīt know how to rearrange my code so that it will work. I get that "private String pink;" is one thing and "String bla = pink" is another.

    Nowhere directly, but I donīt get why you ask me the same question again when I couldnīt answer it the first time.

  10. #35
    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 you ask me the same question again when I couldnīt answer it the first time.
    I'll try to ask very simple questions one at a time to see if you can understand it.
    Do you know what an assignment statement is?
    Please post some assignment statements that assign values to a variable named pink.

  11. #36
    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

    A suggestion. Simplify your code by passing the image filename to the constructor:
    pPink = new Pointer(3600, "pink.jpg");

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

    Default Re: Analogue Clock Problem

    Do you know what an assignment statement is?
    Please post some assignment statements that assign values to a variable named pink.
    pink = something; //pink becomes something

    I know what an assignment statement is.

    I removed public String pink; (and rose, weird)
    I did as you said and used this snippet:
    pPink = new Pointer(3600, "pink.jpg");
    i changed some stuff in the Pointer constructor so that it would compile.
    When it ran, no pics but the wheel was shown, these printlns were also written:
    fileName=pink.jpg
    fileName=weird.jpg
    fileName=rose.jpg
    So thereīs progress

  13. #38
    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

    If you are passing the image file filenames to the constructor, next you should check that the constructor is using the filenames correctly to load the files.
    Create a String that will be used in the getResource() method call and print its value before using it.
    String fPath = .... // Build the filename String
    System.out.println("fPath=" +fPath); // print out its value
    ...getResource(fPath) // use it to get the image

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

    Default Re: Analogue Clock Problem

    Why introduce the new variable fPath when i had the variable fileName that did the same thing?

    Nothing in my getImg() method ever happens during runtime (no printlns are executed etc), so I guess thereīs problems when calling the getImg().

  15. #40
    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

    My code was only meant as an example. You need to write the code that works with your program.

    Did you make the changes I suggested? What prints out for the value of the parameter that is used by the getResource() method? Is that path to the image file correct?
    Does the image get loaded? Test for a null value returned.

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

    Default Re: Analogue Clock Problem

    Everything you wanted me to do was already in there.

    Pointer class:

    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);

    }


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

    }




    }
    fileName=pink.jpg
    fileName=weird.jpg
    fileName=rose.jpg
    is the output, so we just want to fix the issues with calling getImg()

  17. #42
    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 loading of the images done in the getImg method should be moved to the constructor so that the image is only loaded once.

    This is wrong. Don't add the + "jpg" in the print out:
    System.out.println("args=" + fileName + ".jpg");
    You should build the full filename and assign it to a variable and then print it and use it.

    See post#38

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

    Default Re: Analogue Clock Problem

    I moved the loading of images to the constructor and it worked. The flowers are now drawn. The output is:
    fileName=pink.jpg
    fileName=weird.jpg
    fileName=rose.jpg
    got through if statement pPink
    args=pink.jpg
    got through if statement pWeird
    args=weird.jpg
    got through if statement pRose
    args=rose.jpg
    got through if statement pPink
    args=pink.jpg
    got through if statement pWeird
    args=weird.jpg
    got through if statement pRose
    args=rose.jpg
    got through if statement pPink
    args=pink.jpg
    got through if statement pWeird
    args=weird.jpg
    got through if statement pRose
    args=rose.jpg
    The pointer class now looks like this:

    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);
    ImageIcon ii = new ImageIcon(this.getClass().getResource(this.fileNam e));
    img = ii.getImage();

    }


    public Image getImg(){
    System.out.println("args=" + fileName);
    return img;

    }




    }
    I guess the images are loaded three times each. :/

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

    Default Re: Analogue Clock Problem

    Well, not loaded three times each, but the img is returned three times each.

  20. #45
    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 assume that a lot of the printouts you are seeing are from the paintComponent method or by methods that the paintComponent method calls. The paintComponent method can be called many times. Each time it is called it will make a print out.

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

    Default Re: Analogue Clock Problem

    So itīs nothing thatīs going to make my program run slower then? (So that I know for bigger projects)

  22. #47
    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

    No, that is normal.

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

    Default Re: Analogue Clock Problem

    Ok! Thanks for all the help! Very appreciated!

  24. #49
    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

    Good luck on your next project.

Page 2 of 2 FirstFirst 12

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