VP Lab3
VP Lab3
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.
java.awt.Color;
java.awt.Graphics;
javax.swing.JFrame;
javax.swing.JPanel;
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.*;
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
Homework:
Write a program that displays a multiplication table in a panel using the drawing
methods, similar to screen below