How to change this applet to swing?

import java.applet.Applet;
import java.awt.*;
import java.io.PrintStream;

public class Hounds extends Applet
{

    public String getAppletInfo()
    {
        return "Hare and Hounds";
    }

    public String[][] getParameterInfo()
    {
        return parameterInfo;
    }

    public void init()
    {
        String s = getParameter("bgColor");
        try
        {
            BG_COLOR = s != null ? new Color(Integer.parseInt(s, 16)) : DF_BG_COLOR;
        }
        catch(NumberFormatException _ex)
        {
            BG_COLOR = DF_BG_COLOR;
            System.out.println("ERROR: Invalid bgColor parameter.");
        }
        String s1 = getParameter("boardBgColor");
        try
        {
            BOARD_BG_COLOR = s1 != null ? new Color(Integer.parseInt(s1, 16)) : BG_COLOR;
        }
        catch(NumberFormatException _ex)
        {
            BOARD_BG_COLOR = BG_COLOR;
            System.out.println("ERROR: Invalid boardBgColor parameter.");
        }
        sTITLE = getParameter("title");
        if(sTITLE == null)
            sTITLE = new String("Hare and Hounds");
        sNEW_GAME = getParameter("newGame");
        if(sNEW_GAME == null)
            sNEW_GAME = new String("NEW GAME");
        sPLAYER = getParameter("player");
        if(sPLAYER == null)
            sPLAYER = new String("PLAYER");
        sHOUNDS = getParameter("hounds");
        if(sHOUNDS == null)
            sHOUNDS = new String("Hounds");
        sHARE = getParameter("hare");
        if(sHARE == null)
            sHARE = new String("Hare");
        sLEVEL = getParameter("level");
        if(sLEVEL == null)
            sLEVEL = new String("LEVEL");
        sBEGINNER = getParameter("beginner");
        if(sBEGINNER == null)
            sBEGINNER = new String("Beginner");
        sINTERMED = getParameter("intermediate");
        if(sINTERMED == null)
            sINTERMED = new String("Intermediate");
        sEXPERT = getParameter("expert");
        if(sEXPERT == null)
            sEXPERT = new String("Expert");
        sSCORE = getParameter("score");
        if(sSCORE == null)
            sSCORE = new String("SCORE");
        s2PLAYER = getParameter("player2");
        if(s2PLAYER == null)
            s2PLAYER = new String("Player");
        sCOMPUTER = getParameter("computer");
        if(sCOMPUTER == null)
            sCOMPUTER = new String("Computer");
        sFIRST_MOVE = getParameter("firstMove");
        if(sFIRST_MOVE == null)
            sFIRST_MOVE = new String("The Hounds move first..");
        sSTALLING = getParameter("stalling");
        if(sSTALLING == null)
            sSTALLING = new String("The Hounds are stalling !  The Hare wins !");
        sHARE_WINS = getParameter("hareWins");
        if(sHARE_WINS == null)
            sHARE_WINS = new String("The Hare escapes !");
        sHOUNDS_WIN = getParameter("houndsWin");
        if(sHOUNDS_WIN == null)
            sHOUNDS_WIN = new String("The Hare is trapped !  The Hounds win !");
        MediaTracker mediatracker = new MediaTracker(this);
        java.net.URL url = getCodeBase();
        boardImage = getImage(url, "board.gif");
        gifletsImage = getImage(url, "giflets.gif");
        mediatracker.addImage(boardImage, 0);
        mediatracker.addImage(gifletsImage, 1);
        try
        {
            mediatracker.waitForAll();
        }
        catch(InterruptedException _ex) { }
        cp = new ControlPanel(this);
        bc = new BoardCanvas(boardImage, gifletsImage, this);
        bc.resize(440, 276);
        Panel panel = new Panel();
        panel.setBackground(Color.lightGray);
        panel.setLayout(new FlowLayout(1, 5, 10));
        panel.add(cp);
        Panel panel1 = new Panel();
        panel1.setLayout(new BorderLayout(10, 5));
        panel1.setBackground(Color.lightGray);
        panel1.add("West", lTitle = new Label(sTITLE, 1));
        lTitle.setFont(ControlPanel.titleFont);
        tfStatus = new TextField();
        tfStatus.setFont(ControlPanel.statusFont);
        tfStatus.setForeground(Color.white);
        tfStatus.setBackground(Color.black);
        tfStatus.setEditable(false);
        panel1.add("Center", tfStatus);
        setBackground(BG_COLOR);
        Panel panel2 = new Panel();
        panel2.setLayout(new BorderLayout(0, 0));
        panel2.setBackground(BOARD_BG_COLOR);
        Panel panel3 = new Panel();
        panel3.setLayout(new FlowLayout(1, 10, 10));
        panel2.setLayout(new BorderLayout());
        panel2.add("Center", bc);
        panel2.add("East", panel);
        panel2.add("South", panel1);
        panel3.add(panel2);
        add(panel3);
        validate();
        new Ref();
        cp.setPlayerScore(playerScore);
        cp.setComputerScore(computerScore);
        newGame();
    }

