많은 이미지를 칠할 클래스 MenuScreen
이 있습니다. (순간에 4 당신에게 많은하지 않을 수 있지만, 그것은 나에게])BufferedImage가 때때로 렌더링되지 않습니다.
하나만 상관없이, 때로는 그들이 어떤 반 렌더링 무엇을 렌더링하지 않으며, 다른 시간들이 완전히 렌더링,
때때로 그들은 여기
이 는 는
사람은 이런 이유를 이해하는 데 도움이 수
public List<BufferedImage> im;
public MenuScreen() {
setTitle("ALevelUp 0.0.1 Alpha");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initImages();
JPanel p = new JPanel(new GridLayout(1, 1));
p.add(new JLabel(new ImageIcon(im.get(0))));
add(p);
setSize(766, 500);
setLocationRelativeTo(null);
setVisible(true);
System.err.println(getHeight() + "," + getWidth());
Graphics2D g = (Graphics2D) im.get(0).getGraphics();
draw(g);
}
public final void initImages() {
im = a.init();
}
public final void draw(Graphics2D g) {
BufferedImage s1 = im.get(1);
Graphics2D s1g = (Graphics2D) s1.getGraphics();
s1g.setFont(scale(a.getFont(), s1g, "Slot 1", s1));
s1g.setColor(Color.black);
s1g.drawString("Slot 1", s1.getWidth()/2 - 23, s1.getHeight()/2 + 7);
g.drawImage(s1, (getWidth()/2) - (s1.getWidth()/2) - 21,
47, rootPane);
s1g.setColor(new Color(253, 198, 147));
s1g.fillRect(106, 20, 100, 20);
s1g.setColor(Color.black);
s1g.drawString("Slot 2", s1.getWidth()/2 - 23, s1.getHeight()/2 + 7);
g.drawImage(s1, getWidth()/2 - s1.getWidth()/2 - 21, 179, rootPane);
s1g.setColor(new Color(253, 198, 147));
s1g.fillRect(106, 20, 100, 20);
s1g.setColor(Color.black);
s1g.drawString("Slot 3", s1.getWidth()/2 - 23, s1.getHeight()/2 + 7);
g.drawImage(s1, getWidth()/2 - s1.getWidth()/2 - 21, 311, rootPane);
}
public Font scale(Font f, Graphics g, String text, BufferedImage img) {
float ntry = 20.0f;
Font font = null;
while (2 < 3) {
font = f.deriveFont(ntry);
FontMetrics fm = g.getFontMetrics(font);
int width = fm.stringWidth(text);
if (width < img.getWidth()) {
return font;
}
}
}
무엇을 나는 그것을 해결하기 위해 할 수있는 내 코드
모두는 (주 하나는 여전히 렌더링)로 렌더링하지?
편집 : 당신이 그들을 필요로하는 경우 여기, 이미지 리소스 위치 :
The Main Screen
The Slot Panels
How it looks when it works
곧 도움을받을 수없는 경우, 작성 및 게시 생각해 보자 [, 최소를 완료하고 검증 가능한 예제 프로그램 (http://stackoverflow.com/help/mcve) 당신이에 코드를 응축 곳 여전히 컴파일되고 실행되는 가장 작은 비트는 외부 종속성 (예 : 데이터베이스에 연결해야 함)이 없으며 문제와 관련이없는 추가 코드가 없지만 여전히 문제를 보여줍니다. 이미지를 사용해야하는 경우 온라인에서 이미지를 사용할 수 있는지 확인하십시오. –
및 무엇이든 읽기 전에 [Oracle trail 2D Graphics] (http://docs.oracle.com/javase/tutorial/2d/index.html) – mKorbel
getGraphics를 사용하지 마십시오. Swing에서 페인팅이 수행되는 방식이 아닙니다. – MadProgrammer