Showing posts with label jpanel. Show all posts
Showing posts with label jpanel. Show all posts

Draw triangle in JPanel

Complete source code below will show you, how to draw triangle in JPanel

*********************************************************************
COMPLETE SOURCE CODE FOR : DrawTriangle.java
*********************************************************************


import javax.swing.*;

import java.awt.*;

public class DrawTriangle extends JPanel
{
public DrawTriangle()
{
JFrame frame=new JFrame("Draw triangle in JPanel");
frame.add(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//All triangle corner x coordinate
int[]x={0,150,300};

//All triangle corner y coordinate
int[]y={200,0,200};

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=192
//B=0
//So after this all you draw will use this color
g.setColor(new Color(255,192,0));

//Draw triangle in JPanel
g.fillPolygon(x,y,3);

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=1
//G=1
//B=1
//So after this all you draw will use this color
g.setColor(new Color(1,1,1));

//Set font that will use when draw String
g.setFont(new Font("Arial",Font.BOLD,14));

//Draw String in JPanel
g.drawString("(0,200)",10,200);
g.drawString("(150,0)",150,20);
g.drawString("(300,200)",290,200);
}

public static void main(String[]args)
{
DrawTriangle dt=new DrawTriangle();
}

}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************

Set JPanel layout using BoxLayout

Complete source code below will show you how to set JPanel layout using BoxLayout

*******************************************************************
COMPLETE SOURCE CODE FOR : SetJPanelLayoutUsingBoxLayout.java
*******************************************************************


import javax.swing.*;

import java.awt.*;

public class SetJPanelLayoutUsingBoxLayout
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Set JPanel layout using BoxLayout )
JFrame frame=new JFrame("Set JPanel layout using BoxLayout");

//Create a panel using JPanel
JPanel panel=new JPanel();

//Set JPanel layout using BoxLayout with vertical lay out
//You can try change to BoxLayout.X_AXIS to make components lay out in horizontal
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

//Create a button with text ( First button )
JButton button=new JButton("First button");

//Create a button with text ( Second button )
JButton button2=new JButton("Second button");

//Create a button with text ( Third button )
JButton button3=new JButton("Third button");

//Create a dimension with :
//Width : 10 pixels
//Height : 10 pixels
Dimension dim=new Dimension(10,10);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//Add button into panel
panel.add(button);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//Add button2 into panel
panel.add(button2);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//Add button3 into panel
panel.add(button3);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//add panel into created JFrame
frame.add(panel);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,200);

//Make JFrame locate at center of screen
frame.setLocationRelativeTo(null);

//Make JFrame visible
frame.setVisible(true);
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Set JPanel layout using GridBagLayout

Source code below will show you, how to set JPanel layout using GridBagLayout.

**************************************************************
COMPLETE SOURCE CODE FOR : SetJPanelLayoutToGridBagLayout.java
**************************************************************


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

