英文:
I have a frame with different shapes that I would like to move, but only one moves so far
问题
我的意图是能够在我的画面中移动三种不同的形状。但目前我只能移动正方形。有什么建议可以让程序移动所有的形状,而不仅仅是一个吗?
我在下面有一些方法和类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MouseMoveScale extends JPanel {
private Rectangle2D.Float myRect = new Rectangle2D.Float(50, 50, 100, 100);
private Ellipse2D.Float myCr = new Ellipse2D.Float(10,10, 100, 100);
private Rectangle2D.Float myTr = new Rectangle2D.Float(50, 50, 50, 50);
MovingAdapter ma = new MovingAdapter();
public MouseMoveScale() {
addMouseMotionListener(ma);
addMouseListener(ma);
addMouseWheelListener(new ScaleHandler());
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setColor(new Color(0, 0, 200));
g2d.fill(myRect);
g2d.setColor(new Color(139, 89, 255));
int xPoints[] = {110,180,30,110};
int yPoints[] = {30,100,100,30};
g2d.fillPolygon(xPoints, yPoints, 4);
g2d.setColor(new Color(0, 0, 117));
g2d.fillOval(10, 10, 100, 100);
}
class MovingAdapter extends MouseAdapter {
private int x;
private int y;
private boolean draggingRect = false;
private boolean draggingTr = false;
private boolean draggingCr = false;
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
draggingRect = myRect.getBounds2D().contains(x, y);
draggingTr = myTr.getBounds2D().contains(x, y);
draggingCr = myCr.getBounds2D().contains(x, y);
}
public void mouseDragged(MouseEvent e) {
int dx = e.getX() - x;
int dy = e.getY() - y;
if (draggingRect) {
myRect.x += dx;
myRect.y += dy;
}
if (draggingTr) {
myTr.x += dx;
myTr.y += dy;
}
if (draggingCr) {
myCr.x += dx;
myCr.y += dy;
}
x += dx;
y += dy;
repaint();
}
}
class ScaleHandler implements MouseWheelListener {
public void mouseWheelMoved(MouseWheelEvent e) {
int x = e.getX();
int y = e.getY();
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
if (myRect.getBounds2D().contains(x, y)) {
float amount = e.getWheelRotation() * 5f;
myRect.width += amount;
myRect.height += amount;
repaint();
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Shapes World");
MouseMoveScale m = new MouseMoveScale();
m.setDoubleBuffered(true);
frame.add(m);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
英文:
My intensions is to be able to move three different shapes in my frame. But I can move only square so far. Any suggestions how I can make program move all my shapes, not just one?
I have some methods and classes below
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MouseMoveScale extends JPanel {
private Rectangle2D.Float myRect = new Rectangle2D.Float(50, 50, 100, 100);
private Ellipse2D.Float myCr = new Ellipse2D.Float(10,10, 100, 100);
private Rectangle2D.Float myTr = new Rectangle2D.Float(50, 50, 50, 50);
MovingAdapter ma = new MovingAdapter();
public MouseMoveScale() {
addMouseMotionListener(ma);
addMouseListener(ma);
addMouseWheelListener(new ScaleHandler());
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D square = (Graphics2D) g;
Graphics2D triangle = (Graphics2D) g;
Graphics2D circle = (Graphics2D) g;
square.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
square.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
square.setColor(new Color(0, 0, 200));
square.fill(myRect);
triangle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
triangle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
triangle.setColor(new Color(139, 89, 255));
int xPoints[] = {110,180,30,110};
int yPoints[] = {30,100,100,30};
g.fillPolygon(xPoints, yPoints, 4);
circle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
circle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
circle.setColor(new Color(0, 0, 117));
circle.fillOval( 10,10, 100, 100 );
}
class MovingAdapter extends MouseAdapter {
private int x;
private int y;
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseDragged(MouseEvent e) {
int dx = e.getX() - x;
int dy = e.getY() - y;
if (myRect.getBounds2D().contains(x, y)) {
myRect.x += dx;
myRect.y += dy;
repaint();
}
if (myTr.getBounds2D().contains(x, y)) {
myTr.x += dx;
myTr.y += dy;
repaint();
}
if (myCr.getBounds2D().contains(x, y)) {
myCr.x += dx;
myCr.y += dy;
repaint();
}
x += dx;
y += dy;
}
}
class ScaleHandler implements MouseWheelListener {
public void mouseWheelMoved(MouseWheelEvent e) {
int x = e.getX();
int y = e.getY();
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
if (myRect.getBounds2D().contains(x, y)) {
float amount = e.getWheelRotation() * 5f;
myRect.width += amount;
myRect.height += amount;
repaint();
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Shapes World");
MouseMoveScale m = new MouseMoveScale();
m.setDoubleBuffered(true);
frame.add(m);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
I probably need to create new objects?
Or is it enough to fix the problem with if statement?
答案1
得分: 1
我认为这是你的错误
triangle.setColor(new Color(139, 89, 255));
int xPoints[] = {110,180,30,110};
int yPoints[] = {30,100,100,30};
g.fillPolygon(xPoints, yPoints, 4); ---> 注意这里。
你试图移动矩形,但没有改变你使用的xpoints/ypoints。
if (myTr.getBounds2D().contains(x, y)) {
myTr.x += dx; ---> 与xPoints无关
myTr.y += dy; ---> 与yPoints无关
repaint();
}
你可以将 - g.fillPolygon 更改为 -
triangle.fill(myCr);
我们的结果
所以现在你可以移动两个对象。
英文:
I think this is your error
triangle.setColor(new Color(139, 89, 255));
int xPoints[] = {110,180,30,110};
int yPoints[] = {30,100,100,30};
g.fillPolygon(xPoints, yPoints, 4); ---> Notice this.
You are trying to move the rec without changing the xpoints/ypoints you have used.
if (myTr.getBounds2D().contains(x, y)) {
myTr.x += dx; ---> not related to xPoints
myTr.y += dy; ---> not related to yPoints
repaint();
}
You can change - g.fillPolygon for e.x with -
triangle.fill(myCr);
Our result
So now you can move 2 objects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论