    void newGame()
    {
        System.gc();
        if(playerAnimal == 1)
            setStatus(sFIRST_MOVE);
        else
            setStatus(" ");
        gameOver = false;
        bd = new Board(cp, this);
        hnh = new AI(bd, cp, this);
        bc.drawBoard(bd);
        setFocus();
        if(computerAnimal == 1)
            computerMove();
    }

    private void endGame(int i)
    {
        if(i == 1)
            cp.setComputerScore(++computerScore);
        else
            cp.setPlayerScore(++playerScore);
        gameOver = true;
        bc.drawBoard(bd);
    }

    private void computerMove()
    {
        hnh.move(computerAnimal);
        if(bd.isWin(computerAnimal))
        {
            endGame(1);
            return;
        } else
        {
            bc.drawBoard(bd);
            setStatus(" ");
            return;
        }
    }

    void selectPiece(int i, int j)
    {
        if(!gameOver)
        {
            startSq = bc.pixelToSquare(i, j);
            if(bd.selectPiece(playerAnimal, startSq))
            {
                bc.drawBoard(bd, i, j);
                return;
            }
            startSq = -1;
        }
    }

    void dragPiece(int i, int j)
    {
        if(!gameOver && startSq != -1)
            bc.drawBoard(bd, i, j);
    }

    void dropPiece(int i, int j)
    {
        if(!gameOver && startSq != -1)
        {
            endSq = bc.pixelToSquare(i, j);
            if(bd.movePlayerPiece(startSq, endSq))
            {
                if(bd.isWin(playerAnimal))
                {
                    endGame(0);
                } else
                {
                    bc.drawBoard(bd);
                    setStatus(" ");
                    computerMove();
                }
            } else
            {
                bc.drawBoard(bd);
            }
        }
        startSq = -1;
    }

    void setPlayerAnimal(int i)
    {
        if(i != playerAnimal)
        {
            if(i == 2)
            {
                playerAnimal = 2;
                computerAnimal = 1;
            } else
            {
                playerAnimal = 1;
                computerAnimal = 2;
            }
            setFocus();
            if(!gameOver)
                computerMove();
        }
    }

    void setStatus(String s)
    {
        tfStatus.setText(" " + s);
    }

    int getPlayerAnimal()
    {
        return playerAnimal;
    }

    void setFocus()
    {
        bc.requestFocus();
    }

    public Hounds()
    {
        playerAnimal = 1;
        computerAnimal = 2;
        startSq = -1;
    }