public class SetJPanelLayoutToGridBagLayout extends JPanel
{
/**Note**
*When it say, "It will take 4 grid for it's width"
*it mean it will use 4 box in grid and it's direction is to right
*
*When it say, "It will take 4 grid for it's height"
*it mean it will use 4 box in grid and it's direction is to bottom
*/

//Create GridBigLayout object
GridBagLayout gbl=new GridBagLayout();

//Create GridBagConstraints object
GridBagConstraints gbc=new GridBagConstraints();

//Create a label using JLabel with text ( Name : )
JLabel label=new JLabel("Name :");

//Create a label using JLabel with text ( Age : )
JLabel label2=new JLabel("Age :");

//Create a text field using JTextField with numbers of column equal to 12
JTextField textField=new JTextField(12);

//Create a text field using JTextField with numbers of column equal to 3
JTextField textField2=new JTextField(3);

//Create a button with text ( OK )
JButton button=new JButton("OK");

//Create a button with text ( Cancel )
JButton button2=new JButton("Cancel");

//Create a box with vertical layout
Box box1=new Box(BoxLayout.Y_AXIS);

//Create a box with horizontal layout
Box box2=new Box(BoxLayout.X_AXIS);

//Create a box with horizontal layout
Box box3=new Box(BoxLayout.X_AXIS);

//Create a box with vertical layout
Box box4=new Box(BoxLayout.Y_AXIS);

//Create a window using JFrame with title ( Set JPanel layout using GridBagLayout )
JFrame frame=new JFrame("Set JPanel layout using GridBagLayout");

//Constructor
public SetJPanelLayoutToGridBagLayout()
{
//Make text in label allign to right
label.setHorizontalAlignment(SwingConstants.RIGHT);
label2.setHorizontalAlignment(SwingConstants.RIGHT);

//Set panel layout to GridBagLayout
setLayout(gbl);

//Field that will be use when display area is larger than component size.
//It mean when it has some space between component, component will
//enlarge to horizontal direction
gbc.fill=GridBagConstraints.HORIZONTAL;

/*
*Add label into panel with top left corner of the label at crossing between
*first horizontal and first vertical line.
*It will take 4 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(label,1,1,4,1);

/*
*Add label2 into panel with top left corner of the
*label at crossing between
*third horizontal and first vertical line.
*It will take 4 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(label2,3,1,4,1);

/*
*Add textField into panel with top left corner of the text field at
*crossing between first horizontal and sixth vertical line
*It will take 12 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(textField,1,6,12,1);

/*
*Add textField2 into panel with top left corner of the text field at
*crossing between third horizontal and sixth vertical line
*It will take 3 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(textField2,3,6,3,1);

/*
*Add button into panel with top left corner of the button at
*crossing between fifth horizontal and fifth vertical line
*It will take 5 grid for it's width
*It will take 2 grid for it's height
*/
addComponent(button,5,5,5,2);

/*
*Add button2 into panel with top left corner of the button at
*crossing between fifth horizontal and eleventh vertical line
*It will take 5 grid for it's width
*It will take 2 grid for it's height
*/
addComponent(button2,5,11,5,2);

/*
*Add a component that is invisible with 10 pixels width
*and 10 pixels height into box1
*/
box1.add(Box.createRigidArea(new Dimension(10,10)));

/*
*Add box1 into panel with top left corner of the box at
*crossing between first horizontal and fifth vertical line
*It will take 1 grid for it's width
*It will take 3 grid for it's height
*/
addComponent(box1,1,5,1,3);

/*
*Add a component that is invisible with 30 pixels width
*and 10 pixels height into box2
*/
box2.add(Box.createRigidArea(new Dimension(30,10)));

/*
*Add box2 into panel with top left corner of the box at
*crossing between second horizontal and first vertical line
*It will take 17 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(box2,2,1,17,1);

/*
*Add a component that is invisible with 30 pixels width
*and 10 pixels height into box3
*/
box3.add(Box.createRigidArea(new Dimension(30,10)));

/*
*Add box3 into panel with top left corner of the box at
*crossing between fourth horizontal and first vertical line
*It will take 17 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(box3,4,1,17,1);

/*
*Add a component that is invisible with 10 pixels width
*and 10 pixels height into box4
*/
box4.add(Box.createRigidArea(new Dimension(10,10)));

/*
*Add box4 into panel with top left corner of the box at
*crossing between fifth horizontal and tenth vertical line
*It will take 1 grid for it's width
*It will take 2 grid for it's height
*/
addComponent(box4,5,10,1,2);

//Add panel into JFrame
frame.add(this);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//SetJFrame size
frame.setSize(500,400);

//Make JFrame center on the screen
frame.setLocationRelativeTo(null);

//Disble JFrame from resizable
frame.setResizable(false);
frame.setVisible(true);
}

//Method addComponent
public void addComponent(Component component, int row, int column, int width, int height)
{
gbc.gridx=column;
gbc.gridy=row;
gbc.gridwidth=width;
gbc.gridheight=height;
gbl.setConstraints(component,gbc);
add(component);
}

//Main method
public static void main(String[]args)
{
SetJPanelLayoutToGridBagLayout sjpltgbl=new SetJPanelLayoutToGridBagLayout();
}
}


