gpt4 book ai didi

java - 如何在 Canvas 中自由移动?

转载 作者:行者123 更新时间:2023-11-30 11:54:05 25 4
gpt4 key购买 nike

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

这是 prezi 和它的 Canvas :

enter image description here

最佳答案

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

}

关于java - 如何在 Canvas 中自由移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5891217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com