You are on page 1of 5

CSE 2242: Graphics Lab 01 -Introduction to Graphics in Java

Aims and Objectives


The aims of this lab are as follows:
For the students to get acquainted with the graphics library in Java
draw basic graphic primitives/ objects
use colors to fill objects
get acquainted with positioning of graphical objects in a window.

Required Software
For the purpose of our lab, we are going to use
1. Java either using eclipse or Netbeans.
2. The Java 2D API enhances the graphics, text, and imaging capabilities of the Abstract
Windowing Toolkit (AWT), enabling the development of richer user interfaces and new types
of Java applications.
Along with these richer graphics, font, and image APIs, the Java 2D API supports enhanced
color definition and composition, hit detection on arbitrary geometric shapes and text, and a
uniform rendering model for printers and display devices.
The Java 2D API also enables the creation of advanced graphics libraries, such as CAD-CAM
libraries and graphics or imaging special effects libraries, as well as the creation of image and
graphic file read/write filters.
When used in conjunction with the Java Media Framework and other Java Media APIs, the
Java 2D APIs can be used to create and display animations and other multimedia
presentations. The Java Animation and Java Media Framework APIs rely on the Java 2D API
for rendering support.

Getting Started
The following is a simple programme which will display a circle in a square. Write the programme in
Eclipse and run it.
/*
CircleDraw.java
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package dcircle; // change package name as required.


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
/**
*
* @author user
*/
public class CircleDraw extends Frame {
Shape circle = new Ellipse2D.Float(100.0f, 100.0f, 100.0f, 100.0f);
Shape square = new Rectangle2D.Double(100, 100,100, 100);
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D)g;
ga.draw(circle);
ga.fill(circle);
ga.setPaint(Color.red);
ga.draw(square);
}
}

/*
Main.java
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package dcircle; // change package name as required.

/**
*
* @author user
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

Frame frame = new CircleDraw();


frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
frame.setSize(300, 250);
frame.setVisible(true);
}

Have a look at the following link:


http://www.apl.jhu.edu/~hall/java/Java2D-Tutorial.html
Lab Exercise:
From what you have learnt in the reference file you are to do
the followings:

1. Create the following shapes as in Fig 1.

a. A point,
b. A line ,
c. A circle,
d. A rectangle,
e. A Square ,
f. An Oval,
g. Polygons A Triangle, A pentagon and Hexagon

Figure 1

2. Now you are to fill the shapes with colors


as in Fig 2.

3. Positioning shapes. You are to implement


Fig.3

Figure 3
Figure 2
4. Creating a Face as follows and setting the
window background as blue.

Figure 4
CSE 2242: Graphics Lab 02 Interactive Drawings
Aims and Objectives
The aims of this lab are as follows:
Use of interactive inputs such as mouse clicks and keyboard inputs to implement interactive
applications

Interactive Graphics
Graphical interfaces can be used for input and output. In a GUI environment, users typically interact
with their applications by clicking on buttons,choosing items from menus, and typing information
into on-screen text boxes.

Look for the methods that:


allows you to handle mouse input.
returns the X and Y value of the mouse click
outputs a Text at a particular point
allows keyboard inputs from the graphical window

Practicals

Lab Exercise:
1) You are to create an interactive application which allows you to draw a triangle using mouse
clicks
2) Create an application which does the following

Gives you the following options:

1. Draw circle
2. Draw oval
3. Draw rectangle
4. Draw Square
5. Draw Triangle

Upon entering the choice number, it allows you to create the required shapes using mouse clicks

3) Create an applications which asks you the number of sides for your polygon, and allows you to
draw the polygon using mouse clicks
CSE 2242: Graphics Lab 03 Drawing line graphs, Pie and Bar Charts.
Aims and Objectives
The aim of this lab is to allow the students to learn how to :

(I) Draw lines with different line thickness and styles.


(II) Draw different shapes such as circles and rectangles and filling them with different colors
and patterns.
(III) Draw shapes of varying sizes at different positions based on input data.

Practicals

Lab Exercise:
(i) Line graph. Write a programme to allow the input of data in formats as follows and produce
a line graph as shown below.

Place 6:00 9:00 12:00 15:00 18:00 21:00


Grand-Bay 26 28 32 30 28 27
Pamplemouses 24 26 30 29 27 26
Port-Louis 28 30 34 33 31 29
Quatre-Bornes 22 24 28 27 26 24
Vacoas 20 23 28 27 26 24
Curepipe 18 21 27 27 26 24
Plaisance 26 29 32 32 30 28

(ii) Pie Chart. Write a programme to allow the input of data in the following format and output
a PieChart as shown below.

Cars Sold
BMW 40
Mercedes 50
Honda 120
Toyota 130
Nisan 150

(iii) Bar Chart.Write a programme to allow the input of data in the following format and output a
Bar Chart as shown below.

Cars Sold
BMW 40
Mercedes 50
Honda 120
Toyota 130
Nisan 150

You might also like