Solved

Applet Newbie Question

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler 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