Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

strange problem involving Point  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am refactoring my old AnalogClock program to make it a component. i have it working but something seems odd. here is the code:

and here is an example calling program:

if i comment out line 48 the frame is no longer the correct size. it is super small. if you maximize it you can then see the clock. i fail to see what line 48 has to do with setting the size of the frame. i don't really like computing origin once a second, but it works for now
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it intentional that you're instantiating two AnalogClock objects using different constructors, the first of which assigns something to "origin" even if you comment out line 48?
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, that was unintentional. the first instantiation should have been commented out. i was using DisplayClock to test Analog classes two constructors. i would like to assign origins value in each constructor, but i had problems with the second one so i wrote the code in actionPerformed()
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tested it some more using the first constructor. i tried commenting out line 39. it displayed correctly and ran, but it threw a null pointer exception at line 89
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's to be expected if you use the first constructor and comment out line 39: nothing gets assigned to "origin", so it is null, and the code doesn't check for that in line 89.
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will try rephrasing the problem. i can't seem to assign origin in the second constructor. System.out.println returns 0.0, 0.0
however in getPreferredSize it prints 413.0, 409.0
the first constructor works fine
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's impossible to say what's going on without seeing the code that uses this class, i.e., the DisplayClock class.
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i posted it in my original post. if you do a gogle image search for "clock face" you can get an image to use so you can compile and run this and see what i am talking about
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i can see what the problem is. in the second constructor face.getWidth(null) returns -1. since i divide it by 2, the System.out.println() prints 0.0. same thing for face.getHeight(null)
but why does the same code in getPreferredSize() return the actual size of the image?
i don't see why nobody can understand
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:i posted it in my original post.

The original post uses the two-argument constructor, but in your last post you said you were using the "first", i.e. the one-argument constructor. Does that mean that the code you're now using is the same as in your first post, except that you commented out line 20, and uncommented line 16?
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. of course. i don't know you Ulf but it seems you are trying to obfuscate my problem. you can compile it to see what i am talking about. i don't know what else i can do. i give all the source code. you can try it yourself to be sure there is a problem
look people, i am doing this for you. i want you to have an analog clock you can add to your programs
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was actually trying to clarify it for me, precisely I wanted to be sure what code you were talking about. If you look around these forums you'll see that every day folks talk about changes they made to some code they posted earlier, and once you pin them down what change exactly that was, it turns out that they changed something else as well that did turn out to make a difference.

I hadn't actually planned on going that extra mile, but I did now compile and run the code (using http://th06.deviantart.net/fs71/PRE/i/2012/002/9/5/clock_face___3_by_agf81-d4l11wc.jpg as the image), and the output I get is "80.0 80.0 160.0 160.0". That's what I would expect since "face" is null, so minuteHandLength=80, and d=(160,160). Based on that it seems that I can't reproduce the problem.
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf, i told you the first constructor works fine. try changing DisplayClock to use the second constructor. that is where the problem is
face is null

that is the problem in a nutshell. how can it be null when DisplayClock passes an image to it?
this is crazy. i tried moving the assignment into getPreferredSize(). that is better than in actionPerformed().
the same problems remain. i can't comment out the assignment in the second constructor without the frame being minimal.
the assignment in getPreferredSize() doesn't work either.
it only seems to work right if i do it in actionPerformed() and don't comment out the assignment in the constructor.
the following gives this result
0.0
0.0
413.0
409.0
413.0
409.0
etc

i don't see why this is so hard to understand or replicate
 
Piet Souris
Rancher
Posts: 1983
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:i can see what the problem is. in the second constructor face.getWidth(null) returns -1. since i divide it by 2, the System.out.println() prints 0.0. same thing for face.getHeight(null)
but why does the same code in getPreferredSize() return the actual size of the image?
i don't see why nobody can understand

If you read the API, it says that -1 will be returned when the size of the image is
not yet known. That happens when the image is still loading.
It also causes the panel to have an initial size of about zero (I guess).

What you could do is to load the image using the class ImageIO, method read().
As far as I know, this method does not return before the image is completely loaded.

I'd say: give it a try.

Greetz,
Piet

PS: please, be VERY reluctant to criticize a replier. He/she is putting time and effort trying to solve a problem that YOU caused.

 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read the API, it says that -1 will be returned when the size of the image is
not yet known. That happens when the image is still loading.
It also causes the panel to have an initial size of about zero (I guess).

that is pretty much what i thought. i will try your suggestion

at first i doubted, but i see ImageIO has a read(URL) method. you cannot access a file using File inside a jar. i need a break right now. maybe this problem is now resolved. i am still upset that i can't do it the way i first tried.
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:(...)
at first i doubted, but i see ImageIO has a read(URL) method. you cannot access a file using File inside a jar.

I do not know of any particular problems when loading files from within a jar, so what exact problem were you encounting?

Anyway, there are methods for loading an image, that return immediately. You would then supply an ImageObserver, that takes
care of, say, drawing the image once it is loaded. In such cases, you cannot use any characteristics of the image, since these are
not yet known. It is like creating a JPanel, and using the getWith() before that panel is properly laid out.

Randall Twede wrote: i need a break right now. maybe this problem is now resolved. i am still upset that i can't do it the way i first tried.

Have a nice break.
I alway say to myself, when encountering hair-greying problems: "Glad I do not have to earn a living with this" (admittedly, I do not
always live up to this noble saying).

Greetz,
Piet

 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for explaining the problem. i wondered what that ImageObserver stuff was all about. i tried using ImageIO but that turned out even worse. it throws a null pointer exception and doesn't run. i guess i will just go with what i have for now. assign origin each time actionPerformed() gets called, and leave in the assignment in the constructor that doesn't work right but somehow allows getPreferredSize() to work right(i have no idea what is going on with that). the program will run right and only i will know it is a kludge

as for not being able to access files in a jar i am referring to what i read here
although i glanced through the oracle tutorial and didn't see them say the same thing
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, look at the following code:

output is:

File is: D:\JavaProgs\ImageLader\src\imagelader\resouces\MAMLOESIK.png
Created bufim with ImageIO
width is: 881
heigth is: 881
**********************************
Created image with Tooliit
width is: -1
heigth is: -1

QED.

Greetz,
Piet

PS: I used a String and a File in this example, because no matter what I tried.
I endend up with a URL being null.

PS2: I tried to cast the Image 'ci' to a BufferedImage, but I got a class cast
error on that one. I wonder what that Image is...
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i will fool around with it some more when i have time
since your code works. this should to
String s = "D:\\JavaProgs\\ImageLader\\src\\imagelader\\resouces\\MAMLOESIK.png";
File f = new File(s);
URL url = f.toURI().toURL();
io = ImageIO.read(url);
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String s = "D:\\JavaProgs\\ImageLader\\src\\imagelader\\resouces\\MAMLOESIK.png";
File f = new File(s);
URL url = f.toURI().toURL();
io = ImageIO.read(url);

Alternatively, ImageIO has a read(File) method - no need to fiddle around with URIs and URLs.
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from what i read you have to use URL's if the resource is in an executable jar
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct, a File object can't reach inside a jar. I was merely commenting on that code snippet.
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you could do is to load the image using the class ImageIO, method read().
As far as I know, this method does not return before the image is completely loaded.

apparently you were right. i must have just done it wrong the first time i tried it. it works now.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!