0% found this document useful (0 votes)
59 views8 pages

VP Lab3

The document discusses Java layout managers and the Graphics class for drawing graphics. It provides examples of using different layout managers like FlowLayout and BorderLayout. It also demonstrates how to draw various shapes, lines and text by overriding the paintComponent method in a JPanel subclass and using the Graphics class methods. Exercises include drawing shapes like concentric circles, a chessboard and a snowman. The homework is to write a program that displays a multiplication table using graphics drawing methods.

Uploaded by

yaheaalkatalen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views8 pages

VP Lab3

The document discusses Java layout managers and the Graphics class for drawing graphics. It provides examples of using different layout managers like FlowLayout and BorderLayout. It also demonstrates how to draw various shapes, lines and text by overriding the paintComponent method in a JPanel subclass and using the Graphics class methods. Exercises include drawing shapes like concentric circles, a chessboard and a snowman. The homework is to write a program that displays a multiplication table using graphics drawing methods.

Uploaded by

yaheaalkatalen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab (3) Visual Programming

LayoutManager and Graphics


Objectives:

To review about Java Layout Manager and concept of JPanel

To be able to design and build useful Graphical User Interfaces

To learn the java.awt.Graphics class.

To learn how to draw personalized graphics on a panel by overriding the


paintComponent method.

