Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

AffineTransform problems

807603Dec 29 2007 — edited Jan 5 2008
I want to essentially to scale and rotate a picure of the full moon. I want to do it several times and each time start from a standard picture. I import the picture as an ImageIcon from resource, convert it to an Image and finally make a copy to a BufferedImage in order to be able to apply the different tranforms. The code below performs flawlessly on IMac when compiled under XCode. However on a PC the resulting picture is fragmented, consisting of several smaller moon pictures within each other. Any suggestions?? I am using Java 1.5.

....
class Crescent extends JPanel {
....
private ImageIcon im = new ImageIcon(this.getClass().getResource("TheMoonW.jpg"));
//********************************************************************
public Crescent() {
}

/*******************************************************************/
public void setParam(......) {
...
}//SetParam

/*******************************************************************/
public static BufferedImage toBufferedImage(Image image) {
int width = image.getWidth(null), height = image.getHeight(null);
BufferedImage image1 = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image1.createGraphics();
g.drawImage(image,0,0,null);
g.dispose();
return image1;
}
********************************************************************/
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
image0 = im.getImage();
BufferedImage image = toBufferedImage(image0);
int width = image.getWidth(), height = image.getHeight();
g2d = image.createGraphics();

//Create shadow shape
radius = 143;
radius2 = (int)(143*abs(k));
Rectangle rightRect = new Rectangle(width/2, 0, width, height);
Area rr = new Area(rightRect);
Rectangle leftRect = new Rectangle(0, 0, width/2, height);
Area lr = new Area(leftRect);
Ellipse2D.Double circle = new Ellipse2D.Double(width/2-radius, height/2-radius, 2*radius, 2*radius);
Area c = new Area(circle);
Ellipse2D.Double oval = new Ellipse2D.Double(width/2-radius2, height/2-radius, 2*radius2, 2*radius);
Area ov = new Area(oval);
if (firstQuadrant) c.intersect(rr); //Get the right half-circle
else c.intersect(lr);
if (k < 0) c.add(ov); //Add or subtract half ellipse
else c.subtract(ov);

AffineTransform rotKsi = new AffineTransform();
//Set the rotation angle of the shadow
rotKsi.setToRotation(toRadians(-ksi), width/2, width/2);
c.transform(rotKsi);
g2d.setColor(shadow); //Set shadow color, black transparent...
g2d.fill(c); //and draw the shadow

AffineTransform rotation = new AffineTransform();
//Set rotation of the moon+shadow
rotation.setToRotation(toRadians(q), width/2, height/2);
rotation.scale(scaleFac,scaleFac);
//Concat with the scaling
rotation.translate(width*fac3,height*fac3);
//Translate result back to center
g2d.drawImage(image, rotation, null);
//Set up the clearing area
Ellipse2D.Double ellipse = new Ellipse2D.Double(width/2- radius*scaleFac, height/2-radius*scaleFac, radius*scaleFac*2,radius*scaleFac*2);
Area a2 = new Area(ellipse);//Start with a circle
Rectangle rect = new Rectangle(width, height);
Area a1 = new Area(rect);//Then a a rect
a1.subtract(a2); //Make a hole in the rect
g2d.setColor(Color.black);
g2d.fill(a1); //Fill the outer region
g.drawImage(image, -75, -65, this);
}//paintComponent
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 2 2008
Added on Dec 29 2007
8 comments
259 views