Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中绘制缩放图像的顶部?_Java_Image_Swing_Scale_Mouselistener - Fatal编程技术网

如何在java中绘制缩放图像的顶部?

如何在java中绘制缩放图像的顶部?,java,image,swing,scale,mouselistener,Java,Image,Swing,Scale,Mouselistener,我有一个添加到JPanel的buffereImage。 我能够使用仿射变换放大/缩小图像。 我可以写在图片的顶部,但不能写在我想要的位置上。 我绘制的线显示在图像上的错误位置 我想这与规模有关,但我不知道我的代码哪部分是错的 这是我的意思的截图。 这是我的密码: public class MouseScaleTest { int xbegin = 0; int ybegin = 0; int xend = 0; int

我有一个添加到
JPanel
buffereImage
。 我能够使用仿射变换放大/缩小图像。 我可以写在图片的顶部,但不能写在我想要的位置上。 我绘制的线显示在图像上的错误位置

我想这与规模有关,但我不知道我的代码哪部分是错的

这是我的意思的截图。

这是我的密码:


    public class MouseScaleTest {

        int xbegin = 0;
        int ybegin = 0;
        int xend = 0;
        int yend = 0;

        boolean isNewLine = true;

        int initx;
        int inity;

        int count = 0;
        Line2D linebuffer;
        Rectangle2D box;
        final ArrayList<Line2D> lineContainer = new ArrayList();
        final ArrayList<Rectangle2D> boxContainer = new ArrayList();

        public static void main(String[] args) {
            new MouseScaleTest();
        }

        public MouseScaleTest() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());

    //                JScrollPane
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }

        public class TestPane extends JPanel {

            private BufferedImage img;

            private float scale = 1f;
            private float scaleDelta = 0.05f;
            JLabel lbl;
            JLabel scalePoint;
            JLabel begin;
            JLabel end;
            JLabel aft;

            public TestPane() {

                try {
                    img = ImageIO.read(new File("C:\\Users\\John Ebarita\\Documents\\report.jpg"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                lbl = new JLabel();
                lbl.setBounds(0, 0, 50, 10);
                scalePoint = new JLabel();
                scalePoint.setBounds(50, 0, 50, 10);
                begin = new JLabel();
                begin.setBounds(150, 0, 50, 10);
                end = new JLabel();
                end.setBounds(200, 0, 50, 10);
                aft = new JLabel();
                aft.setBounds(250, 0, 50, 10);

                add(lbl);
                add(scalePoint);
                add(begin);
                add(end);
                add(aft);

                addMouseWheelListener(new MouseWheelListener() {

                    @Override
                    public void mouseWheelMoved(MouseWheelEvent e) {
                        int rotation = e.getWheelRotation();
                        if (rotation < 0) {
                            scale -= scaleDelta;
                        } else {
                            scale += scaleDelta;
                        }
                        if (scale < 0) {
                            scale = 0;
                        }
    //                    else if (scale > 1.5) {
    //                        scale = 1;
    //                    }
                        scalePoint.setText("Scale: " + String.valueOf(scale));

                        repaint();

    //                    System.out.println(getSize());
                    }
                });

                addMouseMotionListener(new MouseMotionListener() {
                    @Override
                    public void mouseDragged(MouseEvent e) {
                        if (isNewLine == false) {
                            xend = e.getX();
                            yend = e.getY();

                            end.setText("End Values : " + xend + " " + yend);
                            repaint();
                        }
                    }

                    @Override
                    public void mouseMoved(MouseEvent e) {
                        lbl.setText("Mouse Point: " + e.getX() + " " + e.getY());
                        if (isNewLine == false) {
                            xend = e.getX();
                            yend = e.getY();
                            repaint();
                        }
                    }
                });

                addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        System.out.println(e.getX() + " " + e.getY() + "mouse");
                        if (isNewLine == true) {
                            xbegin = xend = e.getX();
                            ybegin = yend = e.getY();

                            isNewLine = false;
                            box = new Rectangle2D.Float(xend - 5, yend - 5, 12, 12);

                            boxContainer.add(box);
                        } else {

                            linebuffer = new Line2D.Float((float) xbegin, (float) ybegin, (float) xend, (float) yend);

                            System.out.println("xbegin: " + xbegin + " " + ybegin + " " + xend + " " + yend);
                            lineContainer.add(linebuffer);

                            repaint();

                            xbegin = e.getX();
                            ybegin = e.getY();

                            if (box.contains(xend, yend)) {
                                xend = (int) box.getCenterX();
                                yend = (int) box.getCenterY();
                                isNewLine = true;
                                return;
                            }

                            box = new Rectangle2D.Float(xend - 6, yend - 6, 12, 12);

                            boxContainer.add(box);
                            return;
                        }
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        begin.setText("Begin Values : " + xbegin + " " + ybegin);
                    }
                });
                setBorder(new BevelBorder(1));
            }

            @Override
            public Dimension getPreferredSize() {
                return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(), img.getHeight());
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (img != null) {
    //
                    Graphics2D g2 = (Graphics2D) g.create();
    //
                    int x = (int) ((getWidth() - (img.getWidth() * scale)) / 2);
                    int y = (int) (getHeight() - (img.getHeight() * scale)) / 2;

                    aft.setText("AFT : " + x + " " + y);

                    AffineTransform at = new AffineTransform();
                    at.translate(x, y);
    //                System.out.println(scale);
                    at.scale(scale, scale);

                    g2.setTransform(at);
                    g2.drawImage(img, 0, 0, this);

                    g2.draw(new Line2D.Float(xbegin, ybegin, xend, yend));
                    for (int i = 0; i < lineContainer.size(); i++) {

                        g2.draw(lineContainer.get(i));
                    }
                    g2.setStroke(new BasicStroke(3));
                    for (int i = 0; i < boxContainer.size(); i++) {
                        g2.setColor(Color.BLUE);
                        g2.draw(boxContainer.get(i));
                    }

                    g2.dispose();
    //                System.out.println("int x and y " + x + " " + y);
    //                System.out.println("getWidth and getHeight " + getWidth() + " " + getHeight());
                }
            }
        }
    }

