# Analogue Clock Problem

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• September 2nd, 2011, 07:10 AM
gargamel7
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);
• September 2nd, 2011, 07:20 AM
Norm
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
• September 2nd, 2011, 07:49 AM
gargamel7
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.
• September 2nd, 2011, 08:30 AM
Norm
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.
• September 2nd, 2011, 08:49 AM
gargamel7
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?
• September 2nd, 2011, 08:52 AM
Norm
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.

Quote:

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 =
• September 2nd, 2011, 09:09 AM
gargamel7
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.
• September 2nd, 2011, 09:14 AM
Norm
Re: Analogue Clock Problem
Quote:

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
• September 2nd, 2011, 09:29 AM
gargamel7
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.
• September 2nd, 2011, 09:40 AM
Norm
Re: Analogue Clock Problem
Quote:

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.
• September 2nd, 2011, 09:55 AM
Norm
Re: Analogue Clock Problem
A suggestion. Simplify your code by passing the image filename to the constructor:
pPink = new Pointer(3600, "pink.jpg");
• September 2nd, 2011, 10:20 AM
gargamel7
Re: Analogue Clock Problem
Quote:

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 :)
• September 2nd, 2011, 10:27 AM
Norm
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
• September 2nd, 2011, 10:50 AM
gargamel7
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().
• September 2nd, 2011, 10:56 AM
Norm
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.
• September 2nd, 2011, 11:02 AM
gargamel7
Re: Analogue Clock Problem
Everything you wanted me to do was already in there.

Pointer class:
Quote:

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()
• September 2nd, 2011, 11:09 AM
Norm
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:
Code :

`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
• September 2nd, 2011, 11:24 AM
gargamel7
Re: Analogue Clock Problem
I moved the loading of images to the constructor and it worked. The flowers are now drawn. The output is:
Quote:

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:
Quote:

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. :/
• September 2nd, 2011, 11:26 AM
gargamel7
Re: Analogue Clock Problem
Well, not loaded three times each, but the img is returned three times each.
• September 2nd, 2011, 11:32 AM
Norm
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.
• September 2nd, 2011, 12:13 PM
gargamel7
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)
• September 2nd, 2011, 12:33 PM
Norm
Re: Analogue Clock Problem
No, that is normal.
• September 2nd, 2011, 01:19 PM
gargamel7
Re: Analogue Clock Problem
Ok! :) Thanks for all the help! Very appreciated!
• September 2nd, 2011, 01:21 PM
Norm
Re: Analogue Clock Problem
Good luck on your next project.
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12