Soyez le premier à donner votre avis sur cette source.
Snippet vu 4 957 fois - Téléchargée 10 fois
draw(Graphics g, double x, double y)qui le dessine au bon endroit.
package ccm.kx.swing; import java.awt.Color; import java.awt.Graphics2D; public class Fish { private double bodyLength, bodyWidth, bodyAngle, tailWidth, tailLength, tailAngle; private Color bodyColor, tailColor; public Fish(double bodyLength, double bodyWidth, double bodyAngle, double tailWidth, double tailLength, double tailAngle, Color bodyColor, Color tailColor) { this.bodyLength = bodyLength; this.bodyWidth = bodyWidth; this.bodyAngle = bodyAngle; this.tailWidth = tailWidth; this.tailLength = tailLength; this.tailAngle = tailAngle; this.bodyColor = bodyColor; this.tailColor = tailColor; } public Fish(double bodyLength) { this(bodyLength, bodyLength / 3, 0, bodyLength / 4, bodyLength / 4, 0, null, null); } private static int round(double d) { return (int) Math.round(d); } private static void fillTriangle(Graphics2D g, double x1, double y1, double x2, double y2, double x3, double y3) { int[] xPoints = { round(x1), round(x2), round(x3) }; int[] yPoints = { round(y1), round(y2), round(y3) }; g.fillPolygon(xPoints, yPoints, 3); } public void draw(Graphics2D g, double x, double y) { // init graphics Color previousColor = g.getColor(); g.translate(x, y); // draw body g.rotate(Math.toRadians(-bodyAngle)); g.translate(-bodyLength / 2, -bodyWidth / 2); if (bodyColor != null) g.setColor(bodyColor); g.fillOval(0, 0, round(bodyLength), round(bodyWidth)); // draw tail g.rotate(Math.toRadians(tailAngle), 0, bodyWidth / 2); if (tailColor != null) g.setColor(tailColor); fillTriangle(g, 0, bodyWidth / 2, -tailLength, bodyWidth / 2 - tailWidth / 2, -tailLength, bodyWidth / 2 + tailWidth / 2); // reset graphics g.rotate(Math.toRadians(-tailAngle), 0, bodyWidth / 2); g.translate(bodyLength / 2, bodyWidth / 2); g.rotate(Math.toRadians(bodyAngle)); g.translate(-x, -y); g.setColor(previousColor); } public double getBodyLength() { return bodyLength; } public void setBodyLength(double bodyLength) { this.bodyLength = bodyLength; } public double getBodyWidth() { return bodyWidth; } public void setBodyWidth(double bodyWidth) { this.bodyWidth = bodyWidth; } public double getBodyAngle() { return bodyAngle; } public void setBodyAngle(double bodyAngle) { this.bodyAngle = bodyAngle; } public double getTailWidth() { return tailWidth; } public void setTailWidth(double tailWidth) { this.tailWidth = tailWidth; } public double getTailLength() { return tailLength; } public void setTailLength(double tailLength) { this.tailLength = tailLength; } public double getTailAngle() { return tailAngle; } public void setTailAngle(double tailAngle) { this.tailAngle = tailAngle; } public Color getBodyColor() { return bodyColor; } public void setBodyColor(Color bodyColor) { this.bodyColor = bodyColor; } public Color getTailColor() { return tailColor; } public void setTailColor(Color tailColor) { this.tailColor = tailColor; } }
package ccm.kx.forum; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import ccm.kx.swing.Fish; public class UserGuide { public static void drawTestFish(Fish fish, Graphics g, int x, int y, String label1, String label2) { g.setColor(Color.BLACK); g.translate(x, y); g.drawLine(-50, 0, 50, 0); g.drawLine(0, 50, 0, -50); fish.draw((Graphics2D) g, 0, 0); if (label1 != null) g.drawString(label1, 3, -40); if (label2 != null) g.drawString(label2, 3, 50); g.translate(-x, -y); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setContentPane(new Container() { private static final long serialVersionUID = 1L; @Override public void paint(Graphics g) { super.paint(g); // row 1 : constructor and color Fish f = new Fish(50); drawTestFish(f, g, 50, 50, "new Fish(50)", null); f = new Fish(75); drawTestFish(f, g, 200, 50, "new Fish(75)", null); f = new Fish(50); f.setBodyColor(Color.RED); drawTestFish(f, g, 350, 50, "new Fish(50)", "setBodyColor(RED)"); f = new Fish(50); f.setTailColor(Color.BLUE); drawTestFish(f, g, 500, 50, "new Fish(50)", "setTailColor(BLUE)"); // row 2 : body f = new Fish(50); f.setBodyLength(75); drawTestFish(f, g, 50, 200, "new Fish(50)", "setBodyLength(75)"); f = new Fish(50); f.setBodyWidth(25); drawTestFish(f, g, 200, 200, "new Fish(50)", "setBodyWidth(25)"); f = new Fish(50); f.setBodyAngle(45); drawTestFish(f, g, 350, 200, "new Fish(50)", "setBodyAngle(45)"); f = new Fish(50); f.setBodyAngle(180); drawTestFish(f, g, 500, 200, "new Fish(50)", "setBodyAngle(180)"); // row 3 : tail f = new Fish(50); f.setTailLength(30); drawTestFish(f, g, 50, 350, "new Fish(50)", "setTailLength(30)"); f = new Fish(50); f.setTailWidth(20); drawTestFish(f, g, 200, 350, "new Fish(50)", "setTailWidth(20)"); f = new Fish(50); f.setTailAngle(20); drawTestFish(f, g, 350, 350, "new Fish(50)", "setTailAngle(20)"); f = new Fish(50); f.setTailAngle(-30); drawTestFish(f, g, 500, 350, "new Fish(50)", "setTailAngle(-30)"); } }); frame.setSize(625, 445); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
package ccm.kx.forum; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import ccm.kx.swing.Fish; public class FindingNemo { public static void main(String[] args) { Fish marlin = new Fish(150, 100, -25, 100, 75, -25, Color.RED, Color.RED); Fish dory = new Fish(200, 155, 200, 50, 50, -10, Color.BLUE, Color.YELLOW); JFrame frame = new JFrame(); frame.setContentPane(new Container() { private static final long serialVersionUID = 1L; @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.CYAN); g.fillRect(0, 0, getWidth(), getHeight()); marlin.draw((Graphics2D) g, 200, 200); dory.draw((Graphics2D) g, 400, 200); } }); frame.setSize(600, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.