我有一个带有不同形状的框架,我想移动它们,但目前只有一个移动了。

huangapple go评论87阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年3月16日 01:40:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/60695816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定