Tasks:
Task 1: JPanel
A JPanel is an empty area that can be used either to hold other components or to
draw graphics on. Thedefault layout for a JPanel is FlowLayout.
Example 1: Using FlowLayout
Create a new project called Lab3-Example1. Add the following class to the project.
Compile and run the program.
import javax.swing.*;
import java.awt.*;
public class ConverterFrame extends JFrame {
private JLabel prompt = new JLabel("Distance in miles: ");
private JTextField input = new JTextField(6);
private JTextArea display = new JTextArea(10,20);
private JButton convert = new JButton("Convert!");
public ConverterFrame() {
super("Distance Converter");
setLayout(new FlowLayout());
add(prompt);
add(input);
add(convert);
add(display);
}
public static void main(String args[]) {
ConverterFrame cf = new ConverterFrame();
cf.setSize(400, 300);

cf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cf.setVisible(true);
}
}
Example 2: Using a combination of layouts
Create a new project called Lab3-Example2. Add the following class to the project.
Compile and run the program
import javax.swing.*;
import java.awt.*;
public class ConverterFrame extends JFrame {
private JLabel prompt = new JLabel("Distance in miles: ");
private JTextField input = new JTextField(6);
private JTextArea display = new JTextArea(10,20);
private JButton convert = new JButton("Convert!");
private JPanel p1 = new JPanel(new FlowLayout());
public ConverterFrame() {
super("Distance Converter");
setLayout(new BorderLayout());
p1.add(
prompt);
p1.add(input);
p1.add(convert);
p1.setBackground(new Color(0,150,200));
add(p1, BorderLayout.NORTH);
add(display, BorderLayout.CENTER);
}
public static void main(String args[]) {
ConverterFrame f = new ConverterFrame();
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Exercise 1
Create a new project called Lab3-Exercise1. Write a program that generates an
output similar to the following screen.

Hint : Panel Border:


panel.setBorder(new TitledBorder("KeyPad"));
Task-2 : The Graphics class
The Graphics class has methods that can be used to draw geometric shapes in
different colors. These methods can be used to create graphical user interfaces that
are more interesting or to give a visual representation of data.
To draw graphics on JPanel it is mandatory to:
Create a new class that extends JPanel and override a paintComponent
method in it.
Create a new Graphics object and add it to the extended panel.
Example 3: Drawing graphics
Create a new project called Lab3-Example3 .Add the following class to the project.
Then, compile and run the program. Study the source code and the output to learn
the important methods for draw in g graphics.
import
import
import
import

java.awt.Color;
java.awt.Graphics;
javax.swing.JFrame;
javax.swing.JPanel;

public class MyGraphicPanel extends JPanel {


private int []x1 = {300, 340, 390, 350};
private int []y1 = {50, 50, 100, 100};
public void paintComponent(Graphics g) { // override this method
super.paint( g );
// call this method of the superclass
setBackground(new Color(0, 255, 255));

g.drawString("Blank Shapes",20,40);
g.setColor(Color.blue);
g.drawLine(20,50,70,90);
g.setColor(Color.red);
g.drawRect(100,50,32,55);
g.setColor(Color.orange);
g.drawOval(150,46,60,60);
g.setColor(Color.magenta);
g.drawArc(230,50,65,50,30,270);
g.setColor(Color.blue);
g.fillPolygon(x1, y1, x1.length);
}
}
class MyGraphicFrame {
public static void main(String args[]) {
JFrame f = new JFrame();
f.add(new MyGraphicPanel());
f.setTitle("Drawing Graphics");
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Exercise 2: Create a new project called Lab3-Exercise2. Write a program that
draws a series of eight concentric circles. The circles should be separated by 10
pixels as in screen below. Use the drawOval method of class Graphics.

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

public class Concentric extends JFrame


{
public Concentric()
{
super( "Concentric" );
setSize( 300, 300 );
show();
}
public void paint( Graphics g )
{
for ( int x = 0; x <= 160; x += 10 )
{
int y = 160 - ( x * 2 );
g.drawOval( x + 30, x + 30, y, y );
}
}
Exercise 3: Write a program in a new project called Lab3-Exercise3 that draws a
chess board similar to the following screen

package chessboard;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Chessboard extends JPanel {
public void paintComponent (Graphics g)
{
g.fillRect(100,100,400,400);
for(int i=100; i<=400; i+=100)

{
for(int j=100; j<=400;j+=100)
g.clearRect(i,j,50,50);
}
for(int i=150; i<=450; i+=100)
{
for(int j=150; j<=450;j+=100)
g.clearRect(i,j,50,50);
}
}
public static void main(String[] args) {
JFrame f=new JFrame();
f.add(new Chessboard());
f.setVisible(true);
}
Exercise 4: Write a program in a new project called Lab3-Exercise4 that draws a
snowman similar to the following screen

package snowman;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Snowman extends JPanel{
public void paint (Graphics frame)
{
// set up some constants
final int MID = 150;
// middle of the snowman
final int TOP = 50;
// top of the snowman

// need to set the background colors


setBackground (Color.cyan);
// color the ground
frame.setColor (Color.blue);
// the ground is a blue rectangle
frame.fillRect (1, 175, 300, 50) ;
// Can you color in a sun????
// draw three large snowballs to make up snowman
frame.setColor (Color.white);
// draw head
frame.fillOval (MID - 20, TOP, 40, 40);
// draw middle (upper torso)
frame.fillOval (MID - 35, TOP + 35, 70, 50);
// draw base (lower torso)
frame.fillOval (MID - 50, TOP + 80, 100, 60);
// draw in features of snowman
frame.setColor (Color.black);
// draw eyes
// draw left eye
frame.fillOval (MID - 10, TOP + 10, 5, 5);
// draw right eye
frame.fillOval (MID + 5, TOP + 10, 5, 5);
// draw mouth
frame.drawArc (MID - 10, TOP + 20, 20, 10, 190, 160);
// draw arms
// draw left arm
frame.drawLine (MID - 25, TOP + 60, MID - 50, TOP + 40);
// draw right arm
frame.drawLine (MID + 25, TOP + 60, MID + 55, TOP + 60);
// draw hat
// draw brim of hat
frame.drawLine (MID - 20, TOP + 5, MID + 20, TOP + 5);
// draw top of hat
frame.fillRect (MID - 15, TOP - 20, 30, 25);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {

JFrame f = new JFrame();


f.add(new Snowman());
f.setTitle("Drawing Graphics");
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

Homework:
Write a program that displays a multiplication table in a panel using the drawing
methods, similar to screen below

You might also like