开发者社区> 问答> 正文

我有一个要移动的形状不同的框架,但到目前为止只可以移动一个

我的意图是能够在框架中移动三种不同的形状。但到目前为止,我只能移动正方形。关于如何使程序移动所有形状的建议,不只是一种?我下面有一些方法和类

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

我可能需要创建新对象?还是用if语句解决问题就足够了?

问题来源:Stack Overflow

展开
收起
montos 2020-03-23 21:33:25 577 0
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); ---> Notice this.
    

    您试图移动记录而不改变您使用过的xpoint / ypoint。

       if (myTr.getBounds2D().contains(x, y)) {
            myTr.x += dx; ---> not related to xPoints
            myTr.y += dy; ---> not related to yPoints
            repaint();
        }
    

    您可以使用-更改-g.fillPolygon for ex-

     triangle.fill(myCr);
    

    回答来源:Stack Overflow

    2020-03-23 21:34:14
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
典型模型-卷积神经网络入门 从概念原理到应用实现 立即下载
基于神经网络的语言合成 立即下载
典型模型-卷积神经网络入门 立即下载