简体   繁体   中英

How to get the scroll button to zoom in and out an object using java

Beginner here,

I am trying to make a simple application using java where a rectangle is created and can be moved using the keys "w,a,s,d". And also to increase or decrease size of the rectangle using the scroll button in a mouse. Now the object can be moved using the keys but I don't know how to do the scroll function. Somebody Help?

import java.awt.*;
import java.awt.event.*;

public class Animation extends Frame implements KeyListener, MouseWheelListener {
    int x, y, a, b;
    char choice1;

    Animation() {
        setSize(500, 500);
        setVisible(true);
        x = 50;
        y = 50;
        a = 20;
        b = 50;
        addKeyListener(this);
        addWindowListener(new WindowListener() {
            @Override
            public void windowOpened(WindowEvent e) {
            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            @Override
            public void windowClosed(WindowEvent e) {
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowActivated(WindowEvent e) {
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }
        });
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        choice1 = e.getKeyChar();
        if (choice1 == 'w') {
            y = y - 1;
        }
        if (choice1 == 's') {
            y = y + 1;
        }
        if (choice1 == 'a') {
            x = x - 1;
        }
        if (choice1 == 'd') {
            x = x + 1;
        }
        repaint();
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {

    }

    public void paint(Graphics g) {
        g.drawRect(x, y, a, b);
        g.setColor(Color.red);

        g.fillRect(x, y, 20, 50);

    }

    public static void main(String[] args) {
        Animation animation = new Animation();
    }
}

To be specific I want to know what should be written inside the mouseWheelMoved() method and how using that I can make the rectangle bigger or smaller.

Thanks

If somebody faces the same problem.

Use the getPreciseWheelRotation() method to get values. If the value is greater than 0, the scroller is being scrolled towards you and vice versa. I'm entering the code also here,

 double p = e.getPreciseWheelRotation();
    if(p>0){
        a=a+5;
        b=b+5;
    } else{
        a=a-5;
        b=b-5;
    }
    repaint();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM