2011-04-21 5 views
1

초보자 용 질문에 너무 많이 등장하지 않기를 바랍니다. 나는 전에 그래픽 스타일 프로그래밍을 해본 적이 없다. 내 목표는 애플릿에서 핀볼 광고 게임을 만드는 것입니다. 그러나 나는 첫 번째 장애물 중 하나에 빠지고있다. 내 애플릿이 내 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; 
     } 

} 
+0

JPanel 서브 클래스에 초기화 할 때 구성 요소를 추가하고 paintComponent를 오버라이드하는 것은 멋지게 재생되지 않습니다. – justkt

+0

@justkt : 커버하는 구성 요소의 불투명 한 속성을 false로 설정하면 멋지게 재생할 수 있습니다. 내 대답은 아래를 참조하십시오. –

+0

@Hovercraft - 설명해 주셔서 감사합니다. – justkt

답변

2

테이블 JPanel을 확인 할 수있는 다른 JPanel의, 적용되지만 덮고 불투명 한 구성 요소를 통해 볼 수 없습니다 그것, 특히 tablePanel JPanel. 예를 들어 다음을 입력하십시오.

tablePanel = new JPanel(); 
    // tablePanel.setBackground(bgColour); //!! removed 
    tablePanel.setOpaque(false); //!! added 

그리고 어떻게되는지보십시오.

여기에이 코드에 대한 당신을위한 관련이없는 질문 :

public void init() { 
     setSize(300, 300); 
     table = new Table(this); 
     add(table); 
     setContentPane(table); // makes our graphical JPanel container the content 

는 왜 한 번 JApplet에의 contentPane에 두 번 테이블 테이블 개체를 추가하고 있습니다 다음 JApplet에의의 contentPane로?

+0

나는 그 길에서 너무 길었다. ;) –

+0

안녕하세요. 빠른 응답에 감사드립니다. 불행히도 그것은 이상적으로 작동하지는 못했지만 그건 내 일입니다. 5, 5에 로고를 표시하여 이전 좌표 세트인지 확인했습니다 (logoPanel에서 가져옴). 이전에 좌표를 가져 오기 위해 logoPanel.getX() + 5 및 logoPanel.getY() + 5를 사용하고 있었고 왜 표시되지 않는지 확실하지 않았습니다. 그래도 코드가 작동했습니다. 이런 그래픽 프로젝트가 모든 그래픽과 컴포넌트를 사용하지 않는 것이 가장 좋을까요? 이것은 그래픽 측면의 것들에 대한 나의 첫 번째 여행입니다. –

+0

안녕하세요 @ 호버 크래프트 뱀장어로 가득 차 있습니다. 신속한 대응에 감사드립니다. 코드가 제대로 작동했지만 효율적으로 사용할 수 없습니다. 로고를 logoPanel에 표시하려고했지만 이미지가 움직여서이 문제를 디버깅하려고 했으므로 제대로 작동하지 않는 코드를 사용했습니다. 위치 지정을 위해 이전에 사용했던 코드는 (logo, logoPanel.getX() + 5, logoPanel.getY() + 5 ...) 등이었습니다. 필요한 경우 절대 좌표를 사용할 수 있습니다. –

1

실행 후이 코드 &을 검사하여 문제의 원인을 찾아 낼 수 있는지 확인하십시오.

// <applet code='Pinball' width='300' height='300'></applet> 
import java.awt.*; 
import java.awt.image.BufferedImage; 
import javax.swing.*; // useful for the drawing side, also going to be a JApplet 
import javax.imageio.ImageIO; 

import java.net.*; 
import java.io.IOException; 

public class Pinball extends JApplet { 
    // variables go here 
    Table table; 

    // further initialisation of the GUI 
    public void init() { 
     table = new Table(this); 
     setContentPane(table); // makes our graphical JPanel container the content pane for the Applet 
    } 
} 

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"); 
     int size = 100; 
     logo = //loadImage("logo.jpg"); 
      new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB); 
     Graphics g = logo.getGraphics(); 
     g.setColor(Color.red); 
     g.fillRect(0,0,size,size); 

     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(20,20)); 
     // 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.green)); 
     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"); 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     System.out.println("paintComponent"); 
     super.paintComponent(g); 
     g.setColor(Color.black); 
     g.drawImage(logo, 5, 5, 90, 90, this); 
     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) { 
      // do NOT ignore exceptions in broken code. 
     } 
     return image; 
    } 
} 
+0

안녕하세요 @ 앤드류 톰슨, 고마워요. 그래서 당신은 내 loadImage 메서드라고 생각/생각하고 있다고 말하는거야? 나는 그것에 대한 몇 가지 순열을 시도했지만 도움이 될 것이라고 생각한다면 그 하나를 시도 할 것입니다. –

+0

아니요. 'BorderLayout' 생성자에 대한 첫 번째 호출의 인수를 제거하고 차이점을 확인하십시오. 부분적으로 표시된 이미지/선이 다시 사라지게해야합니다. 그냥 옆으로.대부분의'setPreferredSize()'호출을 변경하는 것에 저항했지만, GUI를 제거하고 GUI를 올바르게 배치하는 법을 배우는 것이 낫습니다. –