초보자 용 질문에 너무 많이 등장하지 않기를 바랍니다. 나는 전에 그래픽 스타일 프로그래밍을 해본 적이 없다. 내 목표는 애플릿에서 핀볼 광고 게임을 만드는 것입니다. 그러나 나는 첫 번째 장애물 중 하나에 빠지고있다. 내 애플릿이 내 Table 클래스 (JPanel을 확장)에서 paintComponent 메소드의 결과를 표시하지 않습니다. 나는 여러 가지를 시도했다. (예를 들어 현재 이중 버퍼링을 사용하고 있지만 이전에 mediatracker를 사용했다.) 다른 GUI 물건을 가지고 있지 않다면 페인팅을 허용한다. 어떻게 든 아래에 그려진다). 그리고 다른 물건. 이 문제는 저를 곤두박질 시켰고, 제가 간과 한 작은 것이라면 궁금해지기 시작했습니다. 그렇다면, 미안하지만 여전히 도움을받을 수있어서 감사 할 것입니다. 지금까지이 문제가 해결되지 않은 채로 내 핀볼 (애플릿) 및 테이블 클래스에 대한 내 코드는 아래에 있으며 다른 클래스는 아직 구현되지 않았습니다. 다시 한번, 나는 어떤 도움을 주셔서 감사합니다. JApplet의 자바 페인팅 관련 문제
import javax.swing.*; // useful for the drawing side, also going to be a JApplet
import java.awt.*;
import java.net.*;
public class Pinball extends JApplet {
// variables go here
Table table;
// further initialisation of the GUI
public void init() {
setSize(300, 300);
table = new Table(this);
add(table);
setContentPane(table); // makes our graphical JPanel container the content pane for the Applet
// createGUI(); // this has been moved onto the table class
}
public void stop() {
}
}
이제 테이블 클래스 :
import java.awt.*; // needed for old style graphics stuff
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*; // gives us swing stuff
import java.io.IOException;
import java.net.*; // useful for anything using URLs
public class Table extends JPanel {
// attributes go here
Pinball pb;
Color bgColour;
JPanel eastPanel;
JPanel logoPanel;
JPanel livesPanel;
JPanel scorePanel;
JPanel tablePanel;
JPanel scrollTextPanel;
Image logo;
// constructor goes here
public Table(Pinball pb) {
setPreferredSize(new Dimension(300, 300));
this.pb = pb;
// this is not needed anymore, with the new loadImage class down the bottom
// Toolkit tk = Toolkit.getDefaultToolkit(); // needed to get images
// logo = tk.getImage(base + "images/logo.jpg");
logo = loadImage("logo.jpg");
createGUI();
}
// public methods go here
// all GUI creation stuff goes here
public void createGUI() {
/* allows the three parts (top, middle and right)
* to be made through (north, center and right) */
setLayout(new BorderLayout());
// setting the background colour
bgColour = new Color(190, 186, 221); // makes the sky blue colour for the background.
setBackground(bgColour);
// now putting a panel for the east side
eastPanel = new JPanel();
eastPanel.setBackground(bgColour);
eastPanel.setLayout(new BorderLayout(8, 8));
eastPanel.setPreferredSize(new Dimension(100, 280));
logoPanel = new JPanel();
logoPanel.setBackground(Color.WHITE);
logoPanel.setPreferredSize(new Dimension(100, 100));
logoPanel.setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, Color.white));
//JLabel label1 = new JLabel("Logos go here");
//logoPanel.add(label1);
eastPanel.add(logoPanel, "North");
livesPanel = new JPanel();
livesPanel.setBackground(Color.WHITE);
livesPanel.setPreferredSize(new Dimension(100, 100));
JLabel label2 = new JLabel("Lives go here");
livesPanel.add(label2);
eastPanel.add(livesPanel, "Center");
scorePanel = new JPanel();
scorePanel.setBackground(Color.WHITE);
scorePanel.setPreferredSize(new Dimension(100, 80));
JLabel label3 = new JLabel("Scores go here");
scorePanel.add(label3);
eastPanel.add(scorePanel, "South");
add(eastPanel, "East");
tablePanel = new JPanel();
tablePanel.setBackground(bgColour);
tablePanel.setPreferredSize(new Dimension(200, 280));
add(tablePanel, "Center");
scrollTextPanel = new JPanel();
scrollTextPanel.setPreferredSize(new Dimension(300, 20));
scrollTextPanel.setBackground(Color.WHITE);
scrollTextPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
add(scrollTextPanel, "North");
// repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(logo, 5, 5, 90, 90, null);
g.drawLine(0, 0, 300, 300); // for testing, does not work
}
// a little useful method for handling loading of images and stuff
public BufferedImage loadImage(String filename) {
BufferedImage image = null;
try {
URL url = new URL(pb.getCodeBase(), "images/" + filename);
image = ImageIO.read(url);
} catch (IOException e) {
}
return image;
}
}
JPanel 서브 클래스에 초기화 할 때 구성 요소를 추가하고 paintComponent를 오버라이드하는 것은 멋지게 재생되지 않습니다. – justkt
@justkt : 커버하는 구성 요소의 불투명 한 속성을 false로 설정하면 멋지게 재생할 수 있습니다. 내 대답은 아래를 참조하십시오. –
@Hovercraft - 설명해 주셔서 감사합니다. – justkt