[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1068
  • Last Modified:

How to add multiples Canvas to an Applet?

This is what I have
- Two different java classes that extend Canvas
- The paint method of each of those classes draw different things and handled different events
I want to add an instance of each of those classes to a panel in an applet but I can add only one or the other but not both.
Is it possible to have multiple canvas in a panel?
How to add multiple Canvas to a Panel within an Applet?
here is the code


// Board.java
import java.awt.*;
import java.awt.event.*;
public class Board extends Canvas {
  public Board(Game game, int row, int column) { ...
      addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent event) {   //TODO    }
      });
   }
  public void paint(Graphics g) {
    Dimension d = getSize();
    columnWidth = (d.width/2) / column;
    rowHeight = (d.height/2) / row;
    ...
    g.setColor(Color.pink);
    g.fillRect(0, 0, (d.width/2), (d.height/2));
    g.setColor(Color.black);
    int i, j;
    for (i = 0; i <= row; i++) {
      g.drawLine(0, i * rowHeight, d.width/2, i * rowHeight);
    }
    for (j = 0; j <= column; j++) {
      g.drawLine(j * columnWidth, 0, j * columnWidth, d.height/2);
    }
  }
}
 
// AnyGame.java
import java.awt.*;
import java.awt.event.*;
public class AnyGame extends Canvas {
  public MathGame(Game game, int row, int column) {
     ....
    addMouseListener(new MouseAdapter() {
	public void mouseClicked(MouseEvent event) {   //TODO }
      });
  }
}
 
//Game.java
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Game extends Applet {
  public Game() {
    board = new Board(this, 3, 3);
    messageBar = new Label("Game begin.");
    anyGame = new AnyGame(this,3,3);
    // this only shows the canvas I put to the CENTER (either it is board or anyGame)
    setLayout(new BorderLayout());    
    add(messageBar, BorderLayout.SOUTH);
    add(board, BorderLayout.CENTER);
    add(anyGame, BorderLayout.EAST);
    
    /* THIS DOES NOT SHOW board and anyGame
    setLayout(new FlowLayout());
    add(messageBar);
    add(board);
    add(anyGame);
    */
    Panel controlPanel = new Panel();
    Button newGameButton = new Button("New Game");
    add(controlPanel, BorderLayout.NORTH);
    controlPanel.setLayout(new FlowLayout());
    controlPanel.add(newGameButton);
    public void actionPerformed(ActionEvent event) {//TODO}    });
  }
  protected Board  board;
  protected Label  messageBar;
  protected AnyGame anyGame;
 
   public static void main(String[] args) {
    Frame frame = new Frame("MY GAME");
    frame.setLayout(new BorderLayout());
    Game game = new Game();
    frame.add(game, BorderLayout.CENTER);
    game.newGame();
   frame.setSize(800, 600);
    frame.addWindowListener(new WindowAdapter() {
	public void windowClosing(WindowEvent e) {
	  System.exit(0);
	}
      });
    frame.show();
  }
 
}

Open in new window

0
Rositta
Asked:
Rositta
  • 3
1 Solution
 
CEHJCommented:
Add them to a GridLayout on the containing Panel
0
 
objectsCommented:
use a panel with a GridLayout and add the two to that


0
 
objectsCommented:
That will force them both to be the same size
If you want them to be different size then use something like a FlowLayout, and make sure you set the preferred size of your Canvas


0
 
objectsCommented:
try something like:

    setLayout(new FlowLayout());
    add(messageBar);
    board.setSize(new Dimension(100, 100));
    add(board);
    anyGame.setSize(new Dimension(100, 100));
    add(anyGame);

A BoxLayout is another good option

really depends on exactly how you want the two laid out though

0
 
RosittaAuthor Commented:
Thank you very much Savant, that worked like a charm.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now