    String parameterInfo[][] = {
        {
            "bgColor", "integer", "Background color (24-bit RGB hex int)"
        }, {
            "boardBgColor", "integer", "Board background color (24-bit RGB hex int)"
        }, {
            "title", "string", "Applet title text"
        }, {
            "newGame", "string", "New Game button text"
        }, {
            "player", "string", "Player Side label text"
        }, {
            "hounds", "string", "Player Side Hounds checkbox text"
        }, {
            "hare", "string", "Player Side Hare checkbox text"
        }, {
            "level", "string", "Level label text"
        }, {
            "beginner", "string", "Beginner level text"
        }, {
            "intermediate", "string", "Intermediate level text"
        }, {
            "expert", "string", "Expert level text"
        }, {
            "score", "string", "Score label text"
        }, {
            "player2", "string", "Player Score label text"
        }, {
            "computer", "string", "Computer Score label text"
        }, {
            "firstMove", "string", "Hounds first status text"
        }, {
            "stalling", "string", "Hounds Stalling status text"
        }, {
            "hareWins", "string", "Hare Wins status text"
        }, {
            "houndsWin", "string", "Hounds Win status text"
        }
    };
    static final Color DF_BG_COLOR;
    static final String DF_sTITLE = "Hare and Hounds";
    static final String DF_sNEW_GAME = "NEW GAME";
    static final String DF_sPLAYER = "PLAYER";
    static final String DF_sHOUNDS = "Hounds";
    static final String DF_sHARE = "Hare";
    static final String DF_sLEVEL = "LEVEL";
    static final String DF_sBEGINNER = "Beginner";
    static final String DF_sINTERMED = "Intermediate";
    static final String DF_sEXPERT = "Expert";
    static final String DF_sSCORE = "SCORE";
    static final String DF_s2PLAYER = "Player";
    static final String DF_sCOMPUTER = "Computer";
    static final String DF_sFIRST_MOVE = "The Hounds move first..";
    static final String DF_sSTALLING = "The Hounds are stalling !  The Hare wins !";
    static final String DF_sHARE_WINS = "The Hare escapes !";
    static final String DF_sHOUNDS_WIN = "The Hare is trapped !  The Hounds win !";
    static final int bcWidth = 440;
    static final int bcHeight = 276;
    static final int NULL = -1;
    static final int PLAYER = 0;
    static final int COMPUTER = 1;
    static final int HOUND = 1;
    static final int HARE = 2;
    protected Color BG_COLOR;
    protected Color BOARD_BG_COLOR;
    protected String sTITLE;
    protected String sNEW_GAME;
    protected String sPLAYER;
    protected String sHOUNDS;
    protected String sHARE;
    protected String sLEVEL;
    protected String sBEGINNER;
    protected String sINTERMED;
    protected String sEXPERT;
    protected String sSCORE;
    protected String s2PLAYER;
    protected String sCOMPUTER;
    protected String sFIRST_MOVE;
    protected String sSTALLING;
    protected String sHARE_WINS;
    protected String sHOUNDS_WIN;
    private int playerAnimal;
    private int computerAnimal;
    private int playerScore;
    private int computerScore;
    private int startSq;
    private int endSq;
    private boolean gameOver;
    private Label lTitle;
    private TextField tfStatus;
    private Board bd;
    private BoardCanvas bc;
    private ControlPanel cp;
    private Image boardImage;
    private Image gifletsImage;
    private AI hnh;

    static
    {
        DF_BG_COLOR = Color.white;
    }
}
import java.awt.*;

final class ControlPanel extends Panel
{