**************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************

Get default JPanel background color

Complete source code below will show you, how to get default JPanel background color and after that, it will print RED,GREEN,BLUE(RGB) value for the color.

******************************************************************
COMPLETE SOURCE CODE FOR : GetDefaultJPanelBackgroundColor.java
******************************************************************


import javax.swing.JPanel;

import java.awt.Color;

public class GetDefaultJPanelBackgroundColor
{
public static void main(String[]args)
{
//Create a panel using JPanel
JPanel panel=new JPanel();

//Get panel background color using method getBackground()
Color panelBackgroundColor=panel.getBackground();

//Get RED value from color
int redValue=panelBackgroundColor.getRed();

//Get GREEN value from color
int greenValue=panelBackgroundColor.getGreen();

//Get BLUE value from color
int blueValue=panelBackgroundColor.getBlue();

//Print RGB value that we get
System.out.println("R : "+redValue);
System.out.println("G : "+greenValue);
System.out.println("B : "+blueValue);

//You can check what color that match the RGB value that we get using 'Color picker' at above
}
}


******************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************

Set JPanel background color

Source code below will show you, how to set JPanel background color base on RGB. You can get RGB value for your color using "Color picker" at above.

**********************************************************************
COMPLETE SOURCE CODE FOR : SetJPanelBackgroundColor.java
**********************************************************************


import javax.swing.JPanel;
import javax.swing.JFrame;

import java.awt.Color;

public class SetJPanelBackgroundColor
{
public static void main(String[]args)
{
//Create panel using JPanel
JPanel panel=new JPanel();

//Create JFrame with title ( Set JPanel background color )
JFrame frame=new JFrame("Set JPanel background color");

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JPanel background color to color that you choose
panel.setBackground(color);

//Add JPanel into JFrame
frame.add(panel);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,300);

//Make JFrame visible
frame.setVisible(true);
}
}


**********************************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************************

Why we use JPanel

JPanel we will help java programmer to create more complex graphical user interface. But how ? It answer is, it allow other component to add into it. Component that i mean here like JButton, JLabel or JTextField or others. It also give ability to other JPanel add into it. For example, we has two JPanel called panelA and panelB. We can add panelA into panelB. So, if panelA also contains other components and panelB also contains other component beside panelA that we add before, how complex the graphical user interface...i hope you can imagine it.

Create JPanel in java

Source code below will show you how to create a JPanel in java and after that we will put the JPanel into a JFrame.

***************************************************************************
COMPLETE SOURCE CODE FOR : CreateAJPanel.java
***************************************************************************


import javax.swing.JFrame;
import javax.swing.JPanel;

public class CreateAJPanel
{
public static void main(String[]args)
{
//Create a JPanel
JPanel panel=new JPanel();

//Create a JFrame that we will use to add the JPanel
JFrame frame=new JFrame("Create a JPanel");

//ADD JPanel INTO JFrame
frame.add(panel);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//WIDTH : 400 pixels
//HEIGHT : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************

Add JPanel into JFrame

Source code below will show you, how to add a JPanel into JFrame.

**************************************************************************
COMPLETE SOURCE CODE FOR : AddJPanelIntoJFrame.java
**************************************************************************


import javax.swing.JPanel;
import javax.swing.JFrame;

public class AddJPanelIntoJFrame
{
public static void main(String[]args)
{
//Create a panel
JPanel panel=new JPanel();

//Create a window using JFrame with title ( Add JPanel into JFrame )
JFrame frame=new JFrame("Add JPanel into JFrame");

//add created panel into JFrame
frame.add(panel);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************

RELAXING NATURE VIDEO