[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Applet Newbie Question

Posted on 2006-11-24
5
Medium Priority
?
182 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 1000 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 1000 total points
ID: 18010782
or add the img to label and add the label to panel
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
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.
Suggested Courses

656 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