我想用 paintComponent(Graphics g)
使用for循环绘制一些图像。但是,jframe只是显示为一个白色屏幕,它只显示黑色背景和调整窗口大小后的图像。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.util.Scanner;
public class SimpleAdventure {
private JFrame frame;
private CardLayout cards;
private Container c;
private DrawPanel1 gamepanel;
private BufferedImage greentiles, pinktiles, sandtiles, charsprites;
private Image left1, right1, left2, right2, front;
private Image pinkbg, greenbg, sandbg;
private Image pinktop, greentop, sandtop;
private int charx, chary;
public SimpleAdventure()
{}
public static void main (String [] args)
{
SimpleAdventure retro = new SimpleAdventure();
retro.run();
}
public void run()
{
frame = new JFrame("Simple Adventure");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(576, 576);
frame.setVisible(true);
// frame.setResizable(false);
getImage();
c = frame.getContentPane();
cards = new CardLayout();
c.setLayout(cards);
gamepanel = new DrawPanel1();
gamepanel.setBackground(Color.black);
c.add(gamepanel, "Panel 1");
}
public void getImage()
{
try
{
greentiles = ImageIO.read(new File("greenTiles.png"));
pinktiles = ImageIO.read(new File("pinkTiles.png"));
sandtiles = ImageIO.read(new File("sandTiles.png"));
charsprites = ImageIO.read(new File("charactersheet.png"));
}
catch(IOException e)
{
e.printStackTrace();
}
left1 = charsprites.getSubimage(18, 0, 6, 6);
left2 = charsprites.getSubimage(24, 0, 6, 6);
right1 = charsprites.getSubimage(0, 0, 6, 6);
right2 = charsprites.getSubimage(6, 0, 6, 6);
front = charsprites.getSubimage(12, 0, 6, 6);
greenbg = greentiles.getSubimage(240, 0, 16, 16);
sandbg = sandtiles.getSubimage(240, 0, 16, 16);
pinkbg = pinktiles.getSubimage(240, 0, 16, 16);
greentop = greentiles.getSubimage(224, 0, 16, 16);
sandtop = sandtiles.getSubimage(224, 0, 16, 16);
pinktop = pinktiles.getSubimage(224, 0, 16, 16);
}
class DrawPanel1 extends JPanel implements KeyListener
{
// private BottomPanel lower;
public DrawPanel1()
{
/*this.setLayout(new BorderLayout());
lower = new BottomPanel();
this.add(lower, BorderLayout.SOUTH);*/
addKeyListener(this);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < 7; i++)
{
g.drawImage(greentop, 0 + (i * 96), 480, 96, 96, this);
}
System.out.println("check");
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
} }
2条答案
按热度按时间rkkpypqq1#
你需要打电话
frame.pack()
进行初始布局。调整窗口大小会自动使布局固定,但是frame.setSize(...)
没有*。移动
frame.setVisible(true)
到你生命的尽头run
方法(即在构造完所有ui元素之后)并将frame.pack()
就在之前frame.setVisible(true)
. (感谢气垫船和程序员指出这一点)编辑:现在我重新阅读了javadoc,您可能不想
pack
毕竟-它会调整你的框架。如果不测试的话,我想setVisible
到最后会奏效;如果没有,那就用revalidate
而不是pack
使布局引擎运行。编辑2:现在我重新阅读了javadoc,
setVisible
如果窗口尚未显示,将验证该窗口。所以你不需要一个明确的电话revalidate
.setSize
将使组件无效,但不会重新验证它。热释光;dr:值得一读的是验证和可显示性是如何在swing中工作的,这样你就不会犯我刚才犯的错误。
hmae6n7t2#
呼叫
setVisible
只有在你建立了用户界面之后。。。否则需要重新验证帧