//------------------------------------------------ public class Main{ public static void main(String[] args) {
new MyFrame();
} } //------------------------------------------------ import java.awt.*; import javax.swing.*; public class MyFrame extends JFrame{
MyPanel panel;
MyFrame(){
panel = new MyPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } //------------------------------------------------ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyPanel extends JPanel implements ActionListener{ final int PANEL_WIDTH = 500; final int PANEL_HEIGHT = 500; Image enemy; //Image backgroundImage; Timer timer; int xVelocity = 1; int yVelocity = 1; int x = 0; int y = 0;
MyPanel(){ this.setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT)); this.setBackground(Color.black); enemy = new ImageIcon("enemy.png").getImage(); //backgroundImage = new ImageIcon("space.png").getImage(); timer = new Timer(10, this); timer.start(); } public void paint(Graphics g) {
super.paint(g); // paint background
Graphics2D g2D = (Graphics2D) g;
//g2D.drawImage(backgroundImage, 0, 0, null); g2D.drawImage(enemy, x, y, null); }
@Override public void actionPerformed(ActionEvent e) {
if(x>=PANEL_WIDTH-enemy.getWidth(null) || x=PANEL_HEIGHT-enemy.getHeight(null) || y
I think it is worth paying attention to the fact that the method public void paint(Graphics g) in MyPanel class should be declared as: @Override public void paint(Graphics g)
Simple the best programming videos! 100% Certain! Thank you Bro Code!, I have learned more with YOU with a few videos than with many programming books combined! You always Straight to the point, total knowledge, and nice manners for teaching!
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
I like your explanations. You are simplifying one main method of designing in Java, which helps to many beginners by programing. One High point for you bro👍
very helpful as always! could I ask you cover rotating images/icons in future videos? tried various methods for the past hour, and I don't know how to implement any of the methods I came across
Thank you very much for this tutorial. I need it for one little program that i hope to write in the next days. I will write the URL of this video if i will be successful in my project. You have the great gift to explain very difficult things in a very simple way. Thanks you again; Mr. Bro Code!!! Paolo Maria Guardiani
Hey bro, love your tutorials, and love your channel! You're so good, everybody of all ages can learn from you! I just have on question (not related to your video, nor teaching skills). I Want to make a trail that follows the enemy and makes a path of it, but how would I do that? I'm tried making a line, but I'm having trouble making it follow behind the character, and I'm having trouble making the previous lines stay, as the background (I think) keeps painting over it! Also, is there a way to clone the enemy, like multiple new instances of it, maybe to make a game like space invaders with it?
Is there any way to keep the animation panel in the background? For example, I want to have clickable labels (via mouse listeners) and buttons over the animation. I tried that, but the animation panel ends up completely covering everything else.
you should probably use two different layeredPanes one for the animation and one for the buttons and labels. Here's the video about layered panes: ruclips.net/video/CmK1nObLxiw/видео.html
I'm facing a problem ,when i write timer = new Timer(1000,null); timer.start(); The constructor Timer(int,null)is undefined The method start()is undefined for the type timer
@@BroCodez I found the source of the problem,import wrong. import java.util.Timer; change to import javax.swing.Timer; Thank you for your patience reply
interesting that there are more than one Timer class from Java one from swing and i found another from util. i just know because code doesn't work when i import wrong package lol.
The Swing timer is used to perform actions on the Event Dispatch Thread after a certain delay. It's to prevent issues that multithreading can cause in regards to GUI components. The designers of Swing decided that all changes to GUI components must happen within one thread, which prevents a lot of headaches compared to changing the state of GUI components from multiple threads. If you are writing an app that's based on a schedule and it doesn't use GUI Swing components, then it's better to use the util Timer class for general scheduling.
I have tried to make the movement logic by my ownself . Undoubtedly , your way is much more clever and efficient . Code boolean goLeft = false , goRight = true , goUp = false , goDown = true ; if ((x+100) + xVelocity > panelWidth) // 100 is the image width . { goRight = false ; goLeft = true ; } else if (x - xVelocity < 0) { goLeft = false ; goRight = true ; }
if (goRight) { x += xVelocity ; } else if (goLeft) { x -= xVelocity ; }
if ((y+100) + yVelocity > panelHeight) // 100 is the image height . { goDown = false ; goUp = true ; } else if (y - yVelocity < 0) { goUp = false ; goDown = true ; } if (goUp) { y -= yVelocity ; } else if (goDown) { y += yVelocity ; }
Hi bro I'm having issues with when im creating a timer I also implemented the actionlistener and import the timer Timer timer; timer = new Timer(1000, this); It says it is undefined
My images wink constantly. I think it is about the 10 seconds timer and it paints constantly. I see the painting winking I think but it works correct on your computer. How can I solve this?
I have a background image that is 1024x1024 but the JPanel is 500x500. The background will not display at all if I do not change the background image size to match the JPanel. Is there a way to resize the background image with code?
This doesn't work when I copy-pasted it into eclipse. I changed the "enemy = new ImageIcon("enemy.png).getImage();" to "enemy = new ImageIcon("grass.png").getImage();", but the image doesn't show up at all. Any suggestions?
Well, put the image, on the same location as whr your project file📁. For example my class files and project files within my D-drive. D:(drive name) \java(folder name) \practice(another foldername) \anime(java project name) \enemy(image)
This is a great video, but I have a question, if I want to draw more than 1 object, but they are drawn independently on the same frame, what should I do?
Just put the object code right after the other object, thats it. (you might wanna change some variables, and I don't know how to fix collision but feel free to try it)
My image starts buffering and is stuck at the max x-position in my code, it's still an image, but it's unrelated to this exact code. It starts buffering at x = 900 and fiddles around that. Is there a reason why it won't start moving back towards the left?
BRO CODE !!!!! PLS REPLY i want to get width and height from ym java graphics but i dont know how to get width and height from a java graphics so pls tell me how to do that
Since the draw method is a void type, you can't directly get them. That's the reason he used separate variables to move the position in this video. You pretty much have to do the same, create separate variables and set those as the height and width.
A few questions, I'm trying to get a small circle or image to travel on the edges of a diamond-shaped drawing(like a baseball home run). I was wondering how you would write it where it would reach a corner/coordinates and then have it turn to run to another corner, and then stop at the homebase? Another question is that I'm thinking of implementing a system where the user could input the speed the circle would travel in, should I have it where it change the velocity or the timer? And lastly, how would I implement a pause mechanic, such as when I pause the program and resume it? Your videos are really helpful btw.
use a switch method. Put a case for every base's coordinates and for the start, and once each is reached change the velocity to make it go the right way
Either get a separate engine or do maths to calculate it using the fillPolygon function in the video earlier. I don't think there's a class just for 3d, but you might wanna google that
The paint method rewritting doesn't work for me, the image doesn't appear ... i wrote everything for sure (i read my code a 10 times), what can be the problem ?
this is really late but what I found is that you need to change the overriden Paint method to paintComponent. Then change super.paint() to super.paintComponent()
Hmmm, I would first check the folder that your image is in. Second I would check to make sure that I have the line of code to draw the image g2D.drawImage(enemy, x, y, null);
@@BroCodez wow! Thanks for the reply. Yea inteli J doesn’t show the whole ‘default package’ but my images are within the same folder as my classes. I’ve also set the image observer to null.
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...
//------------------------------------------------
public class Main{
public static void main(String[] args) {
new MyFrame();
}
}
//------------------------------------------------
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame{
MyPanel panel;
MyFrame(){
panel = new MyPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(panel);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
//------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyPanel extends JPanel implements ActionListener{
final int PANEL_WIDTH = 500;
final int PANEL_HEIGHT = 500;
Image enemy;
//Image backgroundImage;
Timer timer;
int xVelocity = 1;
int yVelocity = 1;
int x = 0;
int y = 0;
MyPanel(){
this.setPreferredSize(new Dimension(PANEL_WIDTH,PANEL_HEIGHT));
this.setBackground(Color.black);
enemy = new ImageIcon("enemy.png").getImage();
//backgroundImage = new ImageIcon("space.png").getImage();
timer = new Timer(10, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g); // paint background
Graphics2D g2D = (Graphics2D) g;
//g2D.drawImage(backgroundImage, 0, 0, null);
g2D.drawImage(enemy, x, y, null);
}
@Override
public void actionPerformed(ActionEvent e) {
if(x>=PANEL_WIDTH-enemy.getWidth(null) || x=PANEL_HEIGHT-enemy.getHeight(null) || y
thx
Awesome
Nice
I think it is worth paying attention to the fact that the method
public void paint(Graphics g) in MyPanel class should be declared as:
@Override
public void paint(Graphics g)
Practicing...
public class Main
{
public static void main (String[]args)
{
new MyFrame ();
}
}
******************
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame
{
MyPanel panel;
MyFrame ()
{
panel = new MyPanel ();
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.add (panel);
this.pack ();
this.setLocationRelativeTo (null);
this.setVisible (true);
}
}
******************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyPanel extends JPanel implements ActionListener
{
final int PANEL_WIDTH = 250;
final int PANEL_HEIGHT = 250;
Image avatar;
Image backgroundImage;
Timer timer;
int xVelocity = 2;
int yVelocity = 1;
int x = 0;
int y = 0;
MyPanel ()
{
this.setPreferredSize (new Dimension (PANEL_WIDTH, PANEL_HEIGHT));
this.setBackground (Color.black);
avatar = new ImageIcon ("avatar.png").getImage ();
backgroundImage = new ImageIcon("sea.png").getImage();
timer = new Timer (20, this);
timer.start ();
}
public void paint (Graphics g)
{
super.paint (g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(backgroundImage,0,0,null);
g2D.drawImage (avatar, x, y, null);
}
@Override public void actionPerformed (ActionEvent e)
{
if (x >= PANEL_WIDTH - avatar.getWidth (null) || x > 0)
{
xVelocity = xVelocity * -1;
}
x = x + xVelocity;
if (y >= PANEL_HEIGHT - avatar.getHeight (null) || y > 0)
{
yVelocity = yVelocity * -1;
}
y = y + yVelocity;
repaint ();
}
}
the satisfying moment when the alien perfectly hits the corner of the screen
Simple the best programming videos! 100% Certain! Thank you Bro Code!, I have learned more with YOU with a few videos than with many programming books combined! You always Straight to the point, total knowledge, and nice manners for teaching!
This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro
Excited to watch!!!!!!!!!!
I like your explanations. You are simplifying one main method of designing in Java, which helps to many beginners by programing. One High point for you bro👍
you got me when you said "I hate awkward silences" XD
I just needed to learn how to animate a line moving in a circle but this helped!
LOUD & CLEAR! THANKS BRO
thanks for watching Oberdorfer
Thank you for this. This video got to the point fast and the script is small and just what I need.
The constructor Timer(10, this) is undefined
The method start() is undefined for the type Timer
verify if you add correct import 'java.awt.event.*;' and 'implements ActionListener'
Nice
Thank you for your tutorial
This is awesome
lmaaooo siiiick haha 🔥🔥
You are My Hero bro....
Outstanding!
really cool
I am very happy for the video, it allowed me to understand enough things. Thanks again!
Thanks dude, really needed this!
that's so sick!
Great
very helpful as always!
could I ask you cover rotating images/icons in future videos? tried various methods for the past hour, and I don't know how to implement any of the methods I came across
that's not a bad idea
@@BroCodez how to remove white background from image just like in your case enemy image doesn't have any background
This is exactly what i need!!!
Awesome. Marvelous. Splended. Thank you very much bro code!
SWAG DUDE!!!!
Thank you very much for this tutorial. I need it for one little program that i hope to write in the next days. I will write the URL of this video if i will be successful in my project.
You have the great gift to explain very difficult things in a very simple way.
Thanks you again; Mr. Bro Code!!!
Paolo Maria Guardiani
Dude after 12 fucking long hours.. Thank you so much!!
Thanks! Doing a great job
Can you provide the link where u download the images
you're the bro
thank you . this is very helpful and easy to understand.
Génial la vidéo !
Thanks really coll content!🔥🔥🔥
How do we get the icons for the project? I've been searching far and wide and I can't seem to find one that works for the code 😭 🙏
My notification is broken, the notification came 23secs ago and bro code's comment was 17hr ago wth
He probably unlisted/made the video private when uploading 17 hours ago and only now made it public
I scheduled the video for release at a certain time
thank you very helpful ...
Thank you!
wow this was very useful
Thanks Bro
How do you get all of your images to be perfectly for the panel, did you manually change it for the panel or do something special?
This will be very useful!!
👍👍👍
How to increase background image size
I also want to know that...
thanks bro
Learnt new things... ( I was doing it so wrongly) 😂
Thank you !!!
Hey bro, love your tutorials, and love your channel! You're so good, everybody of all ages can learn from you! I just have on question (not related to your video, nor teaching skills).
I Want to make a trail that follows the enemy and makes a path of it, but how would I do that? I'm tried making a line, but I'm having trouble making it follow behind the character, and I'm having trouble making the previous lines stay, as the background (I think) keeps painting over it!
Also, is there a way to clone the enemy, like multiple new instances of it, maybe to make a game like space invaders with it?
I loveeee it
thanks for watching Aya
Thanks a lot!
Hello from Russia🇷🇺!
Is there any way to keep the animation panel in the background? For example, I want to have clickable labels (via mouse listeners) and buttons over the animation. I tried that, but the animation panel ends up completely covering everything else.
you should probably use two different layeredPanes one for the animation and one for the buttons and labels. Here's the video about layered panes: ruclips.net/video/CmK1nObLxiw/видео.html
I Want You To Turn This into a Software and Publish it for Windows, Microsoft and MacOS
wao superrr
Nice! I tried this with xVelocity = 10 and yVelocity = 21; Excellent!!
I'm facing a problem ,when i write
timer = new Timer(1000,null);
timer.start();
The constructor Timer(int,null)is undefined
The method start()is undefined for the type timer
replace timer = new Timer(1000,null);
with
timer = new Timer(1000,this);
@@BroCodez I found the source of the problem,import wrong.
import java.util.Timer;
change to import javax.swing.Timer;
Thank you for your patience reply
@@jpiongchuanho3422 thanks! :D
thanks
interesting that there are more than one Timer class from Java one from swing and i found another from util. i just know because code doesn't work when i import wrong package lol.
The Swing timer is used to perform actions on the Event Dispatch Thread after a certain delay. It's to prevent issues that multithreading can cause in regards to GUI components. The designers of Swing decided that all changes to GUI components must happen within one thread, which prevents a lot of headaches compared to changing the state of GUI components from multiple threads.
If you are writing an app that's based on a schedule and it doesn't use GUI Swing components, then it's better to use the util Timer class for general scheduling.
thank you my friend , i had this problem but now i fixed it
I modified the code and made the famous DVD logo moving around
Loved it bro
thanks for watching!
Thanks
Thank you
Thanks, Everything works Perfect. Just wanted to know why the hell it lags af
I wanna know also
Yo bro your playlist need some changes timer vid needs to be before in the 2D animation... I don't get the velocity thing and timer thing here
These are really good videos but I can't get mine to draw any images.
I have tried to make the movement logic by my ownself . Undoubtedly , your way is much more clever and efficient .
Code
boolean goLeft = false , goRight = true , goUp = false , goDown = true ;
if ((x+100) + xVelocity > panelWidth) // 100 is the image width .
{
goRight = false ;
goLeft = true ;
}
else if (x - xVelocity < 0)
{
goLeft = false ;
goRight = true ;
}
if (goRight)
{
x += xVelocity ;
}
else if (goLeft)
{
x -= xVelocity ;
}
if ((y+100) + yVelocity > panelHeight) // 100 is the image height .
{
goDown = false ;
goUp = true ;
}
else if (y - yVelocity < 0)
{
goUp = false ;
goDown = true ;
}
if (goUp)
{
y -= yVelocity ;
}
else if (goDown)
{
y += yVelocity ;
}
Can we implement this using labels?
woow he used eclipse
Hi bro I'm having issues with when im creating a timer I also implemented the actionlistener and import the timer
Timer timer;
timer = new Timer(1000, this);
It says it is undefined
me 2
how to pass the argument into the "constructor of timer" ?
My images wink constantly. I think it is about the 10 seconds timer and it paints constantly. I see the painting winking I think but it works correct on your computer. How can I solve this?
I had the same issue, you might have switched position and size arguments when you drew the image
I have a background image that is 1024x1024 but the JPanel is 500x500. The background will not display at all if I do not change the background image size to match the JPanel. Is there a way to resize the background image with code?
shet man
maybe I screwed up the build path or something but its just not working I have gotten crazy errors even though its duplicate code no clue
This doesn't work when I copy-pasted it into eclipse. I changed the "enemy = new ImageIcon("enemy.png).getImage();" to "enemy = new ImageIcon("grass.png").getImage();", but the image doesn't show up at all. Any suggestions?
make sure the image has the same name as the image you have in your project folder
add src/ in front of the image name. I don't know why but some stuff that shouldn't work works for him.
Well, put the image, on the same location as whr your project file📁. For example my class files and project files within my D-drive. D:(drive name) \java(folder name) \practice(another foldername) \anime(java project name) \enemy(image)
@@Blackilykat you saved my day
Please apply for teacher position at my school.
👍🏻
This is a great video, but I have a question, if I want to draw more than 1 object, but they are drawn independently on the same frame, what should I do?
Just put the object code right after the other object, thats it. (you might wanna change some variables, and I don't know how to fix collision but feel free to try it)
as long as they are both before the repaint() method they will get updated in the same frame
How can i remove the white background of the image
My image starts buffering and is stuck at the max x-position in my code, it's still an image, but it's unrelated to this exact code. It starts buffering at x = 900 and fiddles around that. Is there a reason why it won't start moving back towards the left?
BRO CODE !!!!! PLS REPLY i want to get width and height from ym java graphics but i dont know how to get width and height from a java graphics so pls tell me how to do that
Since the draw method is a void type, you can't directly get them. That's the reason he used separate variables to move the position in this video. You pretty much have to do the same, create separate variables and set those as the height and width.
@@ashwinsights3952 sorry for the late reply. Did you want to get it from a frame, panel or label?
A few questions, I'm trying to get a small circle or image to travel on the edges of a diamond-shaped drawing(like a baseball home run). I was wondering how you would write it where it would reach a corner/coordinates and then have it turn to run to another corner, and then stop at the homebase? Another question is that I'm thinking of implementing a system where the user could input the speed the circle would travel in, should I have it where it change the velocity or the timer? And lastly, how would I implement a pause mechanic, such as when I pause the program and resume it?
Your videos are really helpful btw.
use a switch method. Put a case for every base's coordinates and for the start, and once each is reached change the velocity to make it go the right way
Please talk about 3d 🙏🙏🙏
Either get a separate engine or do maths to calculate it using the fillPolygon function in the video earlier. I don't think there's a class just for 3d, but you might wanna google that
how do you make the animationt to stop?
how that image coming on your screen
make sure to add src/ in front of the image name
i did following that you did but my image don`t show out
Is there any way that you can able to click the enemy?
Is there a way to repaint individual objects?
You could put the objects in different JPanels and individually update those
thank you!!
thank you for watching fafa
The paint method rewritting doesn't work for me, the image doesn't appear ... i wrote everything for sure (i read my code a 10 times),
what can be the problem ?
always share the code itself when making a question about it
this is really late but what I found is that you need to change the overriden Paint method to paintComponent. Then change super.paint() to super.paintComponent()
Thanks so much man I was hopelessly stuck with my school project
I've copied and pasted this code, I'm using inteli J though. This first stage with displaying the images does not appear to be working?
Hmmm, I would first check the folder that your image is in. Second I would check to make sure that I have the line of code to draw the image g2D.drawImage(enemy, x, y, null);
@@BroCodez wow! Thanks for the reply. Yea inteli J doesn’t show the whole ‘default package’ but my images are within the same folder as my classes. I’ve also set the image observer to null.
@@johnnymccauley5923 i have the same problem can you help me
@@oussamagoumghar1596 heys! I haven’t revisited it since but I did do another UI tutorial with labels. Move the images outside the package.
Thanks Johnny
one more comment for the algorithm
Hi guys.
I am following the instructions but my Panel doesn't not show up
did you make a JFrame and added the JPanel inside?
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...
I cant run it whyyy? How can you all run it
Timer doesn't work including star(). java.awt.Timer was imported. i just cannot pass parameter Timer().
why?
thank you.
That's because, the Timer import on javax.swing.Timer; that used in this program
Cause it doesn't use java.awt.Timer, but javax.swing.Timer. I suggest importing javax.swing.*, java.awt.* and java.awt.event.* to avoid these issues
hi
better way to illustrate is shown