저는 현재 AWT로 Java로 게임을 만들고 있습니다. 메인 클래스는 Frame을 확장하고 .getGraphics () 및 .drawRect ()를 사용하여 그래픽을 그리는 데 사용했습니다. 프레임에 레이블과 같은 구성 요소를 추가하면 그래픽 렌더링이 중지되고 구성 요소 만 표시된다는 점을 제외하면 제대로 작동했습니다.
getGraphics()
페인트에 사용 합니다. 그것은 적절한 방법이 아닙니다.paintComponent(Graphics g)
JPanel 의 메서드를 재정의합니다 . 이 메서드에서 모든 페인팅을 수행하고 암시 적으로 전달 된 Graphics 컨텍스트를 사용합니다. 암시 적으로 호출 paintComponent
되므로 실제로 호출 할 필요가 없습니다.paintComponent
을 재정의 할 수 있습니다 . 하지만 저는 스윙을 사용하기를 강력히 촉구합니다.paint
paintComponent
public class SimplePaint {
public SimplePaint() {
JFrame frame = new JFrame();
frame.add(new DrawPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class DrawPanel extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(50, 50, 150, 150);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new SimplePaint();
}
});
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다