Você está na página 1de 8

CPIT 285

Computer Graphics
Lab One

Aasif Irshad Khan


0535470967 / alig.asif@gmail.com
http://asifikhan.tech.officelive.com/teaching. aspx
Computer Graphics Using Java 2D and 3D.

What is Computer Graphics:


Computer graphics are graphics created using computers and, more
generally, the representation and manipulation of pictorial data by a computer.

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.
Example 1. Following is a simple demonstration of Java 2D graphics features.
It uses certain advanced capabilities of Java 2D such as transparency,
gradient paint, transformation, and font glyphs that are not available in AWT
.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.geom.*;
public class Demo2D extends JApplet
{
public static void main(String s[])
{
JFrame frame = new JFrame();
frame.setTitle("Java 2D Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new Demo2D();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}
public void init()
{
JPanel panel = new Panel2D();
getContentPane().add(panel);
}
}

class Panel2D extends JPanel {


public Panel2D() {
setPreferredSize(new Dimension(500, 400));
setBackground(Color.white);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
// draw an ellipse
Shape ellipse = new Ellipse2D.Double(150, 100, 200, 200);
GradientPaint paint = new GradientPaint(100,100, Color.white, 400, 400, Color.gray);
g2.setPaint(paint);
g2.fill(ellipse);
// set transparency
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f);
g2.setComposite(ac);
g2.setColor(Color.blue);
// draw transparent text
Font font = new Font("Serif", Font.BOLD, 120);
g2.setFont(font);
g2.drawString("Java", 120, 200);
// get outline of text glyph
FontRenderContext frc = g2.getFontRenderContext();
GlyphVector gv = font.createGlyphVector(frc, "2D");
Shape glyph = gv.getOutline(150,300);
// draw rotated glyph
g2.rotate(Math.PI/6, 200, 300);
g2.fill(glyph);
}
}
Output of the program Demo2D.java

1. The class names of Swing components typically have a prefix "J."


2. The class Panel2D extends the JPanel class and overrides the
paintComponent method.
3. The Graphics parameter in the method is cast to Graphics2D to take
advantage of the extended functionality in Java 2D .
4. The shape for the string "2D" is rotated 30 degrees (p/6).
 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
The Java 2D API includes the java.awt.Graphics2D class, which extends the Graphics class
to provide access to the enhanced graphics and rendering features of the Java 2D API.
These features include:

The pen attribute is applied to the outline of a shape.

The fill attribute is applied to a shape's interior. This paint attribute enables you
to fill shapes with solid colors, gradients, and patterns.

The compositing attribute is used when rendered objects overlap existing objects.

The transform attribute is applied during rendering to convert the rendered object
from user space to device-space coordinates. Optional translation, rotation,
scaling, or shearing transforms can also be applied through this attribute.

The clip, type restricts rendering to the area within the outline of the Shape
object used to define the clipping path

The font attribute is used to convert text strings to glyphs.

Você também pode gostar