![]() ![]() ![]() |
![]() |
import java.awt.*; public class DrawExample4 extends DrawExample3 { public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.clearRect(0, 0, getSize().width, getSize().height); drawFilledCircle(g2d); drawBigString(g2d); drawHollowCircle(g2d); } protected void drawHollowCircle(Graphics2D g2d) { g2d.setPaint(Color.blue); g2d.setStroke(new BasicStroke(8.0F)); g2d.draw(getCircle()); } public static void main(String[] args) { WindowUtil.openInJFrame(new DrawExample4(), 380, 400); } }
import com.sun.java.swing.*; import java.awt.*; import java.awt.geom.*; /** An example of line cap and join styles with * Java2D in Java 1.2. * 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/ */ public class LineStyles extends JPanel { private GeneralPath path; private static float x = 30, deltaX = 150, y = 300, deltaY = 250,thickness = 40; private Circle p1Large, p1Small, p2Large, p2Small,p3Large, p3Small; private int compositeType = AlphaComposite.SRC_OVER; private AlphaComposite transparentComposite = AlphaComposite.getInstance(compositeType, 0.4f); public LineStyles() { path = new GeneralPath(); path.moveTo(x, y); p1Large = new Circle(x, y, thickness/2); p1Small = new Circle(x, y, 2); path.lineTo(x + deltaX, y - deltaY); p2Large = new Circle(x + deltaX, y - deltaY, thickness/2); p2Small = new Circle(x + deltaX, y - deltaY, 2); path.lineTo(x + 2*deltaX, y); p3Large = new Circle(x + 2*deltaX, y, thickness/2); p3Small = new Circle(x + 2*deltaX, y, 2); setForeground(Color.blue); setFont(new Font("SansSerif", Font.BOLD, 20)); } public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.clearRect(0, 0, getSize().width, getSize().height); g2d.setColor(Color.blue); // Default cap and join style BasicStroke stroke = new BasicStroke(thickness, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER); g2d.setStroke(stroke); g2d.draw(path); drawPoints(g2d, "CAP_SQUARE", "JOIN_MITER"); g2d.translate(3*x + 2*deltaX, 0); stroke = new BasicStroke(thickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); g2d.setStroke(stroke); g2d.draw(path); drawPoints(g2d, "CAP_BUTT", "JOIN_BEVEL"); g2d.translate(3*x + 2*deltaX, 0); stroke = new BasicStroke(thickness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2d.setStroke(stroke); g2d.draw(path); drawPoints(g2d, "CAP_ROUND", "JOIN_ROUND"); } private void drawPoints(Graphics2D g2d,String capLabel, String joinLabel) { Paint origPaint = g2d.getPaint(); Composite origComposite = g2d.getComposite(); g2d.setPaint(Color.red); g2d.setComposite(transparentComposite); g2d.fill(p1Large); g2d.fill(p2Large); g2d.fill(p3Large); g2d.setPaint(Color.yellow); g2d.setComposite(origComposite); g2d.fill(p1Small); g2d.fill(p2Small); g2d.fill(p3Small); g2d.setPaint(Color.black); g2d.drawString(capLabel, x + thickness -5, y + 5); g2d.drawString(joinLabel, x + deltaX + thickness - 5, y - deltaY); g2d.setPaint(origPaint); } public static void main(String[] args) { WindowUtil.openInJFrame(new LineStyles(),
(int)(9*x + 6*deltaX), (int)(y + 60)); } } class Circle extends Ellipse2D.Double { public Circle(double centerX, double centerY, double radius) { super(centerX - radius, centerY - radius, 2.0*radius, 2.0*radius); } }
avtor: Filip
Božič
december,
1998