简体   繁体   English

如何使用 java 使滚动按钮放大和缩小 object

[英]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".我正在尝试使用 java 创建一个简单的应用程序,其中创建了一个矩形并且可以使用键“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.现在 object 可以使用键移动,但我不知道如何滚动 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.具体来说,我想知道在 mouseWheelMoved() 方法中应该写什么以及如何使用它来使矩形更大或更小。

Thanks谢谢

If somebody faces the same problem.如果有人面临同样的问题。

Use the getPreciseWheelRotation() method to get values.使用 getPreciseWheelRotation() 方法获取值。 If the value is greater than 0, the scroller is being scrolled towards you and vice versa.如果该值大于 0,则滚动条正在向您滚动,反之亦然。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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