Java contains two nearly parallel sets of facilities for GUI

Download Report

Transcript Java contains two nearly parallel sets of facilities for GUI

CPCS 391
Computer Graphics
Lab One
Computer Graphics Using Java
What is Computer Graphics:
Computer graphics are graphics created using computers and, more generally,
the representation and manipulation of pictorial by a computer.
data
Java contains two nearly parallel sets of facilities for GUI programming.
(1) Abstract Window Toolkit (AWT): Offers capabilities to control
certain rendering attributes such as drawing color and to draw simple
graphics such as lines, rectangles, and ovals. There is also some support for
images. However, these features are severely limited. For example, there is no
way to control the width of drawing lines.
(2) Swing:
The Java 2 platform brings significant improvements in
graphics capabilities with the introduction of Swing and Java 2D and 3D APIs.
The Swing package is a completely redesigned GUI programming API included
in the Java 2 platform. Java 2D and Java 3D is a very attractive option for
graphics programming and learning computer graphics.

Swing, like the rest of the Java API is subdivided
into packages:
javax.swing, javax.accessibility,
 javax.swing.border …


At the start of your code - always
import javax.swing;
 import javax.swing.event;


Most Swing programs also need
import java.awt.*;
 import java.awt.event.*;

Differences between Swing and AWT
 AWT: Button
 Swing: JButton
Frames: To create a user interface, you need to create either a frame or an
applet to hold the user-interface components.
//Class or File Name: MyFrame.java
import javax.swing.*;
public class MyFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("MyFrame"); // Create a frame
frame.setSize(400, 300); // Set the frame size
frame.setLocationRelativeTo(null); // New since JDK 1.4
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); // Display the frame
}
}
Computer Graphics Using Java 2D and 3D.
To begin drawing in Java, we must first understand Java's coordinate
system.
By default, the upper-left corner of a GUI component (e.g., a window)
has the coordinates (0, 0). A coordinate pair is composed of an xcoordinate (the horizontal coordinate) and a y-coordinate (the vertical
coordinate).
Drawing Lines, Rectangles and Ovals:
This section presents Graphics methods
for drawing lines, rectangles and ovals.
The methods and their parameters are
summarized in next slides. For each
drawing method that requires a width
and height parameter, the width and
height must be nonnegative values.
Graphics methods that draw lines, rectangles and ovals
1. public void drawLine( int x1, int y1, int x2, int y2 ) :
Draws a line between the point (x1, y1) and the point (x2, y2).
2. public void drawRect( int x , int y, int width, int height)
Draws a rectangle of the specified width and height. The top-left corner
of the rectangle has the coordinates (x, y) , rectangle is not filled with
this color.
3. public void fillRect( int x, int y, int width, int height )
Draws a filled rectangle with the specified width and height. The top
left corner of the rectangle has the coordinate (x, y). The rectangle is
filled with the Graphics object's color.
4. public void drawOval( int x, int y, int width, int height )
Draws an oval in the current color with the specified width and
height. The bounding rectangle's top-left corner is at the coordinates
(v, y). The oval touches all four sides of the bounding rectangle at the
center of each side. Only the outline of the shape is drawn.
5. public void fillOval( int x, int y, int width, int height )
Draws a filled oval in the current color with the specified width and height.
The bounding rectangle's top-left corner is at the coordinates (x, y). The
oval touches all four sides of the bounding rectangle at the center of each
side
Oval bounded by a rectangle
// Program to draw different Shapes
// File Name DrawShapes.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class DrawShapes
{
public static void main(String[] args)
{
JFrame frame = new JFrame("MyFrame"); // Create a frame
drawFig dr= new drawFig(); // Creating Object of the Class drawFig
dr.setBackground( Color.WHITE );
frame.add( dr); // Adding to the frame
frame.setSize(400, 300); // Set the frame size
frame.setLocationRelativeTo(null); // New since JDK 1.4
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); // Display the frame
}
}
class drawFig extends JPanel {
@Override
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // call superclass's paint method
g.setColor( Color.BLUE ); // Sect the Color of the figures
g.drawLine( 4, 4, 65, 4 ); // Draw Line
g.drawRect( 5, 40, 90, 55 ); // Draw Rect
g.fillRect( 100, 40, 90, 55 ); // Draw Filled Rect
g.setColor( Color.YELLOW ); // Sect the Color of the figures
g.drawOval( 5, 100, 90, 55 ); // Draw Oval
g.fillOval( 100, 100, 90, 55 ); // Draw Filled Oval
}
}
Output of the program DrawShapes.java
Pixels : the picture elements, or pixels is the smallest unit a
computer can display. Pixels come in various sizes and shapes,
depending upon the type of display that you are using, although from
normal viewing distance they look like dots and alike.
Plotting Points or Pixels: The Java language does not offer any way
to draw points (pixels) but we can achieve it by using the drawLine()
method like this:
drawLine(int x1, int y1, int x1, int y1); That is,
we use the same starting point and the ending point for x1 and y1.
For example to plot a point at the 10 by 15 pixels away from the origin
use:
drawLine(10, 15, 10, 15);
Objectives of the laboratory 1






Recognition Computer Graphics.
Swing.
create a user interface.
Recognition Drawing Lines, Rectangles and
Ovals.
The difference between Draw and fill.
First programmable in Computer Graphics