Reflection in Y axis - Affine transofrmations?

Hi,

I would like to rotate a bezier curve drawn using the cubicCurve method in the y axis to save having to draw another, this is what I have currently in my graphics method: (I have an assignment to draw a face with horns using bezier curves)

publicvoid paint(Graphics g){

Graphics2D g2 = (Graphics2D)g;

// Antialiasing looks much nicer

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

// Transformations for all

AffineTransform R1 = AffineTransform.getRotateInstance(Math.PI);// Rotate so that it isnt upside down

AffineTransform T1 = AffineTransform.getTranslateInstance(300.0,300.0);// Translate the origin to middle of screen

T1.concatenate(R1);// Rotate, then translate

g2.setTransform(T1);

c1.paint(g);// Bottom of face

c2.paint(g);// Left eye

c3.paint(g);// Left bottom bit of horn

c4.paint(g);// Left top bit of horn

c5.paint(g);// Left top of face

// Reflect around the x axis.

// FOUND ON GOOGLE AS Y AXIS REFLECTION BUT CAUSES AN ERROR

//AffineTransform R2 = AffineTransform.setElements(1, 0, 0, -1, 0, 0);

//g2.setTransform(R2);

// What I have: Just rotates atm

AffineTransform R2 = AffineTransform.getRotateInstance(Math.PI, 0, 65);// Transformation to draw right side of the face using left curves

g2.transform(R2);

c2.paint(g);// Right eye

c3.paint(g);// Right bottom bit of horn

c4.paint(g);// Right top bit of horn

c5.paint(g);// Right top of face

}// End of paint method

Many thanks in advance,

Ed

[2422 byte] By [eddieboy665a] at [2007-11-27 1:04:04]
# 1

AffineTransform R2 = AffineTransform.setElements(1, 0, 0, -1, 0, 0);

Java's AffineTransform class doesn't have a setElements method.

To reflect about an axis use a negative scale factor.

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

public class Reflection extends JPanel {

CubicCurve2D.Double curve;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();

int h = getHeight();

if(curve == null) initCurve(w, h);

// Draw ordinate.

g2.draw(new Line2D.Double(w/2, 25, w/2, h-25));

g2.setPaint(Color.blue);

g2.draw(curve);

// Reflect curve about the ordinate.

Rectangle r = curve.getBounds();

double x = 2*r.x;

AffineTransform at = AffineTransform.getTranslateInstance(x,0);

at.scale(-1,1);

g2.setPaint(Color.red);

g2.draw(at.createTransformedShape(curve));

}

private void initCurve(int w, int h) {

double x1 = w/2;

double y1 = h/4;

double ctrlX1 = w*3/4;

double ctrlY1 = h/4;

double ctrlX2 = w*3/4;

double ctrlY2 = h*3/4;

double x2 = w/2;

double y2 = h*3/4;

curve = new CubicCurve2D.Double(x1, y1, ctrlX1, ctrlY1,

ctrlX2, ctrlY2, x2, y2);

}

public static void main(String[] args) {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new Reflection());

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-11 23:39:11 > top of Java-index,Security,Cryptography...