Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Graphics problem
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TryinghArd_noob




PostPosted: 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]
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: Mon Dec 20, 2004 11:28 pm   Post subject: (No subject)

First, are you using HSA console? (You know, the whole "c = new Console;" thing?)
TryinghArd_noob




PostPosted: Mon Dec 20, 2004 11:51 pm   Post subject: (No 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 Shocked So yah if it could be done in a HSA console that would be great.
Hikaru79




PostPosted: Tue Dec 21, 2004 11:11 am   Post subject: (No 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
TryinghArd_noob




PostPosted: Tue Dec 21, 2004 4:31 pm   Post subject: (No subject)

Yah i know.. so wut the use of learning Holtsoft version of java if we cant used it in the real world Rolling Eyes nywy thanx for the link that shed a little light on me, even thou i dont even heard most of the method used Laughing

so yah i did what the instruction said i save the file as Viewer.java, but im confused on how to call it.
wtd




PostPosted: Tue Dec 21, 2004 4:49 pm   Post subject: (No 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.
Hikaru79




PostPosted: Wed Dec 22, 2004 12:39 am   Post subject: (No 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:
code:
        public Viewer(String fileName) {

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:

code:

Viewer foo;
foo = new Viewer ("mypicture.jpg");


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

code:
        public void paint(Graphics graphics) {


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.

code:

        public void paint(Graphics graphics) {
                graphics.drawImage(image, 0, 0, null);
        }


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)

code:

        public void paint(Graphics graphics, int x, int y) {
                graphics.drawImage(image, x, y, null);
        }


So now we're saying, take two numbers when the method is called, like:
code:
foo.paint(g,100,100)
and draw our image at 100, 100.

Easy, right? Smile Understand what I've done here up to this point, and then I'll expand for you a bit more, OK?
TryinghArd_noob




PostPosted: Wed Dec 22, 2004 1:00 am   Post subject: (No subject)

Shocked if that's ur easy part, I wonder wuts the hard part Rolling Eyes 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

code:

import java.awt.*;

public class PicLoad extends java.applet.Applet

{
    Image pic;
    public void init ()
    {
        pic = getImage (getCodeBase (), "picture.jpg");
    }
    public void paint (Graphics g)
    {
        g.drawImage (pic, 0, 0, this);
    }
}


it kinda work but now i have to put a code, so dat it automatically resize the applet window...
Sponsor
Sponsor
Sponsor
sponsor
TryinghArd_noob




PostPosted: Wed Dec 22, 2004 1:17 am   Post subject: (No 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

code:


int picWidth, picHeight;

picWidth = pic.getWidth(this);
picHeight = pic.getHeigth(this);

Hikaru79




PostPosted: Wed Dec 22, 2004 1:58 am   Post subject: (No subject)

Oh, OK Smile 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.

code:
public Image getImage(URL url)


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

Image img;
img = getImage ("C:\My_Pictures\me.jpg");


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:
code:
resize (x,y);

where x and y are the new dimensions.

Hope this helps! Ask more questions! ^__^
TryinghArd_noob




PostPosted: Wed Dec 22, 2004 5:40 am   Post subject: (No subject)

All i wanted was to load a pic and hundreds of codes just keeps on appearing Shocked

i should have done your way Hikaru... Embarassed 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... Laughing

Nywy i went back to your code and a little of patience i finally figure it out... Hikaru thank you sooo much... Very Happy but when i try the code

code:


foo.paint(g,100,100)


the pic wont load... it display only blank
Hikaru79




PostPosted: Wed Dec 22, 2004 12:34 pm   Post subject: (No subject)

TryinghArd_noob wrote:
All i wanted was to load a pic and hundreds of codes just keeps on appearing Shocked

i should have done your way Hikaru... Embarassed 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... Laughing

Nywy i went back to your code and a little of patience i finally figure it out... Hikaru thank you sooo much... Very Happy but when i try the code

code:


foo.paint(g,100,100)


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 Smile We'll see what we can do with it! ^__^
TryinghArd_noob




PostPosted: Wed Dec 22, 2004 5:38 pm   Post subject: (No subject)

ok i didnt change anything except for the the public void paint (Grphics grapics) part. here...

code:

import java.awt.*;
import java.awt.event.*;

public class Viewer extends Frame
{
    private Image image;

    public Viewer (String fileName)
    {
        Toolkit toolkit = Toolkit.getDefaultToolkit ();
        image = toolkit.getImage (fileName);
        MediaTracker mediaTracker = new MediaTracker (this);
        mediaTracker.addImage (image, 0);
        try
        {
            mediaTracker.waitForID (0);
        }
        catch (InterruptedException ie)
        {
            System.err.println (ie);
            System.exit (1);
        }
        addWindowListener (new WindowAdapter ()
        {
            public void windowClosing (WindowEvent e)
            {
                System.exit (0);
            }
        }
        );
        setSize (image.getWidth (null), image.getHeight (null));
        setTitle (fileName);
        show ();
    }


    public void paint (Graphics graphics, int x, int y)
    {
        graphics.drawImage (image, x, y, null);
    }


    public static void main (String[] args)
    {
        new Viewer (args [0]);
    }
}


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...

code:

public class PicLoad
{
    Image image;
    Viewer pic;

    public void paint (Graphics g)
    {
        pic.paint (g, 100,100);
    }


    public static void main (String[] args)
    {

        Viewer pic = new Viewer ("pic.jpg");

    }

}


and oh yah can i use this with multiple pictures?
Hikaru79




PostPosted: Wed Dec 22, 2004 9:07 pm   Post subject: (No subject)

TryinghArd_noob wrote:
ok i didnt change anything except for the the public void paint (Grphics grapics) part. here...

code:

import java.awt.*;
import java.awt.event.*;

public class Viewer extends Frame
{
    private Image image;

    public Viewer (String fileName)
    {
        Toolkit toolkit = Toolkit.getDefaultToolkit ();
        image = toolkit.getImage (fileName);
        MediaTracker mediaTracker = new MediaTracker (this);
        mediaTracker.addImage (image, 0);
        try
        {
            mediaTracker.waitForID (0);
        }
        catch (InterruptedException ie)
        {
            System.err.println (ie);
            System.exit (1);
        }
        addWindowListener (new WindowAdapter ()
        {
            public void windowClosing (WindowEvent e)
            {
                System.exit (0);
            }
        }
        );
        setSize (image.getWidth (null), image.getHeight (null));
        setTitle (fileName);
        show ();
    }


    public void paint (Graphics graphics, int x, int y)
    {
        graphics.drawImage (image, x, y, null);
    }


    public static void main (String[] args)
    {
        new Viewer (args [0]);
    }
}


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...

code:

public class PicLoad
{
    Image image;
    Viewer pic;

    public void paint (Graphics g)
    {
        pic.paint (g, 100,100);
    }


    public static void main (String[] args)
    {

        Viewer pic = new Viewer ("pic.jpg");

    }

}


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.
TryinghArd_noob




PostPosted: Wed Dec 22, 2004 10:44 pm   Post subject: (No subject)

Very Happy 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?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: