yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Java GUI Lesson 8 | Using Images


3m read
·Nov 3, 2024

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.

More Articles

View All
15 Things That Are Not Missing From a Rich Person’s Home
Here is something you didn’t know. The inside of a rich person’s house is usually more expensive than the acquisition price of the property, or it’s at least coming close. When you think about rich people’s homes, you probably picture gold-plated everyth…
Redefine Your Limits by Being Honest With Yourself | Kyle Maynard| Big Think
What is one big excuse that you could take on right now, today, and do something about? If you started there, what would your life look like in 30 days? What would it look like a year from today? What would it look like five years from today? And if we …
10 Proven Ways To Master Your Focus
Your focus is your most valuable asset, right alongside your private data. That’s why billions of dollars are spent every year to make you lose track of time. The whole goal of every social platform is to grab your attention and keep it. Well, in this pro…
Ron Conway at Startup School SV 2014
He’s back for a day or an hour. There’s lights behind that thing. Um, okay, so I interviewed Ron on this stage. We’re on stage at Startup School in 2012, and the video’s on YouTube. And Ron told a lot of the good stories then, so I’m not gonna ask him abo…
Who God is in Different Cultures | The Story of God
Who God is, is almost universally a great unknown. There are different manifestations of God: different statues, different icons, different sounds, smells, looks of God across cultures. God has a sound. To Navajo, God is a light, bright light to many peop…
Why Fuel Injectors are AWESOME (28,000 fps Slow Mo) Part 1 - Smarter Every Day 281
Hey, it’s me Destin, welcome back to Smarter Every Day! You remember in an earlier video we talked about how carburetors work? We made this see-through carburetor, and you can see that there’s a bowl… there’s the float… there’s a needle and then there’s t…