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
Limits by rationalizing | Limits and continuity | AP Calculus AB | Khan Academy
Let’s see if we can find the limit as x approaches negative one of ( \frac{x + 1}{\sqrt{x + 5} - 2} ). So our first reaction might just be, okay, well let’s just use our limit properties a little bit. This is going to be the same thing as the limit as x …
How old is a private jet owner?
The average age of a buyer of an airplane, like the CEO of a company or an entrepreneurial lead business, has drastically changed. Not just in the last couple of years, really over the course of the last 20 years. Twenty to thirty years ago, the average …
Long run average total cost curve | APⓇ Microeconomics | Khan Academy
We’ve talked about the idea of average total cost in several videos so far, where it was the sum of your average variable cost and your average fixed cost. But when we’re talking about fixed costs, by definition, that means we’re talking about things in t…
Marginal cost, average variable cost, and average total cost | APⓇ Microeconomics | Khan Academy
Let’s say that we run ABC Watch Factory and we want to understand the economics of our business. So, what we have in this table is some data that we’ve already been able to estimate or measure based on how our business is running, and then we’re going to …
Formal and informal powers of the US president | US government and civics | Khan Academy
What we’re going to do in this video is talk about the powers of the President of the United States, and we’re going to broadly divide them into two categories. Formal powers are those that are explicitly listed in the United States Constitution, and we’…
Lungs 101 | National Geographic
[Narrator] Breathe in, breathe out. With every breath, the body is replenished and cleansed. A process made possible by two of the body’s most important and delicate organs. The lungs are two major components of the respiratory system. Soft, light, and …