在 Java 中移动椭圆和椭圆形?
Move Ellipse and Oval Shapes in Java?
我正在尝试使用秋千画一个机器人。机器人对象由几个椭圆和椭圆组成。我需要创建一种方法来根据用户的输入在 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 方法,但我将它们更改为椭圆和椭圆。现在我不知道如何在没有翻译方法的情况下移动这些。
在moveBot()
方法中更改机器人的x
和y
,并在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;
}
}
}
我正在尝试使用秋千画一个机器人。机器人对象由几个椭圆和椭圆组成。我需要创建一种方法来根据用户的输入在 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 方法,但我将它们更改为椭圆和椭圆。现在我不知道如何在没有翻译方法的情况下移动这些。
在moveBot()
方法中更改机器人的x
和y
,并在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;
}
}
}