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
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()?
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?
Quote:
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?
Re: Analogue Clock Problem
Quote:
How would I structure it so that the images are loaded in the Pointer constructor?
Move this code to the constructor:
Code :
ImageIcon ii = new ImageIcon(this.getClass().getResource(img+".jpg"));
img = ii.getImage();
Re: Analogue Clock Problem
I am unsure as to why you have chosen to add:
Quote:
this.getClass().getResource(img+".jpg")
Java's ImageIcon constructor can take an Image.
Code :
ImageIcon imageIcon = new ImageIcon(this.img)
and you can:
Code :
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.
Re: Analogue Clock Problem
The constructor's args are messed up. He's mixing apples and oranges to build an image filename.
Re: Analogue Clock Problem
No there are no error messages. It compiles correct and I see a window with a bike wheel in it.
Quote:
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!
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
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:
Quote:
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?
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.
Add some printlns to print the values the images For example:
System.out.println("img=" + img);
Quote:
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.
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.
Quote:
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!
Re: Analogue Clock Problem
Quote:
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.
Code :
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
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.
Re: Analogue Clock Problem
Quote:
i insert a println in the pointer constructor it's written 3 times
What is printed out? Copy and paste the print out here.
Re: Analogue Clock Problem
img=null.jpg
img=null.jpg
img=null.jpg
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
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).
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?
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
Re: Analogue Clock Problem
Quote:
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?
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:
Quote:
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:
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 + ".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;
}
}
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?
Re: Analogue Clock Problem
public Clock(){
pPink = new Pointer(3600,pink);
pWeird = new Pointer(60,weird);
pRose = new Pointer(1,rose);
}
Re: Analogue Clock Problem
What you posted does NOT give values to the pink, weird and rose variables.
Where do you give them values?