Results 1 to 1 of 1
Thread: GeneralPath demo
-
GeneralPath demo
Java Code:import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.GeneralPath; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; public class GeneralPathDemo extends JPanel { int x, y, x1, y1, x2, y2; GeneralPath oddShape; BufferedImage bi; Graphics2D big; boolean firstTime; Rectangle area; public GeneralPathDemo() { oddShape = new GeneralPath(); firstTime = true; area = new Rectangle(); } public GeneralPath createPath() { oddShape.moveTo(20, 30); oddShape.lineTo(30, 40); oddShape.lineTo(50, 10); oddShape.lineTo(70, 20); oddShape.curveTo(10, 90, 100, 50, 34, 99); return oddShape; } public void paintComponent(Graphics g) { // super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; if (firstTime) { Dimension dim = getSize(); int w = dim.width; int h = dim.height; oddShape = createPath(); area = new Rectangle(w, h); bi = (BufferedImage) createImage(w, h); big = bi.createGraphics(); big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); firstTime = false; } // Clears the shape that was previously drawn. big.setColor(Color.white); big.fillRect(0, 0, area.width, area.height); big.setColor(Color.magenta); big.setStroke(new BasicStroke(3.0f)); big.draw(oddShape); // Draws the buffered image to the screen. g2.drawImage(bi, 0, 0, this); } public static void main(String s[]) { JFrame f = new JFrame("Odd Shape"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.getContentPane().add(new GeneralPathDemo()); f.setSize(new Dimension(350, 200)); f.setVisible(true); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
How to obtain a filled and stroked GeneralPath
By Java Tip in forum java.awtReplies: 0Last Post: 06-25-2008, 10:37 AM -
How to use GeneralPath class
By Java Tip in forum java.awtReplies: 0Last Post: 06-25-2008, 10:37 AM -
Transform Demo
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:52 PM -
Another GradientPaint Demo
By Java Tip in forum java.awtReplies: 0Last Post: 06-21-2008, 08:48 PM -
If Else Demo
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks