2

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

这是 prezi 和它的画布:

在此处输入图像描述

4

1 回答 1

1

看看这个

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

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);
    }

}
于 2011-05-05T10:01:09.023 回答