    ControlPanel(Hounds hounds)
    {
        main = hounds;
        setLayout(new GridLayout(9, 1, 0, 4));
        setBackground(Color.lightGray);
        setFont(textFont);
        add(bNewGame = new Button(hounds.sNEW_GAME));
        add(new Label(hounds.sPLAYER, 1));
        cbgAnimal = new CheckboxGroup();
        Panel panel = new Panel();
        panel.setLayout(new FlowLayout(0, 10, 0));
        panel.add(cbHounds = new Checkbox(hounds.sHOUNDS, cbgAnimal, true));
        add(panel);
        Panel panel1 = new Panel();
        panel1.setLayout(new FlowLayout(0, 10, 0));
        panel1.add(cbHare = new Checkbox(hounds.sHARE, cbgAnimal, false));
        add(panel1);
        add(new Label(hounds.sLEVEL, 1));
        add(cLevel = new Choice());
        cLevel.addItem(hounds.sBEGINNER);
        cLevel.addItem(hounds.sINTERMED);
        cLevel.addItem(hounds.sEXPERT);
        cLevel.select(hounds.sBEGINNER);
        add(new Label(hounds.sSCORE, 1));
        Panel panel2 = new Panel();
        panel2.setLayout(new FlowLayout(2, 3, 0));
        panel2.add(new Label(hounds.s2PLAYER, 1));
        panel2.add(tfPlayerScore = new TextField(2));
        tfPlayerScore.setFont(monoFont);
        add(panel2);
        Panel panel3 = new Panel();
        panel3.setLayout(new FlowLayout(2, 3, 0));
        panel3.add(new Label(hounds.sCOMPUTER, 1));
        panel3.add(tfComputerScore = new TextField(2));
        tfComputerScore.setFont(monoFont);
        add(panel3);
        validate();
    }

    public boolean action(Event event, Object obj)
    {
        if(main.sNEW_GAME.equals(obj))
            main.newGame();
        else
        if(event.target == cbHounds)
            main.setPlayerAnimal(1);
        else
        if(event.target == cbHare)
            main.setPlayerAnimal(2);
        else
            main.setFocus();
        return true;
    }

    int getLevel()
    {
        switch(cLevel.getSelectedIndex())
        {
        case 1: // '\001'
            return 2;

        case 2: // '\002'
            return 3;
        }
        return 1;
    }

    void setComputerScore(int i)
    {
        String s = Integer.toString(i);
        switch(s.length())
        {
        case 1: // '\001'
            s = "  " + s;
            break;

        case 2: // '\002'
            s = " " + s;
            break;
        }
        tfComputerScore.setText(s);
    }

    void setPlayerScore(int i)
    {
        String s = Integer.toString(i);
        switch(s.length())
        {
        case 1: // '\001'
            s = "  " + s;
            break;

        case 2: // '\002'
            s = " " + s;
            break;
        }
        tfPlayerScore.setText(s);
    }

    static final int LEVEL1 = 1;
    static final int LEVEL2 = 2;
    static final int LEVEL3 = 3;
    static final Font titleFont = new Font("SansSerif", 1, 12);
    static final Font textFont = new Font("SansSerif", 0, 12);
    static final Font monoFont = new Font("Courier", 1, 12);
    static final Font statusFont = new Font("Dialog", 0, 12);
    private Label lPlayer;
    private Label lScore;
    private Label lPlayer2;
    private Label lComputer;
    private Button bNewGame;
    private CheckboxGroup cbgAnimal;
    private Checkbox cbHounds;
    private Checkbox cbHare;
    private Choice cLevel;
    private TextField tfPlayerScore;
    private TextField tfComputerScore;
    private Hounds main;

}

Recommended Answers

All 3 Replies

There are two standard printed references on Swing. Both are in the range of 900 pages long and explain Swing quite thoroughly. If you can't get ahold of one of those, there are many on-line tutorials (including ones released by Sun and those made by third-party writers). Sun's on-line documentation of the Swing classes is really quite good.
If you're not going to even TRY to come up with an answer, how can you expect anyone to want to help you out?

There are two standard printed references on Swing. Both are in the range of 900 pages long and explain Swing quite thoroughly. If you can't get ahold of one of those, there are many on-line tutorials (including ones released by Sun and those made by third-party writers). Sun's on-line documentation of the Swing classes is really quite good.
If you're not going to even TRY to come up with an answer, how can you expect anyone to want to help you out?

which should be changed? the first, second or everything? thank

Um... you're not tracking me very well, are you? Yes, you'd want to change a lot of stuff, depending on what Swing capabilities you wanted to make use of. But unless you try to understand what Swing does, and how it sits on top of the AWT (which I don't fully understand yet, myself) all you're doing is asking people to write code for you for free. Most of us have plenty of stuff to write, we're here to help you understand, not to do your work for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.