java - 如何让火柴人具有互动性?

标签 java swing graphics jpanel

所以,我需要通过用户输入使火柴人移动。当用户点击某个部位(头、手、脚和臀部)时,他应该移动,但不知道如何去做。 如果可能的话,角色周围还需要有一个限制,可能是矩形,这样每个部分可以拉动的距离就有限制。 请参阅下面的我的代码;

// Created by Charlie Carr - (28/11/17 - /11/17)
    import java.awt.*;
    import java.applet.Applet;
    import javax.swing.*;
    import java.awt.Graphics;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    //Imports complete

    //Suppress warning about undeclared static final serialVersionUID field in VS Code
    @SuppressWarnings("serial")
    public class Animator extends JPanel {
            public static class AnimatorWindow extends JPanel {
                    public void paint(Graphics page) {
                            setBackground(Color.gray);
                            setForeground(Color.white);
                            super.paintComponent(page);
                            page.drawString("Stickmen Animation Station", 150, 15);
                            //draw the head
                            //x1, y1, x2, y2
                            page.drawOval(90, 60, 20, 20);
                            // draw the body
                            page.drawLine(100, 80, 100, 110);
                            // draw the hands
                            page.drawLine(100, 90, 80, 105);
                            page.drawLine(100, 90, 120, 105);
                            //draw the legs, he hasn't a leg to stand on..
                            page.drawLine(100, 110, 85, 135);
                            page.drawLine(100, 110, 115, 135);
                    }
            }

            public static void main(String[] args) {
                    AnimatorWindow displayPanel = new AnimatorWindow();

                    JPanel content = new JPanel();
                    content.setLayout(new BorderLayout());
                    content.add(displayPanel, BorderLayout.CENTER);
                    //declare window size
                    int x = 480;
                    int y = 240;

                    JFrame window = new JFrame("GUI");
                    window.setContentPane(content);
                    window.setSize(x, y);
                    window.setLocation(101, 101);
                    window.setVisible(true);

            }

    }

最佳答案

使用MouseListener处理鼠标事件。

此外,您应该重写 paintComponent() 方法而不是 paint(),因为 paint() 还会绘制边框和其他内容东西。

public static class AnimatorWindow extends JPanel implements MouseListener{
    public AnimatorWindow(){
        setBackground(Color.gray);
        setForeground(Color.white);
        //add the listener
        addMouseListener(this);
    }
    public void paintComponent(Graphics page) {
        super.paintComponent(page);
        //You should not alter the Graphics object passed in
        Graphics2D g = (Graphics2D) page.create();

        //draw your stuff with g
        g.drawString("Stickmen Animation Station", 150, 15);
        .......

        //finish
        g.dispose();
    }


    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}
    public void mouseClicked(MouseEvent e){
        //implement your clicking here
        //Use e.getX() and e.getY() to get the click position
    }
}

有关挥杆 Activity 的更多信息,请查看this site

编辑:您的问题还包括动画,您可以使用 javax.swing.Timer 来执行此操作。

关于java - 如何让火柴人具有互动性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47579626/

相关文章:

java - 组件 Paint 方法未绘制到 JPanel 的中间?

c - glCreateShader 正在崩溃

java - 如何在 Java 8 中返回删除了一个条目的 Map

java - 从 src 复制到 Eclipse IDE 中的目标时,Excel 文件损坏

java - JTable 中列内的 Jcheckbox

java - 如何更改 JTable 中包含列的行?

java - 如何从 Java 中的 REST Web 服务中的 curl 获取参数值

java - Spring MVC - 在 Tomcat 上不断出现 404 错误

java - 将矩形保留在 JFrame 中

java - 在java中使用super.paintComponent(g)或getGraphics()