Display text and graphics in Java on JFrame

This tutorial explains how to display text and graphics on JFrmae for example, lines, circle and rectangle.

Background

Java provides us an easy way to draw text and graphics using GUI. Graphics class in AWT package allow us to draw primitive geometric types like line and circle. Other than this it can also display text.  This tutorial will explain various functions of Graphics class used to draw shapes and text.

Draw lines

Graphics class provides a method Graphics.drawline(int x1, int y1, int x2, int y2) to draw a line on the screen. While x1 is the x-coordinate of first point of line and y1 is y-coordinate of the first point of line. Similarly x2 and y2 are the coordinates of second point of line.

Here is the program which displays a line.

package javatutorial.net;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrmaeGraphics extends JPanel{

	public void paint(Graphics g){
		
		g.drawLine(10, 10, 200, 300);			
	}
	
	public static void main(String[] args){
		JFrame frame= new JFrame("Welecome to JavaTutorial.net");	
		frame.getContentPane().add(new JFrmaeGraphics());
		frame.setSize(600, 400);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);		
	}
}

Here is the output of this code

line example

line example




Draw circles

You can draw circle and oval with the help of method Graphics.drawOval(int x, int y, int width, int height). This function serves both purposes. X, and y are the position, the starting point on the screen and width and height are the parameters to set width and height of oval or circle. For circle set same width and height.

Here the program shows code to draw circle on the screen.

package javatutorial.net;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrmaeGraphics extends JPanel{

	public void paint(Graphics g){
		g.drawOval(100, 100, 100, 100);					
	}
	
	public static void main(String[] args){
		JFrame frame= new JFrame("JavaTutorial.net");	
		frame.getContentPane().add(new JFrmaeGraphics());
		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);		
	}
}

Here is the output of this code

circle example

circle example

Draw rectangles

Graphics class provides a method Graphics.drawRect(int x, int y, int width, int height) to draw a rectangle or a square. First two parameters shows starting point and last two parameters shows width and height of rectangle or square. For square width and height should be same.

Here is the code to draw a rectangle

package javatutorial.net;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrmaeGraphics extends JPanel{

	public void paint(Graphics g){
		g.drawRect(10, 10, 100, 100);	
	}
	
	public static void main(String[] args){
		JFrame frame= new JFrame("JavaTutorial.net");	
		frame.getContentPane().add(new JFrmaeGraphics());
		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);		
	}
}	

Here is the output of this code

rectangle example

rectangle example

Draw polygons

Drawing a polygon is very easy  Graphics class provides a method as Graphics.drawPolygon(int [], int [], int points). First parameter is an array containing x values of all point of polygon, second is also an array containing y values of all points of polygon, while the third parameter shows number of points.

package javatutorial.net;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrmaeGraphics extends JPanel{

	public void paint(Graphics g){
	    int xValues[] = {25, 145, 25, 145, 25};
	    int yValues[] = {25, 25, 145, 145, 25};
	    int points = 5;	    
	    g.drawPolygon(xValues, yValues, points);
	}
	
	public static void main(String[] args){
		JFrame frame= new JFrame("JavaTutorial.net");	
		frame.getContentPane().add(new JFrmaeGraphics());
		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);		
	}
}

Here is the output of this code

polygon example

polygon example

Draw text

To draw text on the screen, you can use Graphics.drawText(string text, int x, int y) method. First parameter is the string that you want to display and last two parameters are  the value of point, where this text will start.

Here is the example code

package javatutorial.net;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrmaeGraphics extends JPanel{

	public void paint(Graphics g){
		g.drawString("Hello to JavaTutorial.net", 10, 10);
	}
	
	public static void main(String[] args){
		JFrame frame= new JFrame("JavaTutorial.net");	
		frame.getContentPane().add(new JFrmaeGraphics());
		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);		
	}
}

Here is the output of this code

Text example

Text example

Draw an Image

Graphics class provides Graphics.drawImage(Image, int x, int y, ImageOberver observer) method to draw an image. While Image is the class, you can use getDafaultKit() method to  get the address of image. Place your image in your project’s folder.

Here is the example code

package javatutorial.net;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

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

public class JFrmaeGraphics extends JPanel{

	public void paint(Graphics g){
		 Image image = Toolkit.getDefaultToolkit().getImage("example.jpg");
		    g.drawImage(image, 10, 10, this);
	}
	
	public static void main(String[] args){
		JFrame frame= new JFrame("JavaTutorial.net");	
		frame.getContentPane().add(new JFrmaeGraphics());
		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);		
	}
}

Here is the output of this code

Image example

Image example

Here is the link you can download complete code.

 

 

Share on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Digg thisPin on PinterestShare on RedditShare on StumbleUponPrint this pageEmail this to someone

Leave a Reply

Be the First to Comment!

avatar
wpDiscuz