使用paintComponent在JFrame中镜像对象

0 java swing paintcomponent imageicon

我创建了一个类,它是一个“镜像”对象。类构造函数具有镜像坐标和方向。在这个类中也有一个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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

tra*_*god 5

如所示在这里,可以通过施加合适的反转绘制相对于轴线AffineTransform的图形上下文。在本例中,您可以scale(1.0, 1.0)向左和scale(-1.0, 1.0)向右应用 a以获得镜像效果。

图片

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));
Run Code Online (Sandbox Code Playgroud)