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;
}
}