Computer Science Canada Graphics problem |
Author: | TryinghArd_noob [ Mon Dec 20, 2004 9:36 pm ] |
Post subject: | Graphics problem |
I tried the search botton but i became more confused, anybody knows how to extract a picture from a file and output it in a same console... and in my research i came up with the method getGraphics() and getImage() can anybody help me please.... thanx in advance..[/code] |
Author: | Hikaru79 [ Mon Dec 20, 2004 11:28 pm ] |
Post subject: | |
First, are you using HSA console? (You know, the whole "c = new Console;" thing?) |
Author: | TryinghArd_noob [ Mon Dec 20, 2004 11:51 pm ] |
Post subject: | |
Yah.... im basically done with my program all i need is the code for the graphics... i tried reading applets but it just screwd my hollow brain So yah if it could be done in a HSA console that would be great. |
Author: | Hikaru79 [ Tue Dec 21, 2004 11:11 am ] |
Post subject: | |
I took a quick look through the HSA Appendix in the Holt Java textbook, and I could find nothing in there about loading images. My conclusion is that it simply can't be done on Holt's stupid little Console. Fortunatly, real Java comes to the rescue. You'll need to use toolkit.getImage(String fileName), which is in java.awt.toolkit. Check this link out for a complete tutorial: http://www.geocities.com/marcoschmidt.geo/java-load-image-toolkit.html |
Author: | TryinghArd_noob [ Tue Dec 21, 2004 4:31 pm ] |
Post subject: | |
Yah i know.. so wut the use of learning Holtsoft version of java if we cant used it in the real world nywy thanx for the link that shed a little light on me, even thou i dont even heard most of the method used so yah i did what the instruction said i save the file as Viewer.java, but im confused on how to call it. |
Author: | wtd [ Tue Dec 21, 2004 4:49 pm ] |
Post subject: | |
TryinghArd_noob wrote: Yah i know.. so wut the use of learning Holtsoft version of java if we cant used it in the real world
There is no point. |
Author: | Hikaru79 [ Wed Dec 22, 2004 12:39 am ] | ||||||||||||
Post subject: | |||||||||||||
TryinghArd_noob wrote: so yah i did what the instruction said i save the file as Viewer.java, but im confused on how to call it. Well, first you need to call the object's constructor method. Viewer.java has a line that says:
That's the constructor. How do we know? Because it has the same name as the Class (Viewer). Now we know what to call. But does it require any arguments? It sure does -- a String called 'fileName'. Taking a wild guess, but without looking at the method's code, I'd say it's the filename of the image you wish to load. Therefore, before we can use the Viewer object, in our program, we need the code:
Yay. Now we have an object called 'foo', that stores an image inside of it. With this object, we can draw that image onto a canvas with the
method. But, uh-oh, this method requires an argument - -a "Graphics" object. If you've been using HSA, chances are you haven't worked with the Graphics object (or subclasses, even) yet. If you're ambitious, read up here: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html to get a bit of an idea how to use this program. Otherwise, you can't do much. A Console is NOT a valid argument for the paint method. Assuming you've gotten your Graphics object working, however, we may want to extend our Viewer object a bit more. For example, it will always draw our image in the top left-hand corner of the canvas. Say we want to be able to control where the image gets drawn. We'll have to introduce some more arguments into the paint method.
It's being drawn at 0,0 and that's not good. Let's offer the coder a choice where the image will be drawn, like so. (Simply change this code in your Viewer.java file)
So now we're saying, take two numbers when the method is called, like:
Easy, right? Understand what I've done here up to this point, and then I'll expand for you a bit more, OK? |
Author: | TryinghArd_noob [ Wed Dec 22, 2004 1:00 am ] | ||
Post subject: | |||
if that's ur easy part, I wonder wuts the hard part yah ur right im still in the HSA thing... stupid Holtsoft and their conspiracy on wasting our precious tym on those nonsense stuff.. nywy thanks on the walk through, but i went to the library this morning and found this great book, tick but wut am i gonna do... heres wut i learnd
it kinda work but now i have to put a code, so dat it automatically resize the applet window... |
Author: | TryinghArd_noob [ Wed Dec 22, 2004 1:17 am ] | ||
Post subject: | |||
i now for bigger prog, i need to learn the things that u just explaind thats why im studying it too ryt now.. and also i have this
|
Author: | Hikaru79 [ Wed Dec 22, 2004 1:58 am ] | ||||||
Post subject: | |||||||
Oh, OK You're trying to make an applet. Then that has some pre-built Image functions like you wrote about. Here's Sun's documentation for Applets: http://java.sun.com/j2se/1.3/docs/api/java/applet/Applet.html Let's go through it a bit.
The applet's getImage method returns an object of type Image. You have to catch that Image somehow, though, so when you're actually writing code for the applet, it'll look like:
Now you have the image saved in a variable -- img. Of course, the applet still needs a Graphics object to draw the image TO. Also, resizing the applet, like you said, can be done simply with:
where x and y are the new dimensions. Hope this helps! Ask more questions! ^__^ |
Author: | TryinghArd_noob [ Wed Dec 22, 2004 5:40 am ] | ||
Post subject: | |||
All i wanted was to load a pic and hundreds of codes just keeps on appearing i should have done your way Hikaru... i spent all night messing around with mycode, i still cant figure out. I didnt know applets creat a HTML file, im sorry i guess i really dont know wut im doing... Nywy i went back to your code and a little of patience i finally figure it out... Hikaru thank you sooo much... but when i try the code
the pic wont load... it display only blank |
Author: | Hikaru79 [ Wed Dec 22, 2004 12:34 pm ] | ||
Post subject: | |||
TryinghArd_noob wrote: All i wanted was to load a pic and hundreds of codes just keeps on appearing
i should have done your way Hikaru... i spent all night messing around with mycode, i still cant figure out. I didnt know applets creat a HTML file, im sorry i guess i really dont know wut im doing... Nywy i went back to your code and a little of patience i finally figure it out... Hikaru thank you sooo much... but when i try the code
the pic wont load... it display only blank =) Glad to hear it!! As for the foo.paint (g,100,100);, there could be a couple of reasons for that. First, do you have a correct graphics object (g)? Second, did you correctly modify the original paint method in Viewer.java? Third, (most likely) it's not finding the image. And fourth, maybe the applet is simply too small for the point 100,100 to show up. Remember, in Java, 0,0 is the TOP left corner, and 100,100 would be the top left corner of the image. Actually, attach all the code you have so far We'll see what we can do with it! ^__^ |
Author: | TryinghArd_noob [ Wed Dec 22, 2004 5:38 pm ] | ||||
Post subject: | |||||
ok i didnt change anything except for the the public void paint (Grphics grapics) part. here...
and i tried the original one and it work fine and after i change the graphics part so that i could use foo.paint (g,100,100); now doesnt work, ok heres just the graphics part...
and oh yah can i use this with multiple pictures? |
Author: | Hikaru79 [ Wed Dec 22, 2004 9:07 pm ] | ||||
Post subject: | |||||
TryinghArd_noob wrote: ok i didnt change anything except for the the public void paint (Grphics grapics) part. here...
and i tried the original one and it work fine and after i change the graphics part so that i could use foo.paint (g,100,100); now doesnt work, ok heres just the graphics part...
and oh yah can i use this with multiple pictures? You're not calling the paint method in your main. Also, you have no graphics object. And you can only have one picture at a time stored in a Viewer, but you can just instantiate a new Viewer for each image, or draw one, change image, draw that one, change image, etc. |
Author: | TryinghArd_noob [ Wed Dec 22, 2004 10:44 pm ] |
Post subject: | |
i see wut u mean thank you soo much i got it now... i have two more question and i think im done.. Can i combine GUI and Graphics ? and you know for turing we use cls and for console we use clear(), what about in Graphics? |
Author: | Hikaru79 [ Thu Dec 23, 2004 3:14 pm ] |
Post subject: | |
For your first question, yes you can For your second question, I'm not exactly sure if there even is such an equivalent. For now, try just drawing a white filled rectangle over the whole canvas. Same effect |
Author: | TryinghArd_noob [ Thu Dec 23, 2004 8:04 pm ] |
Post subject: | |
Wow hundreds of codes and the missed that particular thing I hope i dont need to free the buffers up like i have to in turing... Thanks... u have been so helpful to me.. now to get back and mess around with the code.. thanks again... and i know your gonna here from me soon here... |
Author: | wtd [ Fri Dec 24, 2004 12:01 am ] |
Post subject: | |
The Graphics class API reference. Learn to use the API ref. |