用Java移动椭圆和椭圆形状?

时间:2015-09-21 16:06:08

标签: java swing user-interface awt ellipse

我正在尝试用摇摆画一个机器人。机器人对象由几个椭圆和椭圆组成。我需要创建一个方法来根据用户的输入在JFrame周围移动机器人。我开始使用矩形作为机器人,所以我可以使用translate方法移动它。但椭圆不存在。如何编写移动形状的新方法?以下是我到目前为止的情况:

public class SwingBot 
{
public static void main(String[] args) 
{
    JFrame frame = new JFrame();

    frame.setSize(400,400);
    frame.setTitle("SwingBot");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Robot r = new Robot();

    frame.add(r);

    frame.setVisible(true);

    Scanner in = new Scanner(System.in);
    boolean repeat = true;
    System.out.println();
    while (repeat)
    {
        String str = in.next();
        String direc = str.toLowerCase();
        if (direc.equals("right"))
        {
            r.moveBot(10,0);
        }
        else if (direc.equals("left"))
        {
            r.moveBot(-10,0);
        }
        else if (direc.equals("up"))
        {
            r.moveBot(0,-10);
        }
        else if (direc.equals("down"))
        {
            r.moveBot(0,10);
        }
        else if (direc.equals("exit"))
        {
            repeat = false;
        }
    }

}


public static class Robot extends JComponent
{
    private Ellipse2D e = new Ellipse2D.Double(20,20,100,50);

    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;

        g2.setColor(Color.BLACK);
        g2.fillOval(45,60,50,50);
        g2.fill(e);

        g2.setColor(Color.RED);
        g2.fillOval(40,40,20,20);
        g2.fillOval(80,40,20,20);


    }

    public void moveBot(int x, int y)
    {

        repaint();
    }

}

}

底部的moveBot方法是空的,因为它最初在某些矩形对象上调用了translate方法,但我将它们更改为省略号和椭圆形。现在我不知道如何在没有翻译方法的情况下移动它们。

1 个答案:

答案 0 :(得分:1)

更改x方法中机器人的ymoveBot(),并在paintComponent()方法中使用这些变量

例如:(使用您的代码)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Scanner;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class SwingBot {
    public static void main(String[] args) {
        JFrame frame = new JFrame();

        frame.setSize(400, 400);
        frame.setTitle("SwingBot");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Robot r = new Robot();

        frame.add(r);

        frame.setVisible(true);

        Scanner in = new Scanner(System.in);
        boolean repeat = true;
        System.out.println();
        while (repeat) {
            String str = in.next();
            String direc = str.toLowerCase();
            if (direc.equals("right")) {
                r.moveBot(10, 0);
            } else if (direc.equals("left")) {
                r.moveBot(-10, 0);
            } else if (direc.equals("up")) {
                r.moveBot(0, -10);
            } else if (direc.equals("down")) {
                r.moveBot(0, 10);
            } else if (direc.equals("exit")) {
                repeat = false;
            }
        }

    }

    public static class Robot extends JComponent {
        int x = 45;
        int y = 60;

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            Ellipse2D e = new Ellipse2D.Double(x - 25, y - 40, 100, 50);

            g2.setColor(Color.BLACK);
            g2.fillOval(x, y, 50, 50);
            g2.fill(e);

            g2.setColor(Color.RED);
            g2.fillOval(x - 5, y - 20, 20, 20);
            g2.fillOval(x + 35, y - 20, 20, 20);

        }

        public void moveBot(int x, int y) {

            setX(getX() + x);
            setY(getY() + y);
            repaint();
        }

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

    }

}