Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

repaint

Options
  • 21-01-2002 9:45pm
    #1
    Closed Accounts Posts: 8,478 ✭✭✭


    repaint !!, hears the prob. Ive goton some help already here about the hangman project im working on.

    ive got it working [more or less], which is nice, but id like to add a timer, that gives the user a certain amount of time to get the right number. text in bold is the timer stuff ive added

    CODE :

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.awt.Graphics.*;


    public class guessgame extends Applet implements ActionListener, Runnable
    {
    Label enter;
    TextField box;
    int number;//number user enters
    double randomnumber;//ra ndom number
    int displaynum;//diplays random number
    Font standard;
    Font special;
    Font comment;
    int tries;//number of tries allowed
    Polygon beard; //for another method im working on
    int counter; Thread cd;

    public void init()
    {
    enter = new Label("Guess a Number and Press Enter");
    box = new TextField(10);
    standard = new Font("Arial",Font.PLAIN, 14);
    special = new Font("Arial",Font.BOLD, 13);
    comment = new Font("Arial",Font.BOLD,15);
    add(enter);
    add(box);
    tries=-1;
    number=20;
    box.addActionListener(this);
    randomnumber = Math.random() * 10;
    beard = new Polygon();//other method
    beard.addPoint(111,165);
    beard.addPoint(140,165);
    beard.addPoint(125,183);
    }

    public void start()
    { // create thread
    counter = 60; cd = new Thread(this);
    cd.start();
    }
    public void stop()
    {
    cd = null;
    }

    public void run()
    { // executed by Thread
    while (counter>0 && cd!=null)
    {
    try{Thread.sleep(1000);} catch (InterruptedException e){}
    --counter; repaint(); //update screen
    }
    }


    public void man(Graphics g)
    {
    if (tries ==1)
    {
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(115,155,07,07,07,07);//glassleft
    g.drawRoundRect(127,155,07,07,07,07);//glassright
    }
    if (tries ==2)
    {
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(115,155,07,07,07,07);//glassleft
    g.drawRoundRect(127,155,07,07,07,07);//glassright
    g.drawLine(125,200,125,175);//body
    }
    if (tries ==3)
    {
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(115,155,07,07,07,07);//glassleft
    g.drawRoundRect(127,155,07,07,07,07);//glassright
    g.drawLine(125,200,125,175);//body
    g.drawLine(105,195,125,180);//left arm
    }
    if (tries ==4)
    {
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(115,155,07,07,07,07);//glassleft
    g.drawRoundRect(127,155,07,07,07,07);//glassright
    g.drawLine(125,200,125,175);//body
    g.drawLine(105,195,125,180);//left arm
    g.drawLine(145,195,125,180);//right arm
    }
    if (tries ==5)
    {
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(115,155,07,07,07,07);//glassleft
    g.drawRoundRect(127,155,07,07,07,07);//glassright
    g.drawLine(125,200,125,175);//body
    g.drawLine(105,195,125,180);//left arm
    g.drawLine(145,195,125,180);//right arm
    g.drawLine(105,225,125,200);//left leg
    }
    if (tries ==6)
    {
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(110,145,30,30,30,30);//head
    g.drawRoundRect(115,155,07,07,07,07);//glassleft
    g.drawRoundRect(127,155,07,07,07,07);//glassright
    g.drawLine(125,200,125,175);//body
    g.drawLine(105,195,125,180);//left arm
    g.drawLine(145,195,125,180);//right arm
    g.drawLine(105,225,125,200);//left leg
    g.drawLine(145,225,125,200);//right leg
    }
    }

    public void paint(Graphics g)
    {
    g.drawString(String.valueOf(counter),200,75);

    g.setFont(special);
    if (tries!=-1)
    {
    g.drawString("Your Guess : " +number, 30,85);
    }

    g.drawLine(80,135,80,235);//vertical thrust
    g.drawLine(80,135,125,135);//horizontal thrust
    g.drawLine(125,135,125,145);//rope

    if (number!=randomnumber)
    {
    tries++;
    man(g);
    }

    if ((tries >= 6)&&(number != displaynum))
    {
    g.setColor(Color.red);
    g.setFont(special);
    g.drawString("DEAD, well done !!", 100, 280);
    }

    if((number > randomnumber)&&(tries < 6)&&(tries != 0)&&(number != displaynum))
    {
    g.setFont(standard);
    g.drawString("Too High", 40, 100);
    }

    if((number < randomnumber)&&(tries < 6)&&(tries != 0)&&(number != displaynum))
    {
    g.setFont(standard);
    g.drawString("Too Low", 40, 100);
    }

    if(number == displaynum)
    {
    g.setColor(Color.red);
    g.setFont(special);
    g.drawString("Well done", 60, 280);
    }
    }

    public void actionPerformed(ActionEvent e)
    {
    number = Integer.parseInt(e.getActionCommand());
    displaynum = (int)randomnumber;
    box.setText("");
    showStatus("psst!, answer is : "+displaynum+" You didnt hear it from me, right?");
    repaint();
    }
    }

    the problem is the repaints. one for the counter, one for the hangman himself. the two conflict. how can i fix this ? or will i be forced to use images, which is bad as its amazing ive gotton this far :) the timer itself works fine, its the repaint. i tried java.sun.com and the link enygma gave me , but could not get that to work :(

    help ?


Comments

  • Moderators, Music Moderators Posts: 1,481 Mod ✭✭✭✭satchmo


    I think your problem is here:

    if (number!=randomnumber)
    {
    tries++;
    man(g);
    }

    in the paint method. Every time the applet is redrawn, tries will be incremented, regardless of whether the user has tried or not (unless of course they picked the right number).

    You need increment tries in the actionPerformed method instead.


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    I agree with Jazz on that one.

    If you are writing a game, you should try and separate the game's logic from the display (what the user see's). Think of it this way....

    The game consists of many variables (ie. tries etc), in your case depending on what value tries is at, will determine what exactly is going to be drawn to the applet canvas. You shouldn't have any functional game logic in the paint method. Paint is only used to draw the display. Paint should simply check the value of tries (which you have it doing), draw the display and nothing more.

    On the subject of your paint method, you call a method called man(). In this method I noticed that your if blocks are a lot larger than they could be, hence making your code more complicated, and worth less marks in a college situation. Have you noticed that you are constantly repeating code throughout.

    What I recommend you do is
    if(tries >= 1){
    g.drawRoundRect(110,145,30,30,30,30);//head 
    g.drawRoundRect(115,155,07,07,07,07);//glassleft 
    g.drawRoundRect(127,155,07,07,07,07);//glassright
    g.drawRoundRect(127,155,07,07,07,07);//glassright 
    }
    if(tries >= 2){
    g.drawLine(125,200,125,175);//body 
    }
    if(tries >= 3){
    g.drawLine(105,195,125,180);//left arm 
    }
    if(tries >= 4){
    g.drawLine(145,195,125,180);//right arm 
    }
    if(tries >= 5){
    g.drawLine(105,225,125,200);//left leg 
    }
    if(tries >= 6){
    g.drawLine(145,225,125,200);//right leg 
    }
    

    Hope that helps, and gets you a few more marks ;)

    ;-phobos-)


Advertisement