Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Screen won't update.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Reality Check




PostPosted: Thu Mar 08, 2007 8:33 pm   Post subject: Screen won't update.

I'm fairly new to Java and I'm making a guessing game for an assignment. Here is my code:

code:

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.lang.*;

public class AriansGuessingGame extends Frame
{
    int check = 0, rand = (int) ((Math.random () * 100) + 1);
    double guessedNum = 0;
    public AriansGuessingGame ()
    {
        super ("Arian's Guessing Game!");
        setSize (600, 600);
        setLocation (200, 50);
        show ();
    }


    public boolean mouseDown (Event button, int x, int y)
    {
        if (x < 404 && y < 430)
        {
            y -= 30;
            x -= 4;
            guessedNum = ((int) ((x / 40) + 1)) + (((int) ((y / 40)) * 10));
            if (guessedNum > rand)
            {
                check = 1;
            }
        }
        return true;
    }


    // Handles the close button on the window
    public boolean handleEvent (Event evt)
    {
        if (evt.id == Event.WINDOW_DESTROY)
        {
            hide ();
            System.exit (0);
            return true;
        }


        // If not handled, pass the event along
        return super.handleEvent (evt);
    }


    public void paint (Graphics g)
    {
        int x;
        boolean colour = true;
        int num = 0;
        String number;
        for (int b = 0 ; b < 10 ; b += 1) // This is to alternate the y.
        {
            if (b % 2 == 0)
                colour = false;
            else
                colour = true;
            for (int i = 0 ; i < 10 ; i += 1) // This is to alternate the x
            {
                num += 1;
                number = Integer.toString (num);
                if (colour == true)
                {
                    g.setColor (Color.blue); // sets colour to black if the variable is true
                    colour = false; // sets it to false so it enters the other 'if' statement upon the next loop
                }
                else
                {
                    g.setColor (Color.green); // sets colour to white if the varialbe is true
                    colour = true; //sets it to true so it enters the other 'if' statement upon the next loop
                }
                g.fillRect (i * 40 + 4, b * 40 + 30, 40, 40); //Draws the squares
                g.setColor (Color.black);
                g.drawString (number, i * 40 + 16, b * 40 + 52);

            }
        }
        g.setColor (Color.black);
        g.drawRect (0, 0, 404, 430);
        if (check == 1)
        {
            g.drawString ("LOWER!", 10, 500);
        }
    }

    public static void main (String[] args)
    {
        new AriansGuessingGame ();
    }
}


Now, the user clicks a box and I tell them if the number is higher or lower. I've only implemented the "lower" part but the screen only outputs lower if I minimize and restore again. Can someone help me fix this?
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Fri Mar 09, 2007 12:52 am   Post subject: Re: Screen won't update.

The curly brackets, in Java, should not have a newline in front of them and should be next to the statement or declaration.

ex.
Java:
public class AriansGuessingGame extends Frame
{

should be
Java:
public class AriansGuessingGame extends Frame {

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html is a very useful page on Java code conventions.



Java:
int check = 0, rand = (int) ((Math.random () * 100) + 1);
double guessedNum = 0;

tisk, tisk, tisk... Instance variable should be assigned a value in constructors. Trust me, it will make your program a lot easier to work with.



Java:
import java.lang.*;

Are you sure this line is required? I thought java.lang.* was automatically imported.


Anyway, about the error. I haven't run your program but I can state a few solutions. Is the paint method being called when you want it to? Are you sure you aren't mixing up the colours? Are you sure you are drawing in the correct order (ie. foreground goes above background)? It does not look like you are using a double buffer, but if you are, are you sure you are drawing the buffered image at the correct time?
Reality Check




PostPosted: Fri Mar 09, 2007 10:44 am   Post subject: Re: Screen won't update.

Thanks a lot for the suggestions. Like I said, I've only been doing Java for a couple of weeks. In terms of the curly brackets, my teacher insists that they be put under and not next to it because she says its easier to read but I did know that the convention was to put them next to it. I did get it to fix though. Every time the mouse is clicked (in the mouseDown event) I simply put in "repaint ();" at the end of it right before my return statement.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: