放大Java图形

放大Java图形,java,graphics,zooming,Java,Graphics,Zooming,我正在用java图形制作一个声速曲线图。我画了一个静态图,当你运行程序时会弹出,但我正在尝试实现它,如果用户点击x轴或y轴,它就会放大,这样你就可以更仔细地观察声速曲线。我没有音速曲线图,但我已经知道怎么做了,我只是省去了画图的工作量,直到我有了缩放功能。有人对如何使这项工作有什么想法吗?我曾看到有人尝试将仿射变换用于类似的任务,但我不确定这样做是否正确,或者我是否正确地这样做。要查看的特定代码是paint、zoomIn和mouseClicked方法。想法将是非常可取的 public class

我正在用java图形制作一个声速曲线图。我画了一个静态图,当你运行程序时会弹出,但我正在尝试实现它,如果用户点击x轴或y轴,它就会放大,这样你就可以更仔细地观察声速曲线。我没有音速曲线图,但我已经知道怎么做了,我只是省去了画图的工作量,直到我有了缩放功能。有人对如何使这项工作有什么想法吗?我曾看到有人尝试将仿射变换用于类似的任务,但我不确定这样做是否正确,或者我是否正确地这样做。要查看的特定代码是paint、zoomIn和mouseClicked方法。想法将是非常可取的

public class SoundSpeedProfile extends JPanel implements MouseListener, ActionListener {

    private String title;
    private String subTitle;
    private JFrame frame;
    private Graphics g;
    .
        .
        .


    /**Draws the sound speed profile and surrounding graphics
     * @param Graphics g - graphics object
     */
        public void paint(Graphics g){
            this.g = g;
            super.paint(g); //the super knows how to draw "standard" components like squares, rectangles, circles, etc
            g.setColor(Color.DARK_GRAY);
            //1) Set up the graph the sound speed profile lives in
            //X-Axis for Speeds
            g.drawLine(100, 150, 450, 150);//the graphics display has 0,0 in the upper left corner versus the lower left corner
            int i = 120;
            int k = 1460;
            while (i<440){
                g.drawString("|", i, 155);
                g.drawString("" + k + "", i-2, 140);
                k = k + 20;
                i = i + 60;
            }
            //Y-Axis
            g.drawLine(100, 500, 100, 150);
            k= 7000;
            int j = 500;
               while (j>160){
                g.drawString("" + k, 60, j);
                g.drawString("--", 94, j);
                k = k - 1000;
                j = j - 50;
            }

            Font f1 = new Font("Serif", 4, 15);
            g.setFont(f1);
            g.drawString(this.title, 200,30);//Graph Title
            g.drawString(this.subTitle, 225, 50);
            Font f2 = new Font("Serif", 2, 15);
            g.setFont(f2);
            g.drawString("Sound Speed ", 200, 110);//x-axis label
            g.drawString("(" + spdUnits + ")", 290, 110); //Units label--taken from input array
            g.drawString("Depth", 10, 180); //y-axis label
            g.drawString("(" + depUnits + ")", 07, 200); //Units label--taken from input array
            //((Graphics2D)g).scale(20, 20);



        }
        /**Creates and shows the GUI drawing of the sound speed profile in a JFrame
         */
        private void createAndShowGUI(){
            frame = new JFrame("Sound Speed Profile");
            canvas = new Canvas();
            frame.add(canvas);
            frame.addMouseListener(this);
            frame.setBackground(Color.cyan);
            frame.setSize(600,800);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLayout(new BorderLayout());
            frame.add(this, BorderLayout.CENTER);//add the sound speed profile graphic and set a Border Layout
            //frame.pack();
            frame.setVisible(true);
        }

        /**
         * Runs test cases
         * @param args
         */
        public static void main (String [] args){

                ssp.setTitle("Sound Speed Profile 1");
                ssp.setSubtitle("June 1, 2012");
                ssp.createAndShowGUI();

                ssp.repaint(); //necessary?

        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            int x = arg0.getX();
            //System.out.println("x: " + x);
            int xMin = x - 50;
            int xMax = x + 50;
            int y = arg0.getY();
            //System.out.println("y: " + y);
            int yMin = y-50;
            int yMax = y + 50;
            //If the user clicked on the x-axis
            if ( 160<y &&  y<180 && 100<x && x<450){
                System.out.println("About to zoom in on the x-axis");
                zoomIn(x, y);

                //System.out.println("zooming in on the x-axis");
            } 
            //If the user clicked on the y-axis
            if (90<x && x<110 && 150 <y && y<500){
                //System.out.println("zooming in on the y-axis");
            }


        }

        public void zoomIn(int x, int y){
            AffineTransform old = ((Graphics2D) g).getTransform();
            for (double zoom = 1; zoom >=0.1; zoom=-0.1){
                  AffineTransform tr2 =AffineTransform.getTranslateInstance(-x, -y);
                    AffineTransform  tr= AffineTransform.getScaleInstance(zoom,zoom);
                    tr.concatenate(tr2); tr2=tr;
                    tr =AffineTransform.getTranslateInstance(x, y);
                    tr.concatenate(tr2); tr2=tr;
                    tr= new AffineTransform(old);
                    tr.concatenate(tr2);  tr2=tr;
                    ((Graphics2D)g).setTransform(tr2);
                    ((Graphics2D)g).drawRect(x, y, 10, 10);
                    ((Graphics2D)g).setTransform(old);



            }
        }

你试过了吗?有什么问题吗?当我点击时,什么都没有发生:/你能使用吗?不,很遗憾我不能。“我的老板要求我手工绘制图形,我们正在从JFree Chart迁移,以便更快地获得更好的帮助,”发布一条消息。