我使用的是非常基本的Java图形,并试图同时显示多种图形方法。换句话说,我想将它们重叠。
当前,当它运行时,它会短暂显示一个图形,然后消失并显示另一个图形。
有什么办法可以解决这个问题?
这是我图形的跑步者。
import javax.swing.JFrame;
public class GraphicsRunnerCopy extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public GraphicsRunnerCopy(int x,int y,int z,int n)
{
super("My Frame");
setSize(WIDTH,HEIGHT);
getcontentPane().add(new Graphic(x,y,z,n));
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
GraphicsRunnerCopy run = new GraphicsRunnerCopy(3,3,1,20); //I want this to display at
GraphicsRunnerCopy run2 = new GraphicsRunnerCopy(0,5,4); //the same time as this
}
}
在跑步者正在显示的对象中,我正在使用如下代码。
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Canvas;
class Graphic extends Canvas
{
private int x,n;
public Graphic(int xa,ya,za,na)
{
x = xa;
y = ya;
z = za;
n = na;
setSize(800,600);
setBackground(Color.WHITE);
setVisible(true);
}
public void paint( Graphics window )
{
Color tan = new Color(185,133,91);
window.setColor(tan);
window.fillRect((x*n+20/z),(y*n+20/z),(320/z),(320/z)); //base
}
}
感谢您的帮助,谢谢。