Solved

Applet Newbie Question

Posted on 2006-11-24
5
175 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

910 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

21 Experts available now in Live!

Get 1:1 Help Now