Java 2D graphics 🖍️

preview_player
Показать описание
Java 2d graphics GUI swing tutorial for beginners

#Java #2D #graphics #tutorial #beginners #shapes #paint()

// ---------------------------------------------
public class Main{

public static void main(String[] args) {

new MyFrame();

}
}
// ----------------------------------------------

public class MyFrame extends JFrame{

MyPanel panel;

MyFrame(){

panel = new MyPanel();


}
}
// ----------------------------------------------

public class MyPanel extends JPanel{

//Image image;

MyPanel(){

}

public void paint(Graphics g) {

Graphics2D g2D = (Graphics2D) g;

//g2D.drawImage(image, 0, 0, null);

g2D.setStroke(new BasicStroke(5));
g2D.drawLine(0, 0, 500, 500);

//g2D.drawRect(0, 0, 100, 200);
//g2D.fillRect(0, 0, 100, 200);

//g2D.drawOval(0, 0, 100, 100);
//g2D.fillOval(0, 0, 100, 100);

//g2D.drawArc(0, 0, 100, 100, 0, 180);
//g2D.fillArc(0, 0, 100, 100, 0, 180);
//g2D.fillArc(0, 0, 100, 100, 180, 180);

//int[] xPoints = {150,250,350};
//int[] yPoints = {300,150,300};
//g2D.drawPolygon(xPoints, yPoints, 3);
//g2D.fillPolygon(xPoints, yPoints, 3);

//g2D.setFont(new Font("Ink Free",Font.BOLD,50));
//g2D.drawString("U R A WINNER! :D", 50, 50);
}
}
Рекомендации по теме
Комментарии
Автор

//
public class Main{

public static void main(String[] args) {

new MyFrame();

}
}
//
import javax.swing.*;

public class MyFrame extends JFrame{

MyPanel panel;

MyFrame(){

panel = new MyPanel();



this.add(panel);
this.pack();

this.setVisible(true);
}
}
//
import java.awt.*;
import javax.swing.*;

public class MyPanel extends JPanel{

//Image image;

MyPanel(){

//image = new
this.setPreferredSize(new Dimension(500, 500));
}

public void paint(Graphics g) {

Graphics2D g2D = (Graphics2D) g;

//g2D.drawImage(image, 0, 0, null);

g2D.setPaint(Color.blue);
g2D.setStroke(new BasicStroke(5));
g2D.drawLine(0, 0, 500, 500);

//g2D.setPaint(Color.pink);
//g2D.drawRect(0, 0, 100, 200);
//g2D.fillRect(0, 0, 100, 200);


//g2D.drawOval(0, 0, 100, 100);
//g2D.fillOval(0, 0, 100, 100);

//g2D.setPaint(Color.red);
//g2D.drawArc(0, 0, 100, 100, 0, 180);
//g2D.fillArc(0, 0, 100, 100, 0, 180);
//g2D.setPaint(Color.white);
//g2D.fillArc(0, 0, 100, 100, 180, 180);

//int[] xPoints = {150, 250, 350};
//int[] yPoints = {300, 150, 300};

//g2D.drawPolygon(xPoints, yPoints, 3);
//g2D.fillPolygon(xPoints, yPoints, 3);


//g2D.setFont(new Font("Ink Free", Font.BOLD, 50));
//g2D.drawString("U R A WINNER! :D", 50, 50);
}
}

BroCodez
Автор

Here's the code for the picture in the thumbnail. I didn't have time to code this with you guys since I'm trying to cut down on the length of my videos:

// Landscape Image

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.add(panel);
this.pack();

this.setVisible(true);
}
}

import java.awt.*;
import javax.swing.*;

public class MyPanel extends JPanel{

Image image;

MyPanel(){

image = new
this.setPreferredSize(new Dimension(500, 500));

}
public void paint(Graphics g){

Graphics2D g2D = (Graphics2D) g;

g2D.drawImage(image, 0, 0, null);

g2D.setPaint(Color.green);
g2D.fillRect(0, 300, 500, 500);

g2D.setPaint(Color.gray);
g2D.fillRect(225, 325, 50, 100);
g2D.fillRect(325, 350, 50, 100);
g2D.fillRect(125, 350, 50, 100);

g2D.setStroke(new BasicStroke(1));
g2D.setPaint(Color.orange);
g2D.fillArc(150, 200, 200, 200, 0, 180);

int[] xPoints = {0, 100, 200};
int[] yPoints = {300, 150, 300};

g2D.setPaint(new Color(0x694d00));
g2D.drawPolygon(xPoints, yPoints, xPoints.length);
g2D.fillPolygon(xPoints, yPoints, xPoints.length);

int[] xPoints2 = {300, 400, 500};
int[] yPoints2 = {300, 150, 300};

g2D.setPaint(new Color(0x694d00));
g2D.drawPolygon(xPoints2, yPoints2, xPoints.length);
g2D.fillPolygon(xPoints2, yPoints2, xPoints.length);

g2D.setPaint(Color.green);
g2D.setStroke(new BasicStroke(10));
g2D.drawLine(0, 300, 500, 300);

g2D.setPaint(Color.YELLOW);
g2D.setFont(new Font("Ink Free", Font.BOLD, 50));
g2D.drawString("Welcome to my city", 25, 75);

}
}

BroCodez
Автор

Out of all coding related channels on youtube, this is BY FAR the best one.

DiamantOpp
Автор

This is a brilliant tutorial! straight to the point (well, ignoring the like & subscribe stuff), very straightforward and easy to understand!

bluesillybeard
Автор

After I saw the red background that I set I got very happy, at least I saw that something is working maybe now I can focus on programming and quit gaming because the past semester I am just copying my classmates work and not attending classes but when I see and try this tutorial then it worked, It got me very excited and motivated to continue, thank you very much brother keep up....

-to someone who can relate in this comment like it I'm just curious if someone experienced it beside me.

programer
Автор

Thank you for these tutorials! I have completed a basic java course but it didn't include panels, frames or graphics. Your tutorials are very helpful.

jhanzaibhumayun
Автор

draw rect like get rekt, perfectly explained 🙌 👌

ozz
Автор

I'm glad I find your channel! I've learned and still learning so much things in a short time! ^^

radupurecel
Автор

That was great. the first clear description of how to use 2D graphics i've seen.

edsiegel
Автор

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

pavelkvasnicka
Автор

Want to create moving object from one point to other, and thank you for your teaching in a simple and detailed way of explanation

ADAhd
Автор

This was a good summary on 2D graphics basics. I got this link as a course material.

mikloscsuvar
Автор

Great and simple, with no fluff! Thanks much.

zedzed
Автор

Hey, just want to say that your videos are awesome
and this video really helped me, thank you

amira
Автор

Great video, easy to follow and understand. Thanks for uploading!

tecso
Автор

Youtube never recommend your videos.. I watch a lot of videos related to programming.. i randomly searched bro code.. and found your channel

YTSHANKY
Автор

thanks you make learning fun and productive😄

kenkinyua
Автор

Sometimes if you run the code, it might maintain the previous state of the canvas. What you need to do is run each file individually, before running the main file. It might not apply to everyone, but it did for me (I am on Sublime Text 3).

theuser
Автор

thank you for this video . helped me so much to understand GUI

nawfalnjm
Автор

Gave it a like! Thanks man and great video!

cinderacedr