2011-04-21 14 views
1

これは初心者の質問にはあまり表示されないことを願っています。私は前にグラフィカルスタイルのプログラミングをしていない。私の目的は、アプレットにピンボール広告のゲームを作成することです。しかし、私は最初のハードルに落ちています。私のアプレットは、(JPanelを継承する)TableクラスからpaintComponentメソッドの結果を表示していません。私は、画像をロードする方法(現在はダブルバッファリングを使用していますが、前にmediatrackerを使用していました)、他のGUIのものがないかどうかを確認するなど、いくつか試しました。何とか下に描かれている)と他のもの。この問題は私を困惑させてしまいました。私は見落としてしまった小さなものなら、私は不思議に思っています。もしそうなら、申し訳ありませんが、私はとても行くことができないので、まだ助けに感謝しています。この問題はまず解決されていません。ピンボール(アプレット)とテーブルクラスのコードは以下のとおりですが、他のクラスはまだ実装されていません。もう一度、私はどんな助けにも感謝します。JAppletのJavaペインティングのグラフィックス

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をOKすることができ、他のの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

こんにちは@Hovercraft Eelsさん、あなたの迅速な応答に感謝します。あなたのコードはうまくいきましたが、効率的に使用することはできません。私は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

こんにちは@Andrew Thompson、ありがとう。それは私のloadImageメソッドだと思っていると思っていますか?私はそれについていくつかの順列を試しましたが、それが助けになると思うなら、それを試してみましょう。 –

+0

いいえ「BorderLayout」コンストラクタへの最初の呼び出しの引数を削除し、その違いを確認してください。部分的に表示された画像/行が再び消えるようにする必要があります。ちょうど別のところ。私は 'setPreferredSize()'コールのほとんどを変更することに抵抗しましたが、GUIを削除してGUIを適切にレイアウトする方法を検討する方がよいことに注意してください。 –

関連する問題