Solved

Applet Newbie Question

Posted on 2006-11-24
5
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

726 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