HI I have been building a game applet. I have coded as far as getting rectangles to move on a timer and put integers into the rectangles. I am not familar with how to click the rectangles and I also want to get the different numbers going through an ArrayList as its a math game and there can only be one correct answer.

This is my code. I have an applet class and a square class.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package passignment.pkg2;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.Timer;

/**
 *
 * @author camgray
 */
public class Space extends JApplet implements ActionListener {

    Square Square1;
    Square Square2;
    Square Square3;
    Square Square4;
    Square Square5;

    ImageIcon Space;

    int number;
    int answersArray[];
    Timer t = new Timer(15,this);

    public void init() {


        setLayout(new FlowLayout());
        setSize(1030, 600);
        Space = new ImageIcon(getImage(getDocumentBase(), "Space.jpg"));
        Square1 = new Square(100, 100, 2, 2, true,10,10);
        Square2 = new Square(100, 200, 3, 3, true,10,10);
        Square3 = new Square(100, 300, 4, 4, true,10,10);
        Square4 = new Square(100, 400, 5, 5, true,10,10);
        Square5 = new Square(100, 500, 6, 6, true,10,10);
    }

//    public void checkAnswer(MouseEvent e)
//    {
//        for(Square s : Squares)
//        {
//            if(((e.getX()>=s.x)&&(e.getX()<(s.x+s.number)))
//            &&((e.getY()>=s.y)&&(e.getY()<(s.y+s.number))))
//            {
//                if (s.hasBeenClicked.correct)
//                {
//                    QuestionIndex++;
//                    if (firstTry)
//                    {
//                        score += bonus;
//                    }
//                    score++;
//                    firstTry = true;
//                    NextQuestion(QuestionIndex);
//                }
//                else
//                {
//                    firstTry = false;
//                }
//            }
//        }
//    }


    public void paint(Graphics g) {
///      super.paint(g);
        g.drawImage(Space.getImage(), 0, 0, this.getWidth(), this.getHeight(), this);



        Square1.draw(g);
        Square2.draw(g);
        Square3.draw(g);
        Square4.draw(g);
        Square5.draw(g);

        t.start();
    }

    public void Move() {


        Square1.move();
        Square2.move();
        Square3.move();
        Square4.move();
        Square5.move();
        repaint();
    }

    // TODO overwrite start(), stop() and destroy() methods
    @Override
    public void actionPerformed(ActionEvent ae) {


        Square1.move();
        Move();
        Square1.Question1();



    }
}

This is the space class.

*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package passignment.pkg2;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.Timer;

/**
 *
 * @author camgray
 */
public class Square {

    private int x, x1, x2, x3, x4;
    private int y;
    private int velX;
    private int velY;
    private int number;
    private int answers;
    private boolean hasBeenClicked;
    ImageIcon spaceShip;

    List<Answer> possibleAnswers;



    public Square(int x, int y, int velX, int velY, boolean hasBeenClicked, int number, int answers) {
        this.x = x;
        this.y = y;
        this.velX = velX;
        this.hasBeenClicked = hasBeenClicked;
        this.number = number;
        this.answers = answers;
    }



    public void draw(Graphics gShape) {

        gShape.setColor(Color.red);
        gShape.fillRect(x, y, 65, 65);
        gShape.setColor(Color.yellow);
        gShape.setFont(new Font("default", Font.BOLD, 22));
        gShape.drawString(Integer.toString(number),x + 20, y + 40);
        gShape.setColor(Color.white);
        gShape.fillRect(300,0, 400, 65);





    }

    public void move() {

        if ((x < 0 || x > 960)) {
            velX = -velX;

        }
        x += velX;
    }

    public void Question1()
    {
       this.possibleAnswers = new ArrayList();

       Answer A1 = new Answer(true,3);
       Answer A2 = new Answer(false,8);
       Answer A3 = new Answer(false,4);
       Answer A4 = new Answer(false,5);
       Answer A5 = new Answer(false,7);

       possibleAnswers.add(A2);
       possibleAnswers.add(A2);
       possibleAnswers.add(A3);
       possibleAnswers.add(A4);
       possibleAnswers.add(A5);

    }

    public int getAnswers() {
        return answers;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getX() {
        return x;
    }

    public int number() {
        return number;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getVelX() {
        return velX;
    }

    public void setVelX(int velX) {
        this.velX = velX;
    }

    public int getVelY() {
        return velY;
    }

    public void setVelY(int velY) {
        this.velY = velY;
    }


    public boolean isHasBeenClicked() {
        return hasBeenClicked;
    }

    public void setHasBeenClicked(boolean hasBeenClicked) {
        this.hasBeenClicked = hasBeenClicked;
    }

    public ImageIcon getSpaceShip() {
        return spaceShip;
    }

    public void setSpaceShip(ImageIcon spaceShip) {
        this.spaceShip = spaceShip;
    }
}

For clicking the rectangles you will want to take a look at the MouseListener, MouseMotionListener and the Rectangle class (Rectangle class isn't necessary, but easier).

MouseListener: http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
MouseMotionListener: http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html
Rectangle: http://docs.oracle.com/javase/6/docs/api/java/awt/Rectangle.html

As for the second part of your question. How are the numbers "scrolling" through the array list?
What numbers are in the array list?

Just more information on what you want to achieve with the numbers in the arraylist and how this math game works would be beneficial.

Edited 3 Years Ago by sirlink99

I just want the numbers to be different on each rectangle, so that when i get the correct answer it changes the numbers to the next question...but I would have no idea how to do it. :(

Try storing the displayed number in the Square and drawing that. When the correct answer is entered, then change that number, which will automatically update the number in the square.

Please give as much information as possible. We are not mindreaders. Explain what is happening, what the screen looks like, what you wish to achieve, commented code is preferrable. If this answer doesn't help you please post your Answer class so that I can get a feel as to how your program works.

Okay. If you put my code into netbeans or a java program with the applet class and the square class. you can see what my code does. I just have 5 rectangles moving in the applet bouncing from one wall to the other. I have them all hard-coded the number 10 but I want them to have different numbers. I want to be able to click on one number(the correct one) and then change all the numbers on the rectangles and load a new equation at the top of the applet. I have an answer class but I was mucking around. It doesnt do anything in my code.

The way I would go about doing what you want is storing a number in each square and printing in in the square class. In the class where you have your mouselistener make a loadNextQuestion method. This method will change the question and the answers. It will go through each of the squares and update the number in them. You also need to store the correct answer somewhere. I noticed that if you have the super.paintComponents(g). The graphics flicker, and if you gon't they keep painting over. I will see if I can find a solution to this problem.

Okay thankyou.. I am just learning at the moment..Im very average at java. Don't no how I would go about doing a change question method but ill have a play around.

This article has been dead for over six months. Start a new discussion instead.