How to use panels in applets?

49 views Asked by At


I need to write a strategic game which contains awt panel.

Scenario:-
Firstly, I need to read from a txt file(my map) and draw my images based on the txt file. (i did it in the code below).

public class temp extends JApplet implements Runnable,MouseListener {

public void init() {
    setSize(1400,800 );
    setBackground(Color.BLACK);

}
@Override
public void start() {
    tilemap = new int[60][75];
    filereader();
    Thread thread = new Thread(this);
    thread.start();


}

@Override
public void paint(Graphics g) {

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            int mod_i = 100*i;
            int mod_j = 50*j;
            g.setColor(Color.red);
            String str = String.valueOf(tilemap[i][j]);
            g.drawString(str,mod_i,mod_j);

            switch (tilemap[i][j]) {
            case 1:
                g.drawImage(asfalt,mod_i,mod_j,this);
                break;
            case 2://it means everywhere u read 2, so draw a tree
                g.drawImage(tree,mod_i,mod_j,this);
                break;

            }

        }

    }

Secondly , i have to divide my window into 3 parts! (one biggest part, for main background, and 2 small part (one for mini map that will show where I am in the big map and one for showing information) )

Question:- How can I divide it? Should i use panels or borders?? I also have to use containers, as my map is too big and I have to scroll it.

Any suggestions for this scenario? Thanks

1

There are 1 answers

0
AnnoSiedler On BEST ANSWER

It's probably not the best idea to write everything in one class. You should definitely store your Filereader in another class.

By the way, do not mix awt and Swing. Either use JApplet and JPanel or Applet and Panel.

Why do you want to use borders? You can use Borders additionally, but that has nothing to do with your structure.

Make a new JPanel where you do all your "main background"-paintig stuff. Call setContentPane of your JApplet to add this JPanel.

Use for example BorderLayout for this JPanel. Create 2 more JPanels for map and information and simply add these JPanels to the main JPanel.

Use the setOpaque - Method of JPanel, if you need transparent background for a JPanel.

If your map (or your mini-map) is static, it might be a good idea, to draw your Images once to a BufferedImage and just draw the BufferedImage.