Solved

Applet Newbie Question

Posted on 2006-11-24
5
174 Views
Last Modified: 2010-07-27
I am very new to writing applets although I have experience in many other languages.  I am trying to create an object for a game and having some trouble.  The GameBoard is comprised of five pieces:  The GamePanel which is in the center and then an image to the north, east, south and west of the GamePanel.   My GameBoard class in inherting JPanel and using the GridBagLayout that I got from an example, but it does not compile, can anyone give me a bit of guidance and let me know if I am thinking of this correctly.

import java.awt.*;
import javax.swing.*;

public class GameBoard extends JPanel
{
      public static final int WIDTH = 360;
      public static final int HEIGHT = 235;

      GamePanel m_GamePanel;

      public GameBoard(Image topImage, Image leftImage, Image bottomImage, Image rightImage)
      {
            GridBagConstraints c = new GridBagConstraints();

            setPreferredSize(new Dimension(WIDTH, HEIGHT));
            setLayout(new GridBagLayout());

            c.gridx = 0;
            c.gridy = 0;
            c.gridwidth = 3;
            add(topImage, c);

            c.gridx = 0;
            c.gridy = 1;
            add(leftImage, c);

            m_GamePanel = new GamePanel();
            c.gridx = 1;
            c.gridy = 1;
            add(m_GamePanel, c);

            c.gridx = 2;
            c.gridy = 1;
            add(rightImage, c);

            c.gridx = 0;
            c.gridy = 2;
            c.gridwidth = 3;
            add(bottomImage, c);
      }

}

import java.awt.*;
import javax.swing.*;

public class GamePanel extends JPanel
{
      public static final int WIDTH = 300;
      public static final int HEIGHT = 225;

      public GamePanel()
      {
            setPreferredSize(new Dimension(WIDTH, HEIGHT));

      }
}
0
Comment
Question by:jymmealey
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 250 total points
ID: 18010676
You cannot add an Image object to a Container(JPanel). You can make a very simple JPanel extension like:

class ImagePanel extends JPanel
{
    private Image image;
    public ImagePanel(Image image)
    {
        this.image = image;
    }
    public void paintComponent(Graphics g)
    {
        g.drawImage(image,0,0,this);
        super.paintComponent(g);
    }
}

Good luck.
Mark
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18010683
Whoops, don't forget the imports:

import java.awt.*;
import javax.swing.*;

Mark.
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 250 total points
ID: 18010782
or add the img to label and add the label to panel
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 76
wordcount challenge 11 76
Eclipse Neon and jdk 1.8.0 11 72
Groovy:unable to resolve class error 2 31
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now