* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Answers
Stereoscopy wikipedia , lookup
Free and open-source graphics device driver wikipedia , lookup
General-purpose computing on graphics processing units wikipedia , lookup
Spatial anti-aliasing wikipedia , lookup
Anaglyph 3D wikipedia , lookup
Rendering (computer graphics) wikipedia , lookup
Image editing wikipedia , lookup
Hold-And-Modify wikipedia , lookup
Stereo display wikipedia , lookup
Indexed color wikipedia , lookup
Color Graphics Adapter wikipedia , lookup
Waveform graphics wikipedia , lookup
Graphics processing unit wikipedia , lookup
Framebuffer wikipedia , lookup
Tektronix 4010 wikipedia , lookup
BSAVE (bitmap format) wikipedia , lookup
Advanced Programming Quiz II
Answers
04.05.2004
1) …………………………….
public void quiz2paint1 (Graphics g) //draw shapes using Java 2D API
{
clear (g);
Graphics2D g2d = (Graphics2D)g;
// This is gradient fills example drawing 2D ellipse
//Red at (0,0) and white at (150,150) changes gradually
//True means to repeat the pattern
GradientPaint gradient = new GradientPaint(0, 0, Color.red, 150, 150, Color.white, true);
Rectangle2D.Double rectangle = new Rectangle2D.Double(50, 20, 85,120);
g2d.setPaint (gradient);
g2d.fill(rectangle);
}
2) …………………………….
public void quiz2paint2 (Graphics g)
{
//draw shapes using Java 2D API
clear (g);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(Color.blue);
g2d.SetStroke (new BasicStroke (10));
g2d.draw (new Rectangle2D.Double(50, 20, 85,120);
}
For example:
g2d.setStroke (new BasicStroke( 10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND) ;
code line explains that: CAP_ROUND is the end-cap style specifies which round ends are
used to render the ends of line segments.
JOIN_ROUND is the join style and specifies that joining of the lines will be rounded.
3.)
BufferedImage: class
GradientPaint : class
setStroke: method
Elipse2D.Double: class
BufferedImageOp: interface
drawRect: method
ColvolveOp: class
Color: class
fillRect : method
setColor: method
Stroke: interface
4.) i)
Class Graphics as an abstract class performs drawing on the screen as Graphics
objects.
java.awt .Graphics
This object contains various methods for drawing, font manipulation, color manipulation etc.
A Graphics subclass implements all drawing capabilities for each platform.
Thus, graphics class enables us to write programs in an independent platform.
A few methods which use Graphics() constructor:
drawLine (int x1, int y1, int x2, int y2);
fillRect (int, int, int, int) ;
clearRect (int, int, int, int);
setColor (Lolor c);
drawPolygon (Polygon p);
ii)
Java 2D API includes features for processing shapes, text, and images in its
packages and Java2D also render these three types of shapes which are called graphics
primitivies.
Briefly: class java.awt.Graphics2D enables drawing with the Java 2D API, and also class
Graphics2D is a subclass of Graphics.
There are seven Graphics2D state attributes: Paint, Stroke, Font, Rendering hint, Transform,
Composite, Clip. These attributes determine how graphics primitives are rendered.
5.) ……………
public void quiz2paint3 (Graphics g)
{
clear (g);
Graphics2D g2d = (Graphics2D)g;
Rectangle2D.Double rect;
Area rectA, rectB, rectC, rectD;
rect = new Rectangle2D.Double( );
rectA = new Area (rect);
rectB = new Area (rect);
rectC = new Area (rect);
rectD = new Area (rect);
rectA.intersect (rectB);
rectC.subtract (recD);
g2d.setcolor(Color.yellow);
g2d.fill (rectA);
g2d.fill(rectC);
}
6.) import java.awt.image.*;
BufferedImage grid; //declare the image
……………………
public void quiz2paint4 (Graphics g) {
super.quiz2paint4 (g); //call superclass’ paint method as background
Graphics2D g2d = (Graphics2D)g; //get graphics2D by casting g to Graphics2D
if (grid= = null) { // compute the grid only one time
int w = this.getWidht();
int h = this.getHeight();
grid = (BufferedImage ) (this.createImage(w,h));
Graphics2D gc= grid.createGraphics();
for (int x= 0; x<w; x+=10) { gc.drawLine (x,0,x,h); }
for (int y= 0; x<h; y+=10) { gc.drawLine (0,y,w,y); }
} //draw the grid from precompute image
g2d.drawImage (grid, null, 0,0);
We create a new BufferedImage. The java.awt.image.BufferedImage class is used to
create images in memory. We might want to do this for unchanging images that take a long
time to create. We can create the images once, then display them repeatedly, instead of
recomputing them each time.
Assume we want to make the buffered image as the same size with the panel we are drawing.
This buffered image has a grid on it, which will be used as the background for all other
drawing. We only have to compute the grid one time.
ii) Graphics2D gd =myImage.createGraphics();
gd.drawOval (10, 10, 40,100);