Java GUI Lesson 8 | Using Images
Hey um this is Mac heads 101. My name is Jake. Welcome to your eighth Java tutorial, and today I'm going to be talking about images, um, that you can put into your window. I'm just going to be talking about how to put images on your window because in the next tutorial, I'm going to be talking about how to do frame by frame animation.
So right now, I'm just going to show you how to load an image on there so that we can actually start doing frame by frame animation. The first thing we have to do is import an image file, import file system and browse because it's in a directory, and it's called examine your. So open, and I'm just going to get all those frames because we are going to be animating with these specific frames that I have prepared in the next tutorial. So I might as well import them all now and browse, and I'm going to import them into the projects, which is what we're working with. Finish. Yes.
Okay, so now that we have that, I'm going to make a private image icon image, and that image icon is going to be our image. And here's the thing we have to do this. And if you, oh by the way, if you haven't noticed, I made it extend J panel because this is not going to be a frame. The J panel, um, we can draw graphics and images onto, and then we actually in our main method create a JF frame and add the J panel to it so we can have our graphics and images and things like that.
So to be drawing graphics and images, we need this method: public void paint component, and it takes a graphics, and I'll just call that G, and super.paint component G. And super, by the way, um, what this means is you're going super, just means the super class. So like we're extending J panel. um, so now image equals New Image icon, and um, we just put the name of in quotes of, uh, the icon we're going to be using because, and the reason we imported it is because otherwise, we'd have to be reading files from our computer and stuff and that's, um, more complicated.
And also if you sent this to someone, you'd have to rely on that, um, image being on their system, which it probably won't be. So frame one.png, and that's going to just be one of the frames. So image icon then gets that frame that we imported, frame one.png, and that's in quotes. And now image do, uh, paint icon this, which is image. So we're going to be painting this using Graphics G and X and Y are the coordinates of it, and I'll put it at 0, 0.
Okay, so it's going to paint, um, this icon this with the graphics G at location 0, 0, and that's how we draw the image. And let me just get my, um, class with the main method up here because it's not right now. So as you can see, there's all our stuff Mac heads.
Okay, so here's really all we need to do: is create that image and then, um, give it a value and then paint it into the J panel. So do a second s equals new second. Doesn't extend jframe anymore, so we actually have to make the jframe jframe f equals new jframe. f.add s. f.set size 600, 400. Okay, oh yeah, 600, 400. set um, default close operation and set visible.
Okay, and now I'm just going to run this, and you'll see image icon. There we go, and that is the first frame of the animation, which is, um, this guy eating a cheeseburger. And so that was, um, frame one, and I drew it at, um, 0 by the way means that corner. So that's the, um, 0 means the top left corner of my image is going to be at 0, which is that point right there in the top left corner.
And yeah, so uh, in the next tutorial we are going to be using those frames to create an animation. So thank you for watching Mac heads 101, subscribe, and goodbye.