为什么这个图形对象不能绘画? (我究竟做错了什么?)

时间:2015-01-07 10:35:03

标签: java swing graphics drag-and-drop

所以我正在用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 个答案:

答案 0 :(得分:1)

当我用

替换用于绘制矩形的代码时
    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);
    }
}

答案 1 :(得分:0)

不确定为什么要扩展JLabel。您没有使用标签来绘制文本或图标。相反,你实际上是自己做自定义绘画,所以你应该扩展JComponent。

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