【发布时间】:2018-10-23 08:34:03
【问题描述】:
我制作了一个海龟图形面板,它允许海龟从用户输入中移动,例如 forward <distance>、turnright、turnleft 等。但是,我不知道如何移动海龟,因为它被卡住了在屏幕的左上方。有谁知道我在代码上做错了什么并且可以为我调整它?我希望它直接在中心,笔向下
这是到目前为止应用程序的图片Turtle Graphics
头等舱
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
* Represents the graphics display panel within the turtle program. This panel contains an image which is updated to reflect user commands.
*
*
*
*/
@SuppressWarnings("serial")
public class GraphicsPanel extends JPanel
{
private JTextField console = new JTextField(15);
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
JMenuBar myMenuBar;
JMenu file;
JMenu help;
JMenuItem load;
JMenuItem save;
private static void createAndShowGUI() {
System.out.println("Created GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
JFrame f = new JFrame("Turtle Graphics");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new GraphicsPanel());
f.pack();
f.setVisible(true);
}
/**
* The default BG colour of the image.
*/
private final static Color BACKGROUND_COL = Color.DARK_GRAY;
private final static int TURTLE_X_SIZE = 8, TURTLE_Y_SIZE = 8;
/**
* The underlying image used for drawing. This is required so any previous drawing activity is persistent on the panel.
*/
private BufferedImage image, turtleDisplay;
//djm added
private Color PenColour = Color.RED;
private boolean penDown = false;
private int xPos=0, yPos=0;
private int direction = 180; //robot pointing down the screen;
private JMenuItem newfile;
private JMenuItem exit;
private JMenuItem about;
/**
* Draw a line on the image using the given colour.
*
* @param color
* @param x1
* @param y1
* @param x2
* @param y2
*/
public void drawLine(Color color, int x1, int y1, int x2, int y2)
{
Graphics g = image.getGraphics();
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
//djm added commands
public void penDown()
{
penDown = true;
}
public void penUp()
{
penDown = false;
}
public void turnRight()
{
direction +=90;
if (direction >= 360)
direction = 0;
}
public void turnLeft()
{
direction -=90;
if (direction < 0)
direction = 270;
}
public void forward(int distance)
{
//Graphics g = image.getGraphics();
int x=xPos,y=yPos;
//stored xPos and yPos are current location
if (direction == 0) //robot facing up the screen, so forward subtracts y
{
y = yPos-distance;
}
else if (direction == 90) //robot facing right so forward add x
{
x = xPos + distance;
}
else if (direction == 180) //robot facing down the screen, so forwards adds to y
{
y = yPos + distance;
}
else if (direction == 270) //robot facing left, so forwards subtracts from x
{
x = xPos - distance;
}
else
{
System.out.println("strange, shouldn't get here");
}
if (penDown)
{
//x=400; y=400;
drawLine(PenColour, xPos, yPos, x, y);
//g.drawLine(xPos,yPos,x,y);
}
//now robot has moved to the new position
xPos = x;
yPos = y;
}
/**
* Clears the image contents.
*/
public void clear()
{
Graphics g = image.getGraphics();
g.setColor(BACKGROUND_COL);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
}
public void setTurtleColour(Color col)
{
Graphics g = turtleDisplay.getGraphics();
g.setColor(col);
g.fillRect(0, 0, turtleDisplay.getWidth(), turtleDisplay.getHeight()); }
public void green()
{
setTurtleColour(Color.GREEN);
Graphics g = image.getGraphics();
g.setColor(Color.GREEN);
PenColour = Color.GREEN;
}
public void black()
{
setTurtleColour(Color.BLACK);
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
PenColour = Color.BLACK;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
g.drawImage(turtleDisplay, xPos-TURTLE_X_SIZE/2, yPos-TURTLE_Y_SIZE/2, null);
repaint();
// render the image on the panel.
g.drawImage(image, 0, 0, null);
g.drawImage(turtleDisplay, xPos-TURTLE_X_SIZE/2, yPos-TURTLE_Y_SIZE/2, null); }
/**
* Constructor.
*/
public GraphicsPanel() {
add(console);
console.addActionListener(new ActionListener()
//Command List//
{
public void actionPerformed(ActionEvent arg0)
{
if (console.getText().contains("penup"))
{
penUp();
}
else if (console.getText().contains("pendown"))
{
penDown();
}
else if (console.getText().contains("turnleft"))
{
turnLeft();
}
else if (console.getText().contains("turnright"))
{
turnRight();
}
else if (console.getText().contains("forward"))
{
forward(direction);
}
else if (console.getText().contains("reset"))
{
clear();
}
else if (console.getText().contains("red"))
{
setTurtleColour(PenColour);
}
else if (console.getText().contains("green"))
{
green();
}
else if (console.getText().contains("black"))
{
black();
}
else
{
JOptionPane.showMessageDialog(console, "Invalid command, try again");
}
console.setText("");
}
});
myMenuBar = new JMenuBar ();
file = new JMenu ("File");
help = new JMenu("Help");
load = new JMenuItem("Load");
save = new JMenuItem("Save");
newfile = new JMenuItem("New");
exit = new JMenuItem("Exit");
about = new JMenuItem("About");
file.add(load);
file.add(save);
file.add(newfile);
file.add(exit);
help.add(about);
myMenuBar.add(file);
myMenuBar.add(help);
add(myMenuBar);
setBorder(BorderFactory.createLineBorder(Color.black));}
public Dimension getPreferredSize() {
return new Dimension(800,400);
}
{
//main drawing area
image = new BufferedImage(800, 400, BufferedImage.TYPE_INT_RGB);
//small image to display on top of drawing area to represent the turtle
turtleDisplay = new BufferedImage(TURTLE_X_SIZE, TURTLE_Y_SIZE, BufferedImage.TYPE_INT_RGB);
//set up turtle
setTurtleColour(PenColour);
// Set max size of the panel, so that is matches the max size of the image.
setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));
setSize(800,400);
setVisible(true);
clear();
}
}
二等
public class turtleClass {
public static void main(String[] args)
{
turtleClass m = new turtleClass();
m.go();
}
public void go ()
{
GraphicsPanel p = new GraphicsPanel ();
p.turnLeft();
p.forward(100);
p.turnRight();
p.penDown();
p.forward(400);
}
}
【问题讨论】:
-
请不要破坏您的问题。一旦您在本网站上提出问题,根据您在加入本网站时同意的服务条款,您的问题及其代码将成为本网站的财产。
标签: java oop jpanel turtle-graphics