75
7 6 G r a d i e n t P a i n t B a s i c S t r o k e s e t P a i n t ( ) s e t S t o r k e ( ) import java.awt.*; import java.awt.geom.*; public class PaintingAndStroking extends ApplicationFrame { public static void main(string[] args) { PaintingAndStroking f = new PaintingAndStroking(); f.settitle("paintingandstroking v1.0"); f.setsize(300, 150); f.center(); f.setvisible(true); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; double x = 15, y = 50, w = 70, h = 70; Ellipse2D e = new Ellipse2D.Double(x, y, w, h); GradientPaint gp = new GradientPaint(75, 75, Color.white, 95, 95, Color.gray, true); // g2.setpaint(gp); g2.fill(e); // e.setframe(x + 100, y, w, h); g2.setpaint(color.black);
7 7 g2.setstroke(new BasicStroke(8)); g2.draw(e); // e.setframe(x + 200, y, w, h); g2.setpaint(gp); g2.draw(e); s e t P a i n t () G r a p h i c s 2 D s e t P a i n t () j a v a. a w t. P a i n t G r a p h i c s 2 D P a i n t G r a p h i c s 2 D P a i n t P a i n t S h a p e G r a p h i c s 2 D f i l l () P a i n t G r a p h i c s 2 D s e t P a i n t () S h a p e f i l l () G r a p h i c s 2 D P a i n t
7 8 j a v a. a w t. C o l o r P a i n t C o l o r C o l o r C o l o r C o l o r public static final Color white; public static final Color lightgray; public static final Color gray; public static final Color darkgray; public static final Color black; public static final Color red; public static final Color pink; public static final Color orange; public static final Color yellow; public static final Color green; public static final Color magenta; public static final Color cyan; public static final Color blue;
7 9 public Color(int r, int g, int b) public Color(float r, float g, float b) import java.awt.*; import java.awt.geom.*; public class SolidPaint extends ApplicationFrame { public static void main(string[] args) { SolidPaint f = new SolidPaint(); f.settitle("solidpaint v1.0"); f.setsize(200, 200); f.center(); f.setvisible(true); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; Arc2D pie = new Arc2D.Float(0, 50, 150, 150, -30, 90, Arc2D.PIE); g2.setpaint(color.blue); g2.fill(pie); G r a p h i c s s e t C o l o r ( ) s e t C o l o r ( ) s e t C o l o r ( c ) G r a p h i c s 2 D s e t P a i n t ( c )
8 0 Swing j a v a x. s w i n g. J C o l o r C h o o s e r public static Color showdialog(component component, String title, Color initialcolor) c o m p o n e n t t i t l e i n i t i a l C o l o r import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RedTown extends JFrame { public static void main(string[] args) { new RedTown(); public RedTown() { super("redtown v1.0"); createui(); setvisible(true); protected void createui() { setsize(400, 400); setlocation(100, 100); getcontentpane().setlayout(new GridBagLayout()); JButton colorbutton = new JButton("Choose a color...");
8 1 getcontentpane().add(colorbutton); colorbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent ae) { Color c = JColorChooser.showDialog( RedTown.this, "Choose a color...", getbackground()); if (c!= null) getcontentpane().setbackground(c); ); addwindowlistener(new WindowAdapter() { public void windowclosing(windowevent we) { System.exit(0); ); G ra d i e n t Pa i n t j a v a. a w t. G r a d i e n t P a i n t P o i n t 2 D C o l o r G r a d i e n t P a i n t
8 2 G r a d i e n t P a i n t G r a d i e n t P a i n t G r a d i e n t P a i n t P o i n t 2 D C o l o r G r a d i e n t P a i n t G r a d i e n t P a i n t
8 3 public GradientPaint(Point2D pt1, Color color1, Point2D pt2, Color color2) public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2) G r a d i e n t P a i n t p t 1 c o l o r 1 p t 2 c o l o r 2 public GradientPaint(Point2D pt1, Color color1, Point2D pt2, Color color2, boolean cyclic) public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2, boolean cyclic) G r a d i e n t P a i n t p t 1 c o l o r 1 p t 2 c o l o r 2 c y c l i c t r u e G r a d i e n t P a i n t G r a d i e n t P a i n t public Point2D getpoint1() public Color getcolor1() public Point2D getpoint2() public Color getcolor2() public boolean iscyclic() import java.awt.*; import java.awt.geom.*; public class GradientPaintFill extends ApplicationFrame { public static void main(string[] args) { GradientPaintFill f = new GradientPaintFill(); f.settitle("gradientpaintfill v1.0"); f.setsize(200, 200); f.center(); f.setvisible(true);
8 4 public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; Ellipse2D e = new Ellipse2D.Float(40, 40, 120, 120); GradientPaint gp = new GradientPaint(75, 75, Color.white, 95, 95, Color.gray, true); g2.setpaint(gp); g2.fill(e); Tex t u r e Pa i n t j a v a. a w t. T e x t u r e P a i n t T e x t u r e P a i n t B u f f e r e d I m a g e B u f f e r e d I m a g e R e c t a n g l e 2 D T e x t u r e P a i n t
8 5 T e x t u r e P a i n t public TexturePaint(BufferImage txtr, Rectangle2D anchor) T e x t u r e d I m a g e anchor T e x t u r e P a i n t public BufferedImage getimage() public Rectangle2D getanchorrect() T e x t u r e P a i n t p a i n t () T e x t u r e P a i n t T e x t u r e P a i n t import java.awt.*; import java.awt.geom.*; import java.awt.image.bufferedimage; import java.io.*; import com.sun.image.codec.jpeg.*; public class TexturePaintFill
8 6 extends ApplicationFrame { public static void main(string[] args) throws Exception { TexturePaintFill f = new TexturePaintFill("roa2.jpg"); f.settitle("texturepaintfill v1.0"); f.setsize(200, 200); f.center(); f.setvisible(true); private BufferedImage mimage; public TexturePaintFill(String filename) throws IOException, ImageFormatException { // JPEG InputStream in = getclass().getresourceasstream(filename); JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in); mimage = decoder.decodeasbufferedimage(); in.close(); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; // RoundRectangle2D r = new RoundRectangle2D.Float(25, 35, 150, 150, 25, 25); // Rectangle2D tr = new Rectangle2D.Double(0, 0, mimage.getwidth(), mimage.getheight()); // TexturePaint TexturePaint tp = new TexturePaint(mImage, tr); // g2.setpaint(tp); g2.fill(r);
8 7 P a i n t P a i n t j a v a. a w t. P a i n t C o n t e x t P a i n t C o n t e x t G r a p h i c s 2 D P a i n t P a i n t C o n t e x t P a i n t C o n t e x t Trans parency P a i n t j a v a. a w t. T r a n s p a r e n c y T r a n s p a r e n c y public static final int OPAQUE public static final int BITMASK public static final int TRANSLUCENT T r a n s p a r e n c y public int gettransparency() O P A Q U E B I T M A S K T R A N S L U C E N T C o l o r M o d e l T r a n s p a r e n c y C o l o r M o d e l
8 8 Paint T r a n s p a r e n c y P a i n t P a i n t P a i n t C o n t e x t public PaintContext createcontext(colormodel cm, Rectangle devicebounds, Rectangle2D userbounds, AffineTransform xform, RenderingHints hints) P a i n t C o n t e x t P a i n t C o n t e x t d e v i c e B o u n d s u s e r B o u n d s A f f i n e T r a n s f o r m h i n t s P a i n t C o n t e x t T e x t u r e P a i n t P a i n t C o n t e x t K E Y _ I N T E R P O L A T I O N P a i n t c r e a t e C o n t e x t () PaintContext c r e a t e C o n t e x t ( ) P a i n t C o n t e x t P a i n t P a i n t C o n t e x t public void dispose() P a i n t C o n t e x t public ColorModel getcolormodel() P a i n t C o n t e x t P a i n t c r e a t e C o n t e x t ( )
8 9 public Raster getraster(int x, int y, int w, int h) P a i n t C o n t e x t R a s t e r R a s t e r P a i n t P a i n t R o u n d G r a d i e n t P a i n t R o u n d G r a d i e n t C o n t e x t R o u n d G r a d i e n t P a i n t c r e a t e C o n t e x t ( ) R o u n d G r a d i e n t C o n t e x t import java.awt.*; import java.awt.geom.*; import java.awt.image.colormodel; public class RoundGradientPaint implements Paint { protected Point2D mpoint; protected Point2D mradius; protected Color mpointcolor, mbackgroundcolor; public RoundGradientPaint(double x, double y, Color pointcolor, Point2D radius, Color backgroundcolor) { if (radius.distance(0, 0) <= 0) throw new IllegalArgumentException("Radius must be greater than 0."); mpoint = new Point2D.Double(x, y); mpointcolor = pointcolor; mradius = radius; mbackgroundcolor = backgroundcolor;
9 0 public PaintContext createcontext(colormodel cm, Rectangle devicebounds, Rectangle2D userbounds, AffineTransform xform, RenderingHints hints) { Point2D transformedpoint = xform.transform(mpoint, null); Point2D transformedradius = xform.deltatransform(mradius, null); return new RoundGradientContext(transformedPoint, mpointcolor, transformedradius, mbackgroundcolor); public int gettransparency() { int a1 = mpointcolor.getalpha(); int a2 = mbackgroundcolor.getalpha(); return (((a1 & a2) == 0xff)? OPAQUE : TRANSLUCENT); R o u n d G r a d i e n t P a i n t T r a n s p a r e n c y g e t T r a n s p a r e n c y () O P A Q U E T R A N S L U C E N T R o u n d G r a d i e n t P a i n t R o u n d G r a d i e n t P a i n t T R A N S L U C E N T R o u n d G r a d i e n t P a i n t new Color(255, 0, 0, 255) new Color(255, 0, 0, 0) R o u n d G r a d i e n t P a i n t g e t R a s t e r ( ) for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { double distance = mpoint.distance(x + i, y + j); double radius = mradius.distance(0, 0); double ratio = distance / radius; if (ratio > 1.0) ratio = 1.0;
9 1 data[base + 0] = (int)(mc1.getred() + ratio * (mc2.getred() - mc1.getred())); data[base + 1] = (int)(mc1.getgreen() + ratio * (mc2.getgreen() - mc1.getgreen())); data[base + 2] = (int)(mc1.getblue() + ratio * (mc2.getblue() - mc1.getblue())); data[base + 3] = (int)(mc1.getalpha() + ratio * (mc2.getalpha() - mc1.getalpha())); import java.awt.*; import java.awt.geom.*; import java.awt.image.*; public class RoundGradientContext implements PaintContext { protected Point2D mpoint; protected Point2D mradius; protected Color mc1, mc2; public RoundGradientContext(Point2D p, Color c1, Point2D r, Color c2) { mpoint = p; mc1 = c1; mradius = r; mc2 = c2; public void dispose() { public ColorModel getcolormodel() { return ColorModel.getRGBdefault(); public Raster getraster(int x, int y, int w, int h) { WritableRaster raster = getcolormodel().createcompatiblewritableraster(w, h); int[] data = new int[w * h * 4]; for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { double distance = mpoint.distance(x + i, y + j); double radius = mradius.distance(0, 0); double ratio = distance / radius;
9 2 if (ratio > 1.0) ratio = 1.0; int base = (j * w + i) * 4; data[base + 0] = (int)(mc1.getred() + ratio * (mc2.getred() - mc1.getred())); data[base + 1] = (int)(mc1.getgreen() + ratio * (mc2.getgreen() - mc1.getgreen())); data[base + 2] = (int)(mc1.getblue() + ratio * (mc2.getblue() - mc1.getblue())); data[base + 3] = (int)(mc1.getalpha() + ratio * (mc2.getalpha() - mc1.getalpha())); raster.setpixels(0, 0, w, h, data); return raster; R o u n d G r a d i e n t P a i n t import java.awt.*; import java.awt.geom.*; public class RoundGradientPaintFill extends ApplicationFrame { public static void main(string[] args) { RoundGradientPaintFill f = new RoundGradientPaintFill(); f.settitle("roundgradientpaintfill v1.0"); f.setsize(200, 200); f.center(); f.setvisible(true); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; RoundRectangle2D r = new RoundRectangle2D.Float(25, 35, 150, 150, 25, 25); RoundGradientPaint rgp = new RoundGradientPaint(75, 75, Color.magenta, new Point2D.Double(0, 85), Color.blue);
9 3 g2.setpaint(rgp); g2.fill(r); s e t S t r o k e () G r a p h i c s 2 D j a v a. a w t. S t r o k e j a v a. a w t. B a s i c S t r o k e s e t P a i n t () G r a p h i c s 2 D P a i n t G r a p h i c s 2 D d r a w () G r a p h i c s 2 D S t r o k e P a i n t G r a p h i c s 2 D S t r o k e G r a p h i c s 2 D d r a w () G r a p h i c s 2 D S t r o k e S t r o k e S h a p e public abstract Shape createstrokedshape(shape p) S h a p e
9 4 S t r o k e d r a w ( ) G r a p h i c s 2 D S h a p e S h a p e S h a p e S h a p e S h a p e S h a p e G r a p h i c s 2 D d r a w () public void longwindeddraw(graphics2d g2, Shape s) { Stroke stroke = g2.getstroke(); Shape strokedoutline = stroke.createstrokedshape(s); g2.fill(strokedoutline); B a s i c S t r o ke S t r o k e j a v a. a w t. B a s i c S t r o k e B a s i c S t r o k e B a s i c S t r o k e
9 5 public static final int CAP_BUTT public static final int CAP_ROUND public static final int CAP_SQUARE B a s i c S t r o k e B a s i c S t r o k e
9 6 public static final int JOIN_BEVEL public static final int JOIN_MITER J O I N _ B E V E L public static final int JOIN_ROUND C A P _ R O U N D J O I N _ M I T E R halflength miterlength miterlimit m i t e r L e n g t h m i t e r L i m i t h a l f W i d t h J O I N _ B E V E L J O I N _ M I T T E R m i t e r L i m i t m i t e r l i m i t m i t e r l i m i t B a s i c S t r o k e h a l f L e n g t h m i t e r L e n g t h J O I N _ M I T E R
9 7 B a s i c S t r o k e f l o a t G e n e r a l P a t h C A P _ B U T T C A P _ R O U N D C A P _ S Q U A R E
9 8 B a s i c S t r o k e P a i n t S t r o k e public BasicStroke() S t r o k e C A P _ S Q U A R E J O I N _ M I T E R
9 9 public BasicStroke(float width) public BasicStroke(float width, int cap, int join) S t r o k e J O I N _ M I T E R public BasicStroke(float width, int cap, int join, float miterlimit) J O I N _ M I T E R public BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase) S t r o k e Rectangle2D r = new Rectangle2D.Double(50, 50, 100, 100); Stroke stroke = new BasicStroke(8, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 12, 12, 0); g2.setstroke(stroke); g2.draw(r);
1 0 0 o v e r l a p Color smokey = new Color(128, 128, 128, 128); g2.setpaint(smokey); g2.fill(e); g2.draw(e); g2.setpaint(color.black); g2.draw(e); g2.setpaint(color.gray); g2.fill(e);
1 0 1 g2.setpaint(color.gray); g2.fill(e); g2.setpaint(color.black); g2.draw(e); import java.awt.*; import java.awt.geom.*; public class Overlap extends ApplicationFrame { public static void main(string[] args) { Overlap f = new Overlap(); f.settitle("overlap v1.0"); f.setsize(300, 150); f.center(); f.setvisible(true); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; double x = 15, y = 50, w = 70, h = 70; Ellipse2D e = new Ellipse2D.Double(x, y, w, h);
1 0 2 g2.setstroke(new BasicStroke(8)); // Color smokey = new Color(128, 128, 128, 128); g2.setpaint(smokey); g2.fill(e); g2.draw(e); // e.setframe(x + 100, y, w, h); g2.setpaint(color.black); g2.draw(e); g2.setpaint(color.gray); g2.fill(e); // e.setframe(x + 200, y, w, h); g2.setpaint(color.gray); g2.fill(e); g2.setpaint(color.black); g2.draw(e);