1

我正在使用 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);



            }
        }
4

0 回答 0