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
One, Two, Three Bites, You’re Out | Wicked Tuna: Outer Banks
Michael: “Let that one go, all right? Got a double on, baby! What do you want me to do with this one?” TJ: “Let him go! Just cranking up. Keep it tight on them! Get tight on him!” F: “Watch out! What we’re here for, baby! Got a double on here. I think h…
Dreamcraft (S18) - YC Tech Talks: Gaming 2020 (November 9th, 2020)
Um, so we are Dreamcraft. This is Tian; I’m the founder of Dreamcraft. We went through Y Combinator in the summer 2018 batch. We are building a platform for anybody to create, publish, and monetize games without programming expertise. We believe that the…
Why The Middle Class Are Financially RUINED
What’s up, you guys? It’s Graham here and unfortunately, I have some rather discouraging news. Some of this might come as a surprise to you, and some of it might not. But regardless, here’s what we’re dealing with right now in terms of the middle class he…
Decimal multiplication place value
This is an exercise from Khan Academy. It tells us that the product 75 times 61 is equal to 4575. Use the previous fact to evaluate as a decimal this right over here: 7.5 times 0.061. Pause this video and see if you can have a go at it. All right, now le…
The Lure of Horror
Why do we love being scared? Is it the way our hearts pound in our chests? The mixture of curiosity and revulsion when we see a monster or a ghost? Or is it something even darker, like the disturbing themes portrayed in popular culture? I’ll be drawn to g…
This Journal Keeps Me Productive (& Maybe You Too)
This is the theme system journal. It’s something I helped design for me and maybe for you to help improve my life in a practical way. It’s a very flexible tool; there are intentionally almost no labels of what has to go where, so it can be adaptable. But …