公共级鼠标刻度测试{
int-xbegin=0;
int-ybegin=0;
int-xend=0;
int-yend=0;
布尔值isNewLine=true;
int initx;
完整性;
整数计数=0;
Line2D linebuffer;
矩形盒;
最终ArrayList lineContainer=新ArrayList();
final ArrayList boxContainer=新ArrayList();
公共静态void main(字符串[]args){
新的鼠标刻度测试();
}
公共鼠标刻度测试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
//JScrollPane
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
专用缓冲图像img;
专用浮标=1f;
专用浮点数标度LTA=0.05f;
JLabel-lbl;
JLabel标度点;
JLabel开始;
JLabel端;
JLabel-aft;
公共测试窗格(){
试一试{
img=ImageIO.read(新文件(“C:\\Users\\John Ebarita\\Documents\\report.jpg”);
}捕获(IOEX异常){
例如printStackTrace();
}
lbl=新的JLabel();
lbl.立根(0,0,50,10);
scalePoint=new JLabel();
缩放点。后退(50,0,50,10);
begin=新的JLabel();
开始.后退(150,0,50,10);
end=新的JLabel();
结束.立根(200,0,50,10);
aft=新的JLabel();
后立根(250,0,50,10);
添加(lbl);
添加(缩放点);
添加(开始);
添加(结束);
添加(船尾);
addMouseWheelListener(新的MouseWheelListener(){
@凌驾
已移动公共无效鼠标滚轮(鼠标滚轮事件e){
int-rotation=e.getWheelRotation();
如果(旋转<0){
scale-=scaleDelta;
}否则{
scale+=scaleDelta;
}
如果(刻度<0){
比例=0;
}
//否则,如果(比例>1.5){
//比例=1;
//                    }
scalePoint.setText(“Scale:+String.valueOf(Scale));
重新油漆();
//System.out.println(getSize());
}
});
addMouseMotionListener(新的MouseMotionListener(){
@凌驾
公共无效鼠标标记(鼠标事件e){
if(isNewLine==false){
xend=e.getX();
yend=e.getY();
end.setText(“结束值:“+xend+”“+yend”);
重新油漆();
}
}
@凌驾
public void mouseMoved(MouseEvent e){
setText(“鼠标点:+e.getX()+”+e.getY());
if(isNewLine==false){
xend=e.getX();
yend=e.getY();
重新油漆();
}
}
});
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效MouseEvent(MouseEvent e){
System.out.println(e.getX()+“”+e.getY()+“鼠标”);
if(isNewLine==true){
xbegin=xend=e.getX();
ybegin=yend=e.getY();
isNewLine=false;
box=新矩形2D.Float(xend-5,yend-5,12,12);
添加(box);
}否则{
linebuffer=newline2d.Float((Float)xbegin,(Float)ybegin,(Float)xend,(Float)yend);
System.out.println(“xbegin:+xbegin+”“+ybegin+”“+xend+”“+yend”);
lineContainer.add(linebuffer);
重新油漆();
xbegin=e.getX();
ybegin=e.getY();
如果(方框包含(xend,yend)){
xend=(int)box.getCenterX();
yend=(int)box.getCenterY();
isNewLine=true;
返回;
}
box=新矩形2D.Float(xend-6,yend-6,12,12);
添加(box);
返回;
}
AffineTransform atg;

Point2D calcCoordinates(MouseEvent e)
{
    Point p = new Point(e.getX(), e.getY());
    try
    {
        return atg.inverseTransform(p, null);
    }
    catch (NoninvertibleTransformException ex)
    {
        ex.printStackTrace();
        return p;
    }
}
g2.setTransform(at);
atg = (AffineTransform) at.clone(); // new!
xend = e.getX();
yend = e.getY();
Point2D p = calcCoordinates(e);
xend = (int) p.getX();
yend = (int) p.getY();
addMouseWheelListener(new MouseWheelListener()
{

    @Override
    public void mouseWheelMoved(MouseWheelEvent e)
    {
        int rotation = e.getWheelRotation();
        if (rotation < 0)
        {
            scale -= scaleDelta;
        }
        else
        {
            scale += scaleDelta;
        }
        if (scale < 0)
        {
            scale = 0;
        }
        //                    else if (scale > 1.5) {
        //                        scale = 1;
        //                    }
        scalePoint.setText("Scale: " + String.valueOf(scale));

        repaint();

        //                    System.out.println(getSize());
    }
});

addMouseMotionListener(new MouseMotionListener()
{
    @Override
    public void mouseDragged(MouseEvent e)
    {
        if (isNewLine == false)
        {
            xend = e.getX();
            yend = e.getY();

            //NEW
            Point2D p = calcCoordinates(e);
            xend = (int) p.getX();
            yend = (int) p.getY();

            end.setText("End Values : " + xend + " " + yend);
            repaint();
        }
    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
        lbl.setText("Mouse Point: " + e.getX() + " " + e.getY());
        if (isNewLine == false)
        {
            xend = e.getX();
            yend = e.getY();

            //NEW
            Point2D p = calcCoordinates(e);
            xend = (int) p.getX();
            yend = (int) p.getY();

            repaint();
        }
    }

});

addMouseListener(new MouseAdapter()
{

    @Override
    public void mouseReleased(MouseEvent e)
    {
        System.out.println(e.getX() + " " + e.getY() + "mouse");
        if (isNewLine == true)
        {
            xbegin = xend = e.getX();
            ybegin = yend = e.getY();

            //NEW
            Point2D p = calcCoordinates(e);
            xbegin = xend = (int) p.getX();
            ybegin = yend = (int) p.getY();

            isNewLine = false;
            box = new Rectangle2D.Float(xend - 5, yend - 5, 12, 12);

            boxContainer.add(box);
        }
        else
        {

            linebuffer = new Line2D.Float((float) xbegin, (float) ybegin, (float) xend, (float) yend);

            System.out.println("xbegin: " + xbegin + " " + ybegin + " " + xend + " " + yend);
            lineContainer.add(linebuffer);

            repaint();

            xbegin = e.getX();
            ybegin = e.getY();

            //NEW
            Point2D p = calcCoordinates(e);
            xbegin = (int) p.getX();
            ybegin = (int) p.getY();

            if (box.contains(xend, yend))
            {
                xend = (int) box.getCenterX();
                yend = (int) box.getCenterY();
                isNewLine = true;
                return;
            }

            box = new Rectangle2D.Float(xend - 6, yend - 6, 12, 12);

            boxContainer.add(box);
            return;
        }
    }