如何在画布上自由移动?

时间:2011-05-05 00:00:40

标签: java graphics canvas drawing graphics2d

我正在寻找像Prezi这样的效果,但是在Java中。我在Canvas中使用了JPanel个对象,直到现在我只有一个MouseMotionListener来移动对象。你有任何想法吗?

这是prezi及其画布:

enter image description here

1 个答案:

答案 0 :(得分:1)

查看this

这是我的五美分。这是一个简单的例子,绘图是如何工作的,但非常基本。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class DrawPanel extends JPanel {
    int mxstart, mystart;
    ArrayList<GObject> objects = new ArrayList<GObject>();
    {
        objects.add(new Rectangle(50, 100, Color.red, 30, 40));
        objects.add(new Circle(150, 200, Color.cyan, 50));
    }

    public DrawPanel() {
        addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                mxstart = e.getX();
                mystart = e.getY();
            }

            public void mousePressed(MouseEvent e) {
            mxstart = e.getX();
                mystart = e.getY();
            }
        });

        addMouseMotionListener(new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {
               int mx=e.getX();
                int my=e.getY();
                for (GObject go : objects)  {
                    if (go.inShape(mx, my)) {
                        go.setX(go.getX()+ (mx-mxstart));
                        go.setY(go.getY()+ (my-mystart));
                    }
                }
                repaint();
                mxstart=mx;
                mystart=my;
            }

            public void mouseMoved(MouseEvent e) {                
            }
        });

    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0,0,2000,1000);
        for (GObject go : objects) {
            go.draw(g);
        }
    }

    public static void main(String[] args) {
       JFrame jf = new JFrame();
       jf.add(new DrawPanel());
       jf.setBounds(0,0,1000,700);
       jf.setVisible(true);

    }

}


class GObject {
        protected int x,y;
        protected Color col;

        protected GObject(int x, int y, Color col) {
            this.x = x;
            this.y = y;
            this.col = col;
        }

        public Color getCol() {
            return col;
        }

        public void setCol(Color col) {
            this.col = col;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        protected void draw(Graphics g) {}

        protected boolean inShape(int x, int y) {return false;}
    }

class Rectangle extends GObject {
    private int w,h;

    public Rectangle(int x, int y, Color col, int w, int h) {
        super(x, y, col);
        this.w = w;
        this.h = h;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public boolean inShape(int mx, int my) {
        if (mx>=x && mx<=x+w && my>=y && my<=y+h) {
            return true;
        } else {
            return false;
        }
    }

    public void draw(Graphics g) {
        g.setColor(col);
        g.drawRect(x, y, w, h);
    }

}

class Circle extends GObject {
    private int r;

    public Circle(int x, int y, Color col, int r) {
        super(x, y, col);
        this.r = r;
    }

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }

    public boolean inShape(int mx, int my) {
        return Math.sqrt((mx-x)*(mx-x)+(my-y)*(my-y))<r;
    }

    public void draw(Graphics g) {
        g.setColor(col);
        g.drawOval(x, y, r, r);
    }

}