有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么这个图形对象不是绘画?(我做错了什么?)

因此,我正在用Java编写一个程序,它需要能够做的一件重要事情是在屏幕上呈现多个对象,用户可以拖放这些对象。我已经创建了一个类(粘贴在下面),但是当我尝试用

new DragNode();

它不起作用。 有人能告诉我我做错了什么吗? 谢谢 萨姆

public static class DragNode extends JLabel{
    public static final long serialVersionUID=155L;

    public class MA extends MouseAdapter{
        public void mousePressed(MouseEvent e) {
            preX = rect.x - e.getX();
            preY = rect.y - e.getY();

            if (rect.contains(e.getX(), e.getY())) {
                updateLocation(e);
                System.out.println("Mouse pressed on rectangle");
            } else {
                pressOut = true;
            }
        }
        @Override
        public void mouseDragged(MouseEvent e) {
            if (!pressOut) {
                updateLocation(e);
            } else {
            }
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            if (rect.contains(e.getX(), e.getY())) {
                updateLocation(e);
            } else {
                pressOut = false;
            }
        }
        public void updateLocation(MouseEvent e) {
            rect.setLocation(preX + e.getX(), preY + e.getY());
            checkRect();

            repaint();
        }
    }

    public int x,y;
    Rectangle rect,area;
    int preX,preY;
    boolean firstTime=true;
    boolean pressOut=false;
    private Dimension dim=getGraphPanelSize();
    private Image Node_Sprite;

    public DragNode(){
        init();
    }
    public DragNode(int x,int y){
        this.x=x;
        this.y=y;
        init();
    }
    public void init(){
        //setBackground(Color.GRAY);
        setOpaque(false);
        addMouseMotionListener(new MA());
        addMouseListener(new MA());
        rect = new Rectangle(x, y, 50, 50);
        area=new Rectangle(dim);
        try{
        Node_Sprite=ImageIO.read(new File("Node_Sprite.png"));
        }catch(IOException ioe){}
    }
    boolean checkRect() {
        if (area == null) {
            return false;
        }
        if (area.contains(rect.x, rect.y, rect.getWidth(), rect.getHeight())) {
            return true;
        }
        int new_x = rect.x;
        int new_y = rect.y;
        if ((rect.x + rect.getWidth()) > area.getWidth()) {
            new_x = (int) area.getWidth() - (int) (rect.getWidth() - 1);
        }
        if (rect.x < 0) {
            new_x = -1;
        }
        if ((rect.y + rect.getHeight()) > area.getHeight()) {
            new_y = (int) area.getHeight() - (int) (rect.getHeight() - 1);
        }
        if (rect.y < 0) {
            new_y = -1;
        }
        rect.setLocation(new_x, new_y);
        return false;
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        if (firstTime) {
            rect.setLocation(x,y);
            firstTime = false;
        }
        g2d.setColor(new Color(0,0,0,0));
        g2d.drawImage(Node_Sprite, rect.x,rect.y,50,50,null);
    }
}

共 (2) 个答案

  1. # 1 楼答案

    不确定为什么要扩展JLabel。您没有使用标签绘制文本或图标。相反,您实际上是在自己进行自定义绘制,因此应该扩展JComponent

    或者,另一种方法是只使用JLabel显示图像,然后创建侦听器将标签拖动到新位置,并让标签显示在绘图中。您可以签出Component Mover,它允许您拖动任何组件

  2. # 2 楼答案

    当我将绘制矩形的代码替换为

        g2d.setColor(Color.red);
        g2d.fillRect( rect.x, rect.y, 50, 50);
        // g2d.drawImage(Node_Sprite, rect.x, rect.y, 50, 50, null);
    

    它已绘制并可以在x方向上拖动。因此,您的图像可能有问题(是否存在于指定的文件夹中?)。我预计拖动将在两个方向上进行,因此y方向也可能存在问题

    编辑:好的,我也用一张图片试过,效果很好。我还看到你的“边框”设置得很窄,所以我将它们增加到300/300,这样我就可以在两个轴上围绕我的图像拖动

    It's a me, Mario!

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    import net.miginfocom.swing.MigLayout;
    
    public class DragNode extends JLabel {
        public static final long serialVersionUID = 155L;
    
        public static void main(String[] args) {
            JLabel node = new DragNode();
            JFrame frame = new JFrame("blupp");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(new MigLayout(""));
            frame.getContentPane().add(node, "grow, push");
            frame.setLocationRelativeTo(null);
            frame.setSize(new Dimension(500, 300));
            frame.setVisible(true);
        }
    
        public class MA extends MouseAdapter {
            public void mousePressed(MouseEvent e) {
                preX = rect.x - e.getX();
                preY = rect.y - e.getY();
    
                if (rect.contains(e.getX(), e.getY())) {
                    updateLocation(e);
                    System.out.println("Mouse pressed on rectangle");
                } else {
                    pressOut = true;
                }
            }
    
            @Override
            public void mouseDragged(MouseEvent e) {
                if (!pressOut) {
                    updateLocation(e);
                } else {}
            }
    
            @Override
            public void mouseReleased(MouseEvent e) {
                if (rect.contains(e.getX(), e.getY())) {
                    updateLocation(e);
                } else {
                    pressOut = false;
                }
            }
    
            public void updateLocation(MouseEvent e) {
                rect.setLocation(preX + e.getX(), preY + e.getY());
                checkRect();
    
                repaint();
            }
        }
    
        public int x, y;
        Rectangle rect, area;
        int preX, preY;
        boolean firstTime = true;
        boolean pressOut = false; 
        private Dimension dim = new Dimension(300, 300);
        private Image Node_Sprite;
    
        public DragNode() {
            init();
        }
    
        public DragNode(int x, int y) {
            this.x = x;
            this.y = y;
            init();
        }
    
        public void init() {
            //setBackground(Color.GRAY);
            setOpaque(false);
            addMouseMotionListener(new MA());
            addMouseListener(new MA());
            rect = new Rectangle(x, y, 50, 50);
            area = new Rectangle(dim);
            try {
                Node_Sprite = ImageIO.read(new File("Node_Sprite.png"));
            } catch (IOException ioe) {}
        }
    
        boolean checkRect() {
            if (area == null) {
                return false;
            }
            if (area.contains(rect.x, rect.y, rect.getWidth(), rect.getHeight())) {
                return true;
            }
            int new_x = rect.x;
            int new_y = rect.y;
            if ((rect.x + rect.getWidth()) > area.getWidth()) {
                new_x = (int) area.getWidth() - (int) (rect.getWidth() - 1);
            }
            if (rect.x < 0) {
                new_x = -1;
            }
            if ((rect.y + rect.getHeight()) > area.getHeight()) {
                new_y = (int) area.getHeight() - (int) (rect.getHeight() - 1);
            }
            if (rect.y < 0) {
                new_y = -1;
            }
            rect.setLocation(new_x, new_y);
            return false;
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.green);
    //      g2d.fillRect(10, 10, 100, 100);
    
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    
            if (firstTime) {
                rect.setLocation(x, y);
                firstTime = false;
            }
            g2d.setColor(Color.red);
    //      g2d.fillRect( rect.x, rect.y, 50, 50);
            g2d.drawImage(Node_Sprite, rect.x, rect.y, 50, 50, null);
        }
    }