Java 我需要更新图形面板,以便用户可以查看结果
是的,我得到了一个新项目,我必须创建一个海龟程序,用户输入“前进10”,海龟在屏幕上画一条线。我们从一开始就得到了大量的代码,这让我们从中获得了一些乐趣,我也无法完全理解如何更新实际的图形面板。下面是给我们的代码:Java 我需要更新图形面板,以便用户可以查看结果,java,swing,graphics,panel,turtle-graphics,Java,Swing,Graphics,Panel,Turtle Graphics,是的,我得到了一个新项目,我必须创建一个海龟程序,用户输入“前进10”,海龟在屏幕上画一条线。我们从一开始就得到了大量的代码,这让我们从中获得了一些乐趣,我也无法完全理解如何更新实际的图形面板。下面是给我们的代码: `import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferedImage
`import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* Represents the graphics display panel within the turtle program. This panel contains an image which is updated to reflect user commands.
*
* @author mdixon
*
*/
@SuppressWarnings("serial")
public class GraphicsPanel extends JPanel {
int currentX = 0,currentY = 0;
JTextField commandLine;
/**
* The default BG colour of the image.
*/
private final static Color BACKGROUND_COL = Color.DARK_GRAY;
/**
* The underlying image used for drawing. This is required so any previous drawing activity is persistent on the panel.
*/
private BufferedImage image;
/**
* Draw a line on the image using the given colour.
*
* @param color
* @param x1
* @param y1
* @param x2
* @param y2
*/
public void createLine(int x2, int y2) {
System.out.println("current x position: " + currentX + "\ncurrent y position: " + currentY);
Graphics g = image.getGraphics();
g.drawLine(currentX, currentY, x2, y2);
//g.drawString("bloop" , currentX, currentY);
currentX = x2;
currentY = y2;
}
/**
* 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 setColour(Color colour){
Graphics g = image.getGraphics();
g.setColor(colour);
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
// render the image on the panel.
g.drawImage(image, 0, 0, null);
}
/**
* Constructor.
*/
GraphicsPanel() {
setPreferredSize(new Dimension(500, 300));
image = new BufferedImage(500, 300, BufferedImage.TYPE_INT_RGB);
// Set max size of the panel, so that is matches the max size of the image.
setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));
clear();
createLine(currentX + 100,currentY); //THIS WORKS AND UPDATES THE GRAPHICS PANEL
}
}`
我已经对它做了一些修改,但只是很小的改动。
从包含JtextField
和JButton
因此,我使用以下方法调用另一个面板:
GraphicsPanel graphic=新的GraphicsPanel();
graphic.createLine(graphic.currentX+100,graphic.currentY)代码>
我知道这实际上涉及到了createLine
,因为我添加了一个控制台输出,它显示了当前x和y位置的变化
所以,是的,如果有人能在从另一个面板调用图形面板时帮助更新它,那将是非常感谢的。
更新代码:
主要内容:
`
所有有效的代码,都应该使用复制/粘贴1)为了更快地获得更好的帮助,发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例)。2) 在源代码中只需要一行空白就可以了。{
之后或}
之前的空行通常也是多余的。public void paint(Graphics g){super.paintComponent(g);g.drawImage(image,0,0,null);
应该是public void paintComponent(Graphics g){super.paintComponent(g);g.drawImage(image,0,0,this);
@Paul I添加了graphic.repaint()
对代码@andrewhompson没有明显的影响我应该如何调用paintComponent
以及我应该向它传递哪些参数?“我应该如何…”那个MCVE在哪里?@andrewhompson当我从面板调用它时,会向paintComponent
传递哪些参数?(更好?)
`
import java.awt.*;
import javax.swing.*;
public class driver {
public static void main(String[] args) {
JFrame frame = new JFrame("Turtle");
frame.setSize(525, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
WindowsPanel Window = new WindowsPanel();
frame.setJMenuBar(Window.createMenuBar());
frame.setContentPane(Window.createContentPane());
GraphicsPanel panel = new GraphicsPanel(); // creates panel
panel.setLayout(new BorderLayout());
frame.getContentPane().add(panel, BorderLayout.CENTER); // adds panel to frame
CommandPanel panel1 = new CommandPanel(); // creates panel
panel1.setLayout(new GridLayout(1, 2));
frame.getContentPane().add(panel1, BorderLayout.SOUTH); // adds panel to frame
frame.setVisible(true);
}
}
import java.awt.Component;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
public class CommandPanel extends JPanel {
GraphicsPanel graphic = new GraphicsPanel();
private JTextField commandLine;
private JButton commandProcess;
CommandPanel() {
add(commandLine = new JTextField(40), Component.TOP_ALIGNMENT);
add(commandProcess = new JButton("Submit Command"),
Component.TOP_ALIGNMENT);
commandProcess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String commands = commandLine.getText();
try{
String[] userCommand = new String[2];
userCommand = commands.split(" ");
String action = userCommand[0];
int amount = Integer.parseInt(userCommand[1]);
switch (action) {
case ("forward"):
newX = graphic.currentX + amount;
newY = graphic.currentY - amount;
graphic.createLine(newX,newY);
break;
case ("blue"):
graphic.setColour(Color.BLUE);
break;
default:
System.out.println("Invalid command!");
}
} catch (ArrayIndexOutOfBoundsException e1){
System.out.println("both sides of the command mus be inputted");
}
}
});
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* Represents the graphics display panel within the turtle program. This panel contains an image which is updated to reflect user commands.
*
* @author mdixon
*
*/
@SuppressWarnings("serial")
public class GraphicsPanel extends JPanel {
int currentX = 0,currentY = 0;
/**
* The default BG colour of the image.
*/
private final static Color BACKGROUND_COL = Color.DARK_GRAY;
/**
* The underlying image used for drawing. This is required so any previous drawing activity is persistent on the panel.
*/
BufferedImage image;
/**
* Draw a line on the image using the given colour.
*
* @param color
* @param x1
* @param y1
* @param x2
* @param y2
*/
public void createLine(int x2, int y2) {
Graphics g = image.getGraphics();
g.drawLine(currentX, currentY, x2, y2);
currentX = x2;
currentY = y2;
}
/**
* 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 setColour(Color colour){
Graphics g = image.getGraphics();
g.setColor(colour);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
@Override
public void paint(Graphics g) {
super.paint(g);
// render the image on the panel.
g.drawImage(image, 0, 0, null);
}
/**
* Constructor.
*/
GraphicsPanel() {
setPreferredSize(new Dimension(500, 300));
image = new BufferedImage(500, 300, BufferedImage.TYPE_INT_RGB);
// Set max size of the panel, so that is matches the max size of the image.
setMaximumSize(new Dimension(image.getWidth(), image.getHeight()));
clear();
currentX = 200;
currentY = 200;
}
}