我创建了一个类,它是一个“镜像”对象。类构造函数具有镜像坐标和方向。这个类中还有一个paintComponent
方法。我正在尝试在框架中使用此类创建一个镜子对象,并自动绘制带有坐标和方向的镜子。有“镜子”类。我可以这样做吗?
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
@SuppressWarnings("serial")
class Mirror extends JComponent{
public static int xm, ym;
public static boolean direction;
public Mirror(int xmm, int ymm, boolean directionm){
xm=xmm;
ym=ymm;
direction=directionm;;
repaint();
}
public int getX(){
return xm;
}
public int getY(){
return ym;
}
public boolean getDirection(){
return direction;
}
public int getIntDirection(){
int a;
if(direction==true){
a=1;
}else{
a=0;
}
return a;
}
public void setDirection(boolean status){
direction=status;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
switch(getIntDirection()){
case 0: ImageIcon mirrorr = new ImageIcon("imagess/mirrorrigt.jpg");
Image mirrorrImage = mirrorr.getImage();
g.drawImage(mirrorrImage,xm,ym,null);
break;
case 1: ImageIcon mirrorl = new ImageIcon("imagess/mirrorleft.jpg");
Image mirrorlImage = mirrorl.getImage();
g.drawImage(mirrorlImage,xm,ym,null);
break;
}
}
}
如图所示here https://stackoverflow.com/a/9373195/230513,您可以通过应用合适的方法来反转相对于轴的渲染AffineTransform
到图形上下文。在这个example https://stackoverflow.com/a/2244285/230513,你可以应用一个scale(1.0, 1.0)
向左和scale(-1.0, 1.0)
向右移动以获得镜面效果。
Box box = new Box(BoxLayout.X_AXIS);
BufferedImage image = ImageIO.read(
new URL("http://sstatic.net/stackoverflow/img/logo.png"));
AffineTransform xfrm1 = new AffineTransform();
xfrm1.scale(1, 1);
box.add(new ImageView(image, xfrm1));
AffineTransform xfrm2 = new AffineTransform();
xfrm2.scale(-1, 1);
box.add(new ImageView(image, xfrm2));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)