The Joy of Coding
Learn Code By Gaming Learn Code By Gaming
28.4K subscribers
9,721 views
0

 Published On Jan 25, 2021

In this beginner friendly Java project, we create a simple Bob Ross inspired painting using the Java Swing Graphics library. I go step-by-step through each line of code so that anyone can follow along. Understanding how to work with 2D graphics like this is a crucial step for learning video game development.

Bob Ross is a Twitch and gaming icon at this point, and I thought “wouldn’t it be cool to do a beginner programming tutorial in his style!?” Turns out my acting is cringe, but I hope you can still learn a lot from following along with this project!

Need help getting set up for Java development?    • How To Get Started with Java Developm...  

Get all the code for this project on GitHub: https://github.com/learncodebygaming/...

Remember to share your finished painting on Twitter using #joyofcoding
  / joyofcoding  

0:40 Setting up JFrame window
5:09 Adding a JPanel canvas
12:24 Setting the background color
13:43 Adding graphics with paintComponent()
21:22 Creating a custom Paintbrush class
27:50 Mixing paint colors
29:25 Using a for loop to draw a tree
37:00 RIP devil
38:20 Making a color gradient
41:17 The finished painting
41:24 Drawing image files
44:17 The finishing touches result

Full written tutorial: https://learncodebygaming.com/blog/th...

I thought today we'd do a fantastic little Java project. Just a simple little painting exercise that I think you'll really, really enjoy doing. If this is your first time with us, let me extend a personal invitation for you to drag out VSCode, or your favorite Java editor, and code along with us. And if this is your first time coding in Java, I've got a handy little guide that will get you all set up in less than 10 minutes (linked above).

Tell you what, let's get right to it. Today we're going to be using the Java Swing library to make ourselves a little painting. I've already primed our project with an empty class and a simple main() method.

And the first thing we need to do is create a window for our canvas to live in. With the Java Swing library, we can do that using JFrame.

So here's your bravery test, right at the start. Ready? Time to write your first line of code.

We're going to create a variable that represents our window. The data type of this variable is going to be JFrame, and then you can name your variable whatever you like. I'm going to call mine "window", but you might call yours "frame", or something else, but keep it sensible. Ok, now we need to assign some data to our variable. The equals sign is our assignment operator, and we're going to create a new object of type JFrame. And end it with a semicolon. Most statements in Java end with a semicolon.

JFrame window = new JFrame();

Let's look at this line of code we've written, and shoot, VSCode is unhappy with us already. We know it is because we've got these little squiggly red lines under JFrame. If you hover over one of them with your mouse you can see the error message. "JFrame cannot be resolved to a type". This is saying it doesn't know what we mean by JFrame. What we need to do is import the JFrame class.

So we could type in that import at the top of our file, above the App class definition, or we could use the "Quick Fix" feature in VSCode to do that a lot faster. Just click somewhere on the word JFrame, so your cursor is there, and then press ctrl+. to open up the Quick Fix options. Here it asks if we want to import JFrame from Swing, or create a new class called JFrame, or maybe we had a misspelling and we meant to write "JobName". In this case, we certainly want to import JFrame from Swing. There. The import has been added all nice and neat for us, and those little red lines are gone. Now we've got this yellow squiggly line, but that's ok, that's just a warning, not an error. It's just telling us "hey, you've got this variable here but you're not using it anywhere, so you might want to check that out." But of course, we'll be using it in a minute.

Let's give the window a size. On the window object, we'll call the setSize() method, and we'll give it the width and height we want, in pixels. I'm going to set mine to 900 by 600, but you choose any window size you want. And now to show the window, we need to call window.setVisibility(), and pass in "true".

When you close your window, you'll see in your console that your program is actually still running. That's no good. In the console, press ctrl+c to close the process. "Terminate batch job?" Yes.

Let's prevent that from happening. Back in our code, make a call to setDefaultCloseOperation(), still on our window object. And I'm going to do this first thing after initializing our object. And we need to tell it what that default close operation should be, so we'll pass in JFrame.EXIT_ON_CLOSE to exit our program when the window is closed.

Continue with the written tutorial here: https://learncodebygaming.com/blog/th...

#programming #java

show more

Share/Embed