Showing posts with label draw. Show all posts
Showing posts with label draw. 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
*********************************************************************

Draw grid on JFrame

Complete source code below will show you, how to draw grid on a JFrame.

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


import javax.swing.JFrame;

import java.awt.Graphics;

public class DrawGridOnJFrame extends JFrame
{
//Constructor
public DrawGridOnJFrame()
{
//Set JFrame title to ( Draw grid on JFrame )
setTitle("Draw grid on JFrame");

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

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

//Make JFrame visible
setVisible(true);
}


/**
*To draw grid line on JFrame we will override it's paint method.
*We will draw grid with size 10 pixels width and 10 pixels height for each grid box
**/
public void paint(Graphics g)
{
//Override paint method in superclass
super.paint(g);

//Get current JFrame width
int frameWidth=getSize().width;

//Get current JFrame height
int frameHeight=getSize().height;

int temp=0;

//Draw vertical grid line with spacing between each line equal to 10 pixels
while(temp<frameWidth)
{
temp=temp+10;
g.drawLine(temp,0,temp,frameHeight);
}

temp=0;

//Draw horizontal grid line with spacing between each line equal to 10 pixels
while(temp<frameHeight)
{
temp=temp+10;
g.drawLine(0,temp,frameWidth,temp);
}
}

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


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

Draw rectangle in JFrame

Source code below will show how to draw rectangle in JFrame.

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


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawRectangleInJFrame extends JFrame
{
public DrawRectangleInJFrame()
{
//Set JFrame title
super("Draw A Rectangle In JFrame");

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

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

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

//draw rectangle outline
g.drawRect(50,50,300,100);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill rectangle with RED
g.fillRect(50,50,300,100);
}

public static void main(String[]args)
{
DrawRectangleInJFrame dlijf=new DrawRectangleInJFrame();
}
}


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

Draw square in JFrame

Source code below will show you, how to draw a square in a JFrame. For your information, square has 4 equal side. It mean it has 4 side with same length.

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


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawSquareInJFrame extends JFrame
{
public DrawSquareInJFrame()
{
//Set JFrame title
super("Draw A Square In JFrame");

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

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

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

//draw square outline
g.drawRect(50,50,100,100);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill square with RED
g.fillRect(50,50,100,100);
}

public static void main(String[]args)
{
DrawSquareInJFrame dlijf=new DrawSquareInJFrame();
}
}


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

Draw oval in JFrame

Source code below will show you, how to draw an oval in a JFrame

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


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawOvalInJFrame extends JFrame
{
public DrawOvalInJFrame()
{
//Set JFrame title
super("Draw An Oval In JFrame");

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

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

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

//draw oval outline
g.drawOval(50,50,100,300);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill oval with RED
g.fillOval(50,50,100,300);
}

public static void main(String[]args)
{
DrawOvalInJFrame dlijf=new DrawOvalInJFrame();
}
}


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

Draw circle in JFrame

Source code below will show you, how to draw a circle in a JFrame

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


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawCircleInJFrame extends JFrame
{
public DrawCircleInJFrame()
{
//Set JFrame title
super("Draw A Circle In JFrame");

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

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

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

//draw circle outline
g.drawOval(50,50,100,100);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill circle with RED
g.fillOval(50,50,100,100);
}

public static void main(String[]args)
{
DrawCircleInJFrame dlijf=new DrawCircleInJFrame();
}
}


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

Draw line in JFrame

Source code below will draw a line from top left corner of a JFrame until bottom right corner of the JFrame.

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


import javax.swing.JFrame;

import java.awt.Graphics;

public class DrawLineInJFrame extends JFrame
{
public DrawLineInJFrame()
{
//Set JFrame title
super("Draw A Line In JFrame");

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

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

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

//draw line in JFrame from top left corner until bottom right corner
g.drawLine(0,0,getSize().width,getSize().height);
}

public static void main(String[]args)
{
DrawLineInJFrame dlijf=new DrawLineInJFrame();
}
}


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

Draw image in JFrame

Source code below will show you, how to draw image in JFrame. Image that will be use is drawImageIntoJFrame.jpg like below


drawImageIntoJFrame.jpg


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


import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.Image;
import java.awt.Graphics;
import java.awt.Toolkit;

public class DrawImageOnJFrame extends JFrame
{
Image imageToBeDraw;
ImageIcon ii;

public DrawImageOnJFrame()
{
//set JFrame title
super("Draw Image On JFrame");

//Get image. You must change image location follow to image location in your computer.
imageToBeDraw=Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\evergreen\\Desktop\\drawImageIntoJFrame.jpg");

//Create an ImageIcon object
ii=new ImageIcon(imageToBeDraw);

//set close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set JFrame size follow the image size
setSize(ii.getIconWidth(),ii.getIconHeight());

//make you JFrame cannot resizable
setResizable(false);

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

public void paint(Graphics g)
{
//This will draw drawImageIntoJFrame.jpg into JFrame
g.drawImage(imageToBeDraw,0,0,this);
}

public static void main(String[]args)
{
DrawImageOnJFrame diojf=new DrawImageOnJFrame();
}
}


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

How to draw rotate string in java ???

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


import java.awt.Frame;
import java.awt.Panel;

import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import java.awt.geom.AffineTransform;

public class DrawRotateText extends Panel
{
public DrawRotateText()
{
Frame a=new Frame("Draw Rotate Text");
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

a.addWindowListener(wl);
a.add(this);
a.setSize(400,400);
a.setVisible(true);
}

public void paint(Graphics g)
{
Graphics2D g2d=(Graphics2D)g;
AffineTransform at=new AffineTransform();
at.setToRotation(-Math.PI/4);//SET ROTATION HERE IN radians
g2d.setTransform(at);

g2d.drawString("HI EVERYONE !!",100,200);
}

public static void main(String[]args)
{
DrawRotateText drt=new DrawRotateText();
}
}


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

PART 3 Java Source Code : Draw String In Java

This part will show you why when you set g.drawString("HI EVERYONE",10,50); to
g.drawString("HI EVERYONE",10,10); in PART 1, you can't see the wording in frame. This is because wording "HI EVERYONE " is hide at back of frame title. So if you want to see it, you just call method setUndecorated for frame and set it's argument to true. This will make your frame title hide and you can see the wording.

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


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText3 extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText3()
{
super("Drawing Text");
addWindowListener(wl);
setUndecorated(true);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);

g.drawString("HI EVERYONE",10,10);
}

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


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

PART 2 Java Source Code : Draw String In Java

This part of draw string in java is not much different to the previous part. In this part, we just use FontMetrics class to get information about Font like it's height. You can see it at g.drawString("HI EVERYONE",10,fm.getHeight()*3);

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


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.FontMetrics;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText2 extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText2()
{
super("Drawing Text");
addWindowListener(wl);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);

FontMetrics fm=g.getFontMetrics();
g.drawString("HI EVERYONE",10,fm.getHeight()*3);
}

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


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

PART 1 Java Source Code : Draw String In Java

This is part one of draw string in java. If you change g.drawString("HI
EVERYONE",10,50); in source code below to g.drawString("HI EVERYONE",10,10); you can't see wording. If you want to see it, you can see part three of draw string in
java.So you can know why you can't see it in this part.

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


import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DrawingText extends Frame
{
WindowListener wl=new WindowAdapter()
{
public void windowClosing(WindowEvent evt)
{
System.exit(0);
}
};

public DrawingText()
{
super("Drawing Text");
addWindowListener(wl);
setSize(400,200);
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("Serif",Font.BOLD,12));
g.setColor(Color.BLACK);
g.drawString("HI EVERYONE",10,50);
}

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


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

RELAXING NATURE VIDEO