I love your videos! They are short and sweet. You do one thing in each video and you keep it short. It makes it really easy to learn one thing each day and not get overwhelmed. Thank you for making these.
You just saved me, Bucky. I've been watching your videos since I started programming. It's 3:36am and I'm still awake 'cause I didn't pay attention on our last Java class. THANK YOU VERY MUCH.
For those who may still be confuse, I made my own comments in the source code. package Class; import java.awt.*; // ^---Contains all of the classes for creating user interfaces and for painting graphics and images. import java.awt.event.*; // ^---Provides interfaces and classes for dealing with different types of events fired by AWT components. import javax.swing.*; // ^---Provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms. public class Class extends JPanel { // ^---Extends JPanel to use the frame
public void paintComponent(Graphics g){ // ^---You need a paintComponent method to paint pictures super.paintComponent(g); // ^---You need to equip the variable g into super.paintComponent to draw pictures this.setBackground(Color.yellow); // ^---This sets the background color g.setColor(Color.BLUE); // ^---This sets the color you want to use g.fillRect(0, 0, 10, 10); // ^---This draws a rectangle using the color you selected g.setColor(new Color(190,81,215)); g.fillRect(11, 11, 10, 10); g.setColor(Color.RED); g.drawString("This is some text.", 21, 21); // ^---This displays a string using the color you selected }
public static void main(String[] args) { JFrame f = new JFrame("Title"); // ^---This creates a frame to hold the graphics f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ^---This exits the program when you close the window Class graphics = new Class(); // ^---This creates an object so you can access methods outside of the main method f.add(graphics); // ^---This adds the object to the frame // ^---I think since paintComponent is the only method outside of the main method... // ^---...the objects finds it, and adds it the the frame...or something... // ^---This part kind of confuse me. It creates the object, but then it add the... // ^---object to the frame without telling it which method to use. f.setSize(300,300); // ^---This sets the size of the frame f.setVisible(true); // ^---This sets the frame so it's visible } }
Notice: This is a bit different than normal coordinate system. The bigger the y is, the lower the coordinate point is (normal coordinates tell us that bigger value y is, the higher the coordinate is).
@pspiso123 Yes, the super method is just the object of the superclass, JPanel. Without extending it, you can just call it using JPanel panel = new JPanel(); panel.paintComponents(g); rather than super.paintComponent(g);
I really like your tutorials. We have at school java but till I saw your vids I didn't understand it. You make it very well and explain it perfect. Thanks
I think the reason that bucky is a better teacher than some college professors is because some teachers have a "knack" for teaching, and saying what they think others will understand, but that doesn't mean all teachers that know java, also know how to teach "well". idk just a thought
Why do we need to add a JFrame object in the main class.? We never did that before. Is it because in all earlier GUIs we extend to JFrame but here we extended to JPanel?
Hello how are you? Can anyone help me on how I can use Graphics2D other than using JFrame, JPanel or Applets? So, my project is web, I need the drawing to be rendered in a DIV, HTML element. Can someone help me? I've looked on many websites and videos, but they all just talk about JFrame, JPanel, Applets...
Error: Main method not found in class Test, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
Hi, i tried to run the program but i only got a white screen, not rectangles ; there is a problem and its about the p component, it tells me "null" or something like i did not initialize the component called p
any difference between canvas class and jpanel class as far as making a window with some images goes? I've been told jpanel is a component of a jframe (or a window as i understand it in layman terms) and a canvas object is added to that component. So so far i think that you use .add(canvas) on a jframe with the canvs having an overriden paint method. but by simply changing class extension from canvas to JPanel, and paint() method to paintComponent() I see the same result. can anyone explain why? does this video's way make replace an existing default jpanel from the jframe and paintComponent() puts a canvas on there for us? or am I completely off the rails?
I sat down for a while but i cant figure it out by myself (dumb? -_-) How can i access the "p" variable from another class in order to make draw calls from this class?
make sure it's the exact same as in the video and you should still get what he got. if youre making your own code, make sure that you change the color to something that isn't white
I am looking to write a hex-based board game. For that, I need the basic tile operations: read from file, blit to screen, write to file. I have looked all over for them. There are plenty of code samples to do this with square "tiles," as they're called, but I can't find anything that will do this with hexagons. I'm currently looking at something called "mapy," but it's confusing. Any pointers would be appreciated.
paintComponent(Graphics g) is the method of Jpanel. We are overriding it (method overloading) and its called automatically when we override, you don't have to worry about initiating it.
I don't understand the use of the line "super.paintComponent(g);" My program runs fine without this line. Also, I tried changing all the "paintCompenent"s to "paint" and the program also runs fine. Why is that? Thanks in advance for any help!
When you don't put in that line of code is because it wouldn't repaint otherwise. when moving graphics around by changing the x and y values, without that code you would see where the graphic previously was, but with that line of code the previous instance of the graphic would change every time it's updated.
@theMacBoy11 If you google "java graphics api" you can see a list of the methods you are able to use. If it is possible, then you will see a method like setColor(*your hex thingie*). If not, then it's either not possible, or the method is located in one of the parrent classes (Graphics extends other classes).
+thenewboston i have been following your tuts as far as the completion of having the images blink(close eyes open eyes). since then i have used your code to keep the screen settings and now i trying to draw two rectangles on the screen, i got thru with that. is there anyway to make it clickable?(the rectangles) using the graphics class?
can some explain the code below please, why do we have x,y in constructor parameter initialised with axes of the top ones. int x=10; int y=10; public Constructor(int x, int y){ this.x=x; this.y=y; }
I have to draw a cartoon character for a school project and *I have a question.* How to I pass multiple parameters to the paintComponent method. I tried this: public void paintComponent(Graphics g, Graphics g2) { Graphics2D pearl = (Graphics2D) g; Graphics2D wings = (Graphics2D) g2; I wanted to draw the different parts separately, however this printed nothing. public void paintComponent(Graphics g) { Graphics2D pearl = (Graphics2D) g; This however worked just fine. The issue is that I've already drawn a big part of my character, but I need to draw parts behind what I already have, and whenever I do that, things like .rotate() and .translate() will mess up everything that comes after, so I was hoping I could draw them in a separate method or something (I'm still rather new to Java so forgive my possible errors in terminology). I'd REALLY appreciate if you could respond asap. Currently drowning in other homework and I had to put this aside! P.S. I extended JComponent into the class. not JPanel. The character I'm drawing is the pokemon Palkia. cdn.bulbagarden.net/upload/thumb/6/66/484Palkia.png/250px-484Palkia.png I've already drawn the big shield thing with the pearl in the center on her shoulder, but I now need to draw the three wings that are behind that and that go rightwards. I don't know how to .rotate() those wings without rotating what I have as well. Thank you
Hi focky (how do you spell it) I'd like to draw an arrow between 2 buttoms to represent a graph (vertex-edges) do you have any suggestion for me? thanks dude!
Could you please tell me what text editor are you using in this video? It looks pretty cool. I'm using plain old Notepad++ and obviously it doesn't give any suggestions:(...
hi, i need to draw standard normal diviation, whats the easiest way to draw that line? o do i actually have to make 100ths of drawLine statements and then sort each dot on the line?
you never showed us how to download the java 2D library when i go on the site i am seeing lots of links and still cant access the libraries pls help it;s firetrucking frustrating man????
I've looked at his video and my work over and over but I can't find my mistake. I copied exactly what bucky did but my GUI is blank. My program doesn't even give me errors. Anyone know what could be wrong?
SynEnergy You have type what is there in the main method in the bucky class...not whatever in the peaches class.... there , there are certain code which is required to set up the JFrame/JPanel !
the step super.PaintComponent(g); its giving me error saying graphics.Graphics canot be converted to java.awt.graphics what thats suppose to mean any one help
im just a complete beginner with java (i havent been following these tuts), but i believe since you are importing those methods, you have to run them off the same name, so i believe they have changed the name of that method from the time that this tut came out and the time that you are doing this!
so weird, after I compile it (no errors) nothing prints out on the screen, its just an empty box. I've checked my code like 10 times now, still haven't found a problem. Has anyone else gotten this?
If I correctly understand: whenever I want to draw something, I should call repaint() and all instruction for drawing have to be in the paintComponent() function? But how that function know, what to draw? Should I pass somehow specific instruction (in component's public variables?) to that function? Or should I biuld my program in such way, that everything is in the paintComponent()? I have in mind a program which takes from user some info and then draw something based on that data.
If your program doesn't work maybe you could resolve that writing: "public class Example extends jFrame" instead of the "public class Example extends Jpanel" that he shows in the video,if that doesn't work try "public class Example extends JComponent". If you get an error "The serializable class Example does not declare a static final serialVersionUID field of type long" simply click on that error icon on the Eclipse IDE and click on "Add default serial version ID".
if some one could tell me why mine is not working I have public void paintComponent(Graphics g) as the error in my code saying that it is a bad class file. If somebody could tell me the problem i would have much thanks!
@theMacBoy11 I did a little Googling, and I saw that if you make a new Color class you can specify the HEX number directly in the constructor's parameter like this: java.awt.Color colorNameHere = new java.awt.Color(0xFF0096); as long as you remember the '0x' before the HEX code. You can then use that color object with the g.setColor(colorNameHere); to use the example in the video to change to color. Have fun! :D
I prefer BlueJ, it has a very simple interface and shows graphical relationships between classes (such as inheritance and other dependencies), i've also started to use NetBeans also. Also, BlueJ doesn't irritate me by constantly suggesting stuff, yet it still color-codes key words.
'My ultimate goal is to make computer games, and we can't make computer games without SWEET graphics' - quote of the day by a mile, although I'm sure the guy who made pong said the same...
The "super" part means it runs the parents class's paintComponent(g) method (the parent class being JPanel in this case, as that is the class that is extended), so rather than just running your code, some of the java code is run first, probably to make sure everything's initialised properly. Hope this clears up your confusion :)
Wow this is great, looking to learn Java before starting Uni, and I'm coming from C++. Just read Java for Dummies (awful book, but luckily the transition from C++ to Java is easy) and was looking for where to go next, this seems like a good start :) Thanks :)
hello people these are the imports you need import java.awt.Graphics; import java.awt.Color; import javax.swing.JPanel; import javax.swing.JComponent; import javax.swing.JFrame;
I prefer BlueJ, it has a very simple interface and shows graphical relationships (such as inheritance and other dependencies), i've also started to use NetBeans also. Also, BlueJ doesn't irritate me by constantly suggesting stuff, yet it still color-codes key words.
I have to add the import "java.awt.Graphics" because if I don't then I get errors on the setColor and fillRect methods and the g as the argument for paintComponent. Anyone know why?
Yes you can! But before you proceed further with android make sure you have a strong understanding of the basic android development. Android uses Java but to develop apps for it you need to read a lot of documentation to know how to use Android API's which is not related to java.
When a window becomes visible (uncovered or deminimized) or is resized, the "system" automatically calls the paintComponent() method for all areas of the screen that have to be redrawn.
Can anyone explain to me where the paintCompenent method is actually called? I'm confused as to how it is being used even though it is not called as part of the t object in the main method.
@gamalytical I think what's happening is the paintComponent method in the Peach class is overiding the paintComponent method in the JPanel class, and it gets called whenever the JPanel needs to be repainted.
paintComponent is called automatically when setVisible is called. This in turns calls repaint which calls paintComponent. PaintComponent belongs to JPanel class yes.
Hey i was wondering why it we come 'down' in "y" axis in programing?!?? but in maths when we say y is 25 units it means we go to UPWARD. but in computers we come DOWNWARD WHY!???
Why is it I am unable to draw two different instances of the peach. If I draw one it works, if I try to draw another the first one disappears? SOMEONE HELP!
@riky9031 I don't know, but if you are new to java like I am, I doubt it really matters to be honest. Both should work fine, although you can get both to see for yourself.
I love your videos! They are short and sweet. You do one thing in each video and you keep it short. It makes it really easy to learn one thing each day and not get overwhelmed. Thank you for making these.
You just saved me, Bucky. I've been watching your videos since I started programming. It's 3:36am and I'm still awake 'cause I didn't pay attention on our last Java class. THANK YOU VERY MUCH.
You, sir are a flat out genious!!! I am filled with gratitude! Thank You for making this tutorial, and keep up the good work!
For anyone that has done this in a class called "Graphics" and getting errors: Don't call your class Graphics.... xD
People like you make this a world worth living in
yeah, i already done that xD
Thanks
Lifesaver!
Thanks
Here is a video to create free hand painting in java:
czcams.com/video/ifVf9ejuFWI/video.html
@nath6161 nath6161 How about 'Melon?'
you know why everyone loves your channel ... cause you make java more fun :D love you man .
For those who may still be confuse, I made my own comments in the source code.
package Class;
import java.awt.*;
// ^---Contains all of the classes for creating user interfaces and for painting graphics and images.
import java.awt.event.*;
// ^---Provides interfaces and classes for dealing with different types of events fired by AWT components.
import javax.swing.*;
// ^---Provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms.
public class Class extends JPanel {
// ^---Extends JPanel to use the frame
public void paintComponent(Graphics g){
// ^---You need a paintComponent method to paint pictures
super.paintComponent(g);
// ^---You need to equip the variable g into super.paintComponent to draw pictures
this.setBackground(Color.yellow);
// ^---This sets the background color
g.setColor(Color.BLUE);
// ^---This sets the color you want to use
g.fillRect(0, 0, 10, 10);
// ^---This draws a rectangle using the color you selected
g.setColor(new Color(190,81,215));
g.fillRect(11, 11, 10, 10);
g.setColor(Color.RED);
g.drawString("This is some text.", 21, 21);
// ^---This displays a string using the color you selected
}
public static void main(String[] args) {
JFrame f = new JFrame("Title");
// ^---This creates a frame to hold the graphics
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// ^---This exits the program when you close the window
Class graphics = new Class();
// ^---This creates an object so you can access methods outside of the main method
f.add(graphics);
// ^---This adds the object to the frame
// ^---I think since paintComponent is the only method outside of the main method...
// ^---...the objects finds it, and adds it the the frame...or something...
// ^---This part kind of confuse me. It creates the object, but then it add the...
// ^---object to the frame without telling it which method to use.
f.setSize(300,300);
// ^---This sets the size of the frame
f.setVisible(true);
// ^---This sets the frame so it's visible
}
}
This appears:The serializable class Class does not declare a static final serialVersionUID field of type long
+Pandelis Symeonidis Just click on the error icon on Eclipse and select "Add default serial version ID".
Thanks a lot for source code
Notice: This is a bit different than normal coordinate system. The bigger the y is, the lower the coordinate point is (normal coordinates tell us that bigger value y is, the higher the coordinate is).
In all the programming languages that I've learned it has been that way
@@skepticalscientist4203 Yeah, that's normal.
THANKS!!! This is the only good example I've found that doesn't have errors!
why do he need to import java.awt.event.*; when he already imported java.awt.*; ?
java.awt and java.awt.event are two different imports. java.awt.* does not include java.awt.event
MrOlympus223 yes i was curious about that too, but i figured it out by myself ;)
MrOlympus223 But he put a wildstar at the end of the java.awt import, so it imports all its children automatically.
+StanlyHD I know your comment is old but i am still going to answer: If you saw every video and listened to everything he said, you would know.
+Lukas Schmidt It's episode 84, you really expect people to have seen them all?
"You should know this if you ever took a math class if your life" - Bucky
@pspiso123 Yes, the super method is just the object of the superclass, JPanel. Without extending it, you can just call it using
JPanel panel = new JPanel();
panel.paintComponents(g);
rather than
super.paintComponent(g);
I love how he says "sweet graphics". It's great xD
I really like your tutorials. We have at school java but till I saw your vids I didn't understand it. You make it very well and explain it perfect.
Thanks
I think the reason that bucky is a better teacher than some college professors is because some teachers have a "knack" for teaching, and saying what they think others will understand, but that doesn't mean all teachers that know java, also know how to teach "well". idk just a thought
Why do we need to add a JFrame object in the main class.? We never did that before. Is it because in all earlier GUIs we extend to JFrame but here we extended to JPanel?
For anyone that gets the error with super.paintComponent(g); you have to make sure that you're inheriting JPanel and not JFrame.
WOW, THAT FIXED MY PROBLEM.
I believe I was having another error or exception, but this fix still fixed my program.
Hello how are you? Can anyone help me on how I can use Graphics2D other than using JFrame, JPanel or Applets? So, my project is web, I need the drawing to be rendered in a DIV, HTML element. Can someone help me? I've looked on many websites and videos, but they all just talk about JFrame, JPanel, Applets...
You're hilarious 😂 thank you for everything Bucky
Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Hi, i tried to run the program but i only got a white screen, not rectangles ; there is a problem and its about the p component, it tells me "null" or something like i did not initialize the component called p
any difference between canvas class and jpanel class as far as making a window with some images goes? I've been told jpanel is a component of a jframe (or a window as i understand it in layman terms) and a canvas object is added to that component. So so far i think that you use .add(canvas) on a jframe with the canvs having an overriden paint method. but by simply changing class extension from canvas to JPanel, and paint() method to paintComponent() I see the same result. can anyone explain why? does this video's way make replace an existing default jpanel from the jframe and paintComponent() puts a canvas on there for us? or am I completely off the rails?
I sat down for a while but i cant figure it out by myself (dumb? -_-) How can i access the "p" variable from another class in order to make draw calls from this class?
Thank you so much! Clear, simple intro to Java drawing!
for some reason, the paint component method is coming up as red on my intelliJ program? anyone know how to help?
i wrote down the program & I've got no errors but when i run it nothing comes out .. help me out !!
Notice that the main class is different from previous videos.
me to help me pls.............
make sure it's the exact same as in the video and you should still get what he got. if youre making your own code, make sure that you change the color to something that isn't white
did u spelled "texct" wrong? :D
Oh boy oh boy OH BOY!
Just 3 tutorials away to finish this beautiful *cough* beginner *cough* journey !!!!!!!!!! :D
I am looking to write a hex-based board game. For that, I need the basic tile operations: read from file, blit to screen, write to file. I have looked all over for them. There are plenty of code samples to do this with square "tiles," as they're called, but I can't find anything that will do this with hexagons. I'm currently looking at something called "mapy," but it's confusing.
Any pointers would be appreciated.
paintComponent(Graphics g) is the method of Jpanel. We are overriding it (method overloading) and its called automatically when we override, you don't have to worry about initiating it.
i copy exactly the same code but i can't see anything and i don't get any errors, someone having the same prob here?
+Sebastian Borromeo I AM
+Ali Shahbaz It's probably because u spelt "paintComponent" wrong.
exactly I typed pain component
Yeah i did "PaintComponent" for one and "paintComponent" for the other. Thanks for the tip!
I realy admire your patience that's what i lacked and therefore couldn't become a programmer
eclipse is acting like i dont have graphics or graphics2d, what is the proper import for this, or is there an install im missing? helphelphelp
I don't understand the use of the line "super.paintComponent(g);"
My program runs fine without this line.
Also, I tried changing all the "paintCompenent"s to "paint" and the program also runs fine. Why is that? Thanks in advance for any help!
When you don't put in that line of code is because it wouldn't repaint otherwise. when moving graphics around by changing the x and y values, without that code you would see where the graphic previously was, but with that line of code the previous instance of the graphic would change every time it's updated.
@theMacBoy11 If you google "java graphics api" you can see a list of the methods you are able to use. If it is possible, then you will see a method like setColor(*your hex thingie*). If not, then it's either not possible, or the method is located in one of the parrent classes (Graphics extends other classes).
+thenewboston i have been following your tuts as far as the completion of having the images blink(close eyes open eyes). since then i have used your code to keep the screen settings and now i trying to draw two rectangles on the screen, i got thru with that. is there anyway to make it clickable?(the rectangles) using the graphics class?
Ahaiziah Shako yes there is checkout some game programing tutorials and you'll learn it
thanks will check it out
I didn't write the line of code super.paintComonent(g); And the program worked. What is it useful for ?
Cool feature in IntelliJ, if your setting custom colors a sample of the color appears to the left of the line your coding on.
Thank you for posting this video. What would be the Java code to write text into an existing word (.docx) file?
can some explain the code below please, why do we have x,y in constructor parameter initialised with axes of the top ones.
int x=10;
int y=10;
public Constructor(int x, int y){
this.x=x;
this.y=y;
}
He uses eclipse i recommend you watch the tuts in the right order youll learn a lot and it should be extremely easy to follow along
hi bucky for the fill Rect Function do i have to specify int for the variables??????i
I have to draw a cartoon character for a school project and *I have a question.* How to I pass multiple parameters to the paintComponent method. I tried this:
public void paintComponent(Graphics g, Graphics g2) {
Graphics2D pearl = (Graphics2D) g;
Graphics2D wings = (Graphics2D) g2;
I wanted to draw the different parts separately, however this printed nothing.
public void paintComponent(Graphics g) {
Graphics2D pearl = (Graphics2D) g;
This however worked just fine. The issue is that I've already drawn a big part of my character, but I need to draw parts behind what I already have, and whenever I do that, things like .rotate() and .translate() will mess up everything that comes after, so I was hoping I could draw them in a separate method or something (I'm still rather new to Java so forgive my possible errors in terminology).
I'd REALLY appreciate if you could respond asap. Currently drowning in other homework and I had to put this aside!
P.S.
I extended JComponent into the class. not JPanel.
The character I'm drawing is the pokemon Palkia.
cdn.bulbagarden.net/upload/thumb/6/66/484Palkia.png/250px-484Palkia.png
I've already drawn the big shield thing with the pearl in the center on her shoulder, but I now need to draw the three wings that are behind that and that go rightwards. I don't know how to .rotate() those wings without rotating what I have as well.
Thank you
Hi focky (how do you spell it)
I'd like to draw an arrow between 2 buttoms to represent a graph (vertex-edges) do you have any suggestion for me? thanks dude!
guille vazquez yes i have suggestion you can create arrow image in photoshop and put that picture in your program ;)
What happens if you don't put the super.paintComponent(g) line? I didn't use it in any of my drawings before and they still worked.
Could you please tell me what text editor are you using in this video? It looks pretty cool. I'm using plain old Notepad++ and obviously it doesn't give any suggestions:(...
That's Eclipse.
hi, i need to draw standard normal diviation, whats the easiest way to draw that line? o do i actually have to make 100ths of drawLine statements and then sort each dot on the line?
I loked at the date it was uploaded and i thought it said 2019 and was like "I knew my schedule was off but WOW I'm behind on EVERYTHING."
Here is a video to create free hand painting in java:
czcams.com/video/ifVf9ejuFWI/video.html
Hi! Just a confused beginner from video 37! Hope to get to this video some time!
you never showed us how to download the java 2D library when i go on the site i am seeing lots of links and still cant access the libraries pls help it;s firetrucking frustrating man????
Doesn't that come included with Java? Hopefully I'm not wrong :).
regarding 2d character movements If my sprite sheet is horizontal would it cause problems?
I've looked at his video and my work over and over but I can't find my mistake. I copied exactly what bucky did but my GUI is blank. My program doesn't even give me errors. Anyone know what could be wrong?
I've got the same problem
Mtho Kunene instead of extends JPanel try extends JFrame, I don't know why it works but it does
SynEnergy You have type what is there in the main method in the bucky class...not whatever in the peaches class....
there , there are certain code which is required to set up the JFrame/JPanel !
Thanks for the help. :)
SynEnergy lol took mea day to realize I left out f.add(p);
the step super.PaintComponent(g);
its giving me error saying
graphics.Graphics canot be converted to java.awt.graphics what thats suppose to mean any one help
+Dawood Ahmed PaintComponent is wrong, use paintComponent
can we write g in the place of this ?? both will do same work or theres some diff.
im just a complete beginner with java (i havent been following these tuts), but i believe since you are importing those methods, you have to run them off the same name, so i believe they have changed the name of that method from the time that this tut came out and the time that you are doing this!
Tìm hôm qua tới giờ mới thấy. Thank you very much.
Bro only white page is displaying the out screen .what the promble I don't know bro😭😭
@ConMahaff alright found the problem, spelled Component wrong, my mistake
how can i delete components from the window (JFrame ) not closing the window, just by pressing some button ?
+Void U can just repaint something else.
+Void I would get the background color and put a rectangle over the thing you want to remove.
"Make sure you spell tecxt wrong or it won't work" haha I love Buckey
so weird, after I compile it (no errors) nothing prints out on the screen, its just an empty box. I've checked my code like 10 times now, still haven't found a problem. Has anyone else gotten this?
such a nice how u talk and explain the stuff
If I correctly understand: whenever I want to draw something, I should call repaint() and all instruction for drawing have to be in the paintComponent() function?
But how that function know, what to draw? Should I pass somehow specific instruction (in component's public variables?) to that function? Or should I biuld my program in such way, that everything is in the paintComponent()?
I have in mind a program which takes from user some info and then draw something based on that data.
If your program doesn't work maybe you could resolve that writing:
"public class Example extends jFrame" instead of the
"public class Example extends Jpanel" that he shows in the video,if that doesn't work try
"public class Example extends JComponent".
If you get an error "The serializable class Example does not declare a static final serialVersionUID field of type long" simply click on that error icon on the Eclipse IDE and click on "Add default serial version ID".
if some one could tell me why mine is not working I have
public void paintComponent(Graphics g)
as the error in my code saying that it is a bad class file.
If somebody could tell me the problem i would have much thanks!
We didn't create the object of Graphics class. How can we access all the methods from Graphics class??
... does the object make the final drawing that of a "g-string"?
.
.
.
.
.
.
I'll let myself out...
@theMacBoy11 I did a little Googling, and I saw that if you make a new Color class you can specify the HEX number directly in the constructor's parameter like this: java.awt.Color colorNameHere = new java.awt.Color(0xFF0096); as long as you remember the '0x' before the HEX code.
You can then use that color object with the g.setColor(colorNameHere); to use the example in the video to change to color.
Have fun! :D
I prefer BlueJ, it has a very simple interface and shows graphical relationships between classes (such as inheritance and other dependencies), i've also started to use NetBeans also.
Also, BlueJ doesn't irritate me by constantly suggesting stuff, yet it still color-codes key words.
'My ultimate goal is to make computer games, and we can't make computer games without SWEET graphics' - quote of the day by a mile, although I'm sure the guy who made pong said the same...
@thenewboston
I'm a bit confused. This looks a bit like a recursive method:
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
The "super" part means it runs the parents class's paintComponent(g) method (the parent class being JPanel in this case, as that is the class that is extended), so rather than just running your code, some of the java code is run first, probably to make sure everything's initialised properly.
Hope this clears up your confusion :)
Wow this is great, looking to learn Java before starting Uni, and I'm coming from C++. Just read Java for Dummies (awful book, but luckily the transition from C++ to Java is easy) and was looking for where to go next, this seems like a good start :) Thanks :)
hello people these are the imports you need
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JFrame;
I prefer BlueJ, it has a very simple interface and shows graphical relationships (such as inheritance and other dependencies), i've also started to use NetBeans also.
Also, BlueJ doesn't irritate me by constantly suggesting stuff, yet it still color-codes key words.
I have to add the import "java.awt.Graphics" because if I don't then I get errors on the setColor and fillRect methods and the g as the argument for paintComponent. Anyone know why?
Yes you can! But before you proceed further with android make sure you have a strong understanding of the basic android development. Android uses Java but to develop apps for it you need to read a lot of documentation to know how to use Android API's which is not related to java.
thank you so so so much i couldn't figure this out but you helped me!
@EarthWorms77 I got it, i had to import another file. Now I am doing graphics like a boss!
When a window becomes visible (uncovered or deminimized) or is resized, the "system" automatically calls the paintComponent() method for all areas of the screen that have to be redrawn.
Same thing here. I reread my code so many times before deciding to check the comments.
how would i make a square appear if i use this in a action listener? say if i press a button. i cannot figure this out!
Can anyone explain to me where the paintCompenent method is actually called? I'm confused as to how it is being used even though it is not called as part of the t object in the main method.
In bucky.java:
f.setLocationRelativeTo(null); // Pop up window in the middle.
@gamalytical I think what's happening is the paintComponent method in the Peach class is overiding the paintComponent method in the JPanel class, and it gets called whenever the JPanel needs to be repainted.
paintComponent is called automatically when setVisible is called. This in turns calls repaint which calls paintComponent.
PaintComponent belongs to JPanel class yes.
Thanks, man. I changed the color like mad and wondered why the canvas is still grey.
Hey i was wondering why it we come 'down' in "y" axis in programing?!?? but in maths when we say y is 25 units it means we go to UPWARD. but in computers we come DOWNWARD
WHY!???
Or simply do this:
import java.awt.*;
import javax.swing.*;
And when you finish all the drawings, press SHIFT + CONTROL + O.
this guy is better than my computer science teacher lol
a blue rectangle chilling on our screen
Why is it I am unable to draw two different instances of the peach. If I draw one it works, if I try to draw another the first one disappears? SOMEONE HELP!
how to paint circle and square using java.awt.Canvas like drawing it
What JDK is this? I keep getting a stack overflow when I do this in JDK13 on JRE14.
Eclipse just doesnt allow me to run it when i did "f.add(p);" i followed exactly what Bucky typed but i dont know why its not letting me output it
Loved the "constant" joke :))
Great work mate - thanks for sharing
I added the second class to my main class, with the f.add(p); and I have nothing showing up on my screen still
can you draw a blue rectangle, and then change the color to red when you click it?
@riky9031 I don't know, but if you are new to java like I am, I doubt it really matters to be honest. Both should work fine, although you can get both to see for yourself.
@scullshot ...Create the action or method for a graphic to be drawn inside the action handler for your JButton
If there is more than one rectangle which one gets filled?
you must do p.add(f) instead of f.add(p). You cannot add a window to a container, only a container to a window