Can someone help me with this Error that I get. When I try to pass in the image it gives me Error. This is part of my project, I completed the rest but have problem with the main screen.
Here is the Error:
Project.java:36: cannot find symbol
symbol : method drawImage(java.awt.Image)
location: class java.awt.Graphics
g.drawImage(img);
^
1 error
----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
And this the full Program:
import javax.imageio.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Project extends JPanel
implements KeyListener, MouseListener, MouseMotionListener {
char shape = 'r';
int x = 0;
int y = 0;
Image img;
boolean start = false;
boolean help = false;
boolean Player1 = false;
boolean Player2 = false;
public Project() {
img = Toolkit.getDefaultToolkit().getImage("mp.jpg");
setFocusable(true);
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
setSize(400, 400);
}
public void paintComponent(Graphics g) {
// BG
Dimension d = getSize();
g.setColor(getBackground());
// IMAGE ************************************* ERROR
g.drawImage(img);
g.setColor(Color.black);
// Help
g.setFont(new Font("default", Font.BOLD, 12));
if (x >= 900 && x <= 950 && y >= 600 && y <= 650 && start == false) {
g.drawString("Press START to", 960, 620);
g.drawString("start the game.", 960, 635);
}
if (x >= 900 && x <= 950 && y >= 600 && y <= 650 && start) {
g.drawString("Choose a Player", 960, 620);
}
else {
g.setColor(Color.red);
g.fillOval(900, 600, 50, 50);
g.setFont(new Font("default", Font.BOLD, 45));
g.setColor(Color.cyan);
g.drawString("?", 915, 640);
}
// Help
g.setColor(Color.black);
g.fillRect(550, 555, 97, 50);
g.setFont(new Font("default", Font.BOLD, 30));
g.setColor(Color.cyan);
g.drawString("Start", 559, 590);
// Start
if (start) {
g.setColor(Color.black);
g.fillRect(400, 550, 400, 55);
g.setFont(new Font("default", Font.BOLD, 30));
g.setColor(Color.yellow);
g.drawString("PLayer 1", 425, 585);
g.drawString("Player 2", 645, 585);
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
shape = e.getKeyChar();
repaint();
}
public void keyReleased(KeyEvent e) {
}
public void mouseEntered(MouseEvent e) {
if (x >= 800 && x <= 850 && y >= 600 && y <= 650) {
help = true;
}
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {// ****** Players Button *******
// Variables
Player1 = false;
Player2 = false;
// Levels
if (start) {
if (x >= 427 && x <= 555 && y >= 564 && y <= 589) {
Player1 = true;
}
if (x >= 648 && x <= 769 && y >= 564 && y <= 587) {
Player2 = true;
}
}
if (x >= 550 && x <= 650 && y >= 560 && y <= 607) {
start = true;
}
else {
start = false;
}
}
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
public static void main(String args[]) {
JFrame f = new JFrame("Project");
Project dc = new Project();
f.getContentPane().add(dc);
f.setSize(1200, 775);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}
Take a look at the API for Graphics: http://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html
That class does not contain a method that takes just an image. You probably want something closer to this:
g.drawImage(image, x, y, this);
Where image is the image to be drawn, x and y are the position, and this is the JPanel being drawn in, which is an ImageObserver.
There are other drawImage() methods in the Graphics class that take other parameters as well.
Just use:
g.drawImage(img, 0, 0, null);
Numbers are for (x, y) coordinates you'd like to put your image at and keep last as null (it's for slow sources like internet).
drawImage should contain four arguments.
drawImage(Image, int, int, ImageObserver) // i dont know, why you are passing one argument to this.
Change this line :
From:
g.drawImage(img);
To:
g.drawImage(img,0,0,null);
It works
Related
Here is the code which creates the bezier curve with total 4 control points:
Blue color is the starting and end control point.
Cyan color is the second and third control point.
I want to add a control point to the existing curve so that now I have 3 cyan coloured control points so that I can resize the curve(like dragging the curve) from any of these 3 control points instead of the 2 control points. What needs to be done for this?
I have attached the output of the below code as an attachment.
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.GeneralPath;
public class Piyush extends Frame implements MouseListener, MouseMotionListener {
// private int[] xs = { 75, 150, 300, 375 };
//
// private int[] ys = { 250, 100, 350, 250 };
private int[] xs = { 75, 200, 300, 375 };
private int[] ys = { 250, 100, 100, 250 };
private int dragIndex = NOT_DRAGGING;
private final static int NEIGHBORHOOD = 15;
private final static int NOT_DRAGGING = -1;
public static void main(String[] args) {
new Piyush();
}
public Piyush() {
setSize(500, 450);
addMouseListener(this);
addMouseMotionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g) {
for (int i = 0; i < 4; i++) {
if (i == 0 || i == 3){
g.setColor(Color.blue);
g.fillOval(xs[i] - 6, ys[i] - 6, 12, 12);}
else{
g.setColor(Color.cyan);
g.fillOval(xs[i] - 6, ys[i] - 6, 12, 12);
}
}
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
GeneralPath path = new GeneralPath();
path.moveTo(xs[0], ys[0]);
path.curveTo(xs[1], ys[1], xs[2], ys[2], xs[3], ys[3]);
g2d.draw(path);
}
public void mousePressed(MouseEvent e) {
dragIndex = NOT_DRAGGING;
int minDistance = Integer.MAX_VALUE;
int indexOfClosestPoint = -1;
for (int i = 0; i < 4; i++) {
int deltaX = xs[i] - e.getX();
int deltaY = ys[i] - e.getY();
int distance = (int) (Math.sqrt(deltaX * deltaX + deltaY * deltaY));
if (distance < minDistance) {
minDistance = distance;
indexOfClosestPoint = i;
}
}
if (minDistance > NEIGHBORHOOD)
return;
dragIndex = indexOfClosestPoint;
}
public void mouseReleased(MouseEvent e) {
if (dragIndex == NOT_DRAGGING)
return;
xs[dragIndex] = e.getX();
ys[dragIndex] = e.getY();
dragIndex = NOT_DRAGGING;
repaint();
}
public void mouseDragged(MouseEvent e) {
if (dragIndex == NOT_DRAGGING)
return;
xs[dragIndex] = e.getX();
ys[dragIndex] = e.getY();
repaint();
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
}
Also asked here
http://www.dreamincode.net/forums/topic/408270-need-help-in-adding-a-control-point-for-bezier-curve/page__pid__2347742__st__0&#entry2347742
code base comes from here
http://www.java2s.com/Tutorials/Java/Graphics/Shape/Drag_the_control_point_for_Bezier_curve_in_Java.htm
I'm currently coding a simple game of brickbreaker, however I've come across a huge roadblock. The collisions I have declared are going off on the wrong bricks sometimes, also there is a lot of internal bouncing of the ball. On top of that the loop that draws the non hit blocks goes out of bounds but no matter how much I've tried tinkering with it I have not been able to fix it. Any help would be extremely appreciated. Thank you in advance.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class brickBreakerApplet extends Applet implements Runnable, KeyListener, MouseListener, MouseMotionListener
{
private int speed;
int currentLine;
int x_pos = 30;
int y_pos = 100;
int x_speed = 1;
int y_speed = 1;
int radius = 5;
int appletsize_x = 400;
int appletsize_y = 300;
int mousex=0;
int mousey=0;
int score=0;
int life=3;
int counter=0;
int[] bricks;
Boolean game=false;
Image background;
private Image dbImage;
private Graphics dbg;
public void init()
{
setBackground (Color.blue);
background = getImage(getCodeBase(), "background.gif");
bricks=new int[50];
for (int x=0; x<50; x++){
bricks[x]=1;
}
currentLine = 10;
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void start ()
{
Thread th = new Thread (this);
th.start ();
}
public void stop()
{
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyChar() == 'r'){
x_pos=0;
y_pos=0;
x_speed=1;
y_speed=1;
}
currentLine+=20;
}
public void keyReleased(KeyEvent e)
{
// System.out.println("User released key " + e.getKeyChar());
// currentLine+=20;
}
public void keyTyped(KeyEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
// System.out.println("User clicked mouse " + e.getClickCount() + " times!");
// currentLine+=20;
}
public void mouseEntered(MouseEvent e)
{
// System.out.println("Mouse entered applet at " + e.getX() + " " + e.getY());
// currentLine += 20;
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
if (game==false){
x_speed=1;
y_speed=-1;
game=true;
}
if (life==0){
x_pos=0;
y_pos=0;
x_speed=1;
y_speed=1;
life=3;
score=0;
game=false;
}
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
mousex=e.getX();
mousey=e.getY();
}
public void mouseDragged(MouseEvent e)
{
}
public void destroy()
{
}
public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
if (x_pos > appletsize_x - radius)
{
if (x_speed==1){
x_speed = -1;
}
if (x_speed==2){
x_speed = -2;
}
}
else if (x_pos < radius)
{
if (x_speed==-1){
x_speed = +1;
}
if (x_speed==-2){
x_speed= 2;
}
}
else if (y_pos < radius)
{
y_speed = +1;
}
else if (y_pos > appletsize_y - radius)
{
death();
}
if (game==true){
x_pos += x_speed;
y_pos += y_speed;
//HITTING TWICE, LOTS OF ERRORS WHILE RUNNING CODE, ASK SIR FOR ASSISTANCE TOMORROW. LOOKS PRETTY GOOD THOUGH.
counter=0;
for (int y=0; y<5; y++){
for (int x=0; x<10; x++){
if (y_pos-(radius)==(40+y*10) &&((x_pos+(radius)>=(x*40) && (x_pos+radius<= 40+(x*40)))) && bricks[counter]==1 && y_speed==-1){
bricks[counter]=0;
score++;
y_speed=1;
}
counter++;
}
}
counter=0;
for (int y=0; y<5; y++){
for (int x=0; x<10; x++){
if (y_pos+(radius)==(30+y*10) && ((x_pos+(radius)>=(x*40) && (x_pos+radius<= 40+(x*40)))) && bricks[counter]==1 && y_speed==1){
bricks[counter]=0;
score++;
y_speed=-1;
}
counter++;
}
}
counter=0;
for (int y=0; y<5; y++){
for (int x=0; x<10; x++){
if (x_pos+radius==(x*40) && ((y_pos>=(30+(y*10)) && (y_pos<= 40+(y*10)))) && bricks[counter]==1){
bricks[counter]=0;
score++;
x_speed=-1;
}
counter++;
}
}
counter=0;
for (int y=0; y<5; y++){
for (int x=0; x<10; x++){
if (x_pos-radius==(40+(x*40)) && ((y_pos>=(30+(y*10)) && (y_pos<= 40+(y*10)))) && bricks[counter]==1){
bricks[counter]=0;
score++;
x_speed=1;
}
counter++;
}
}
if (y_pos >= 280-(radius))
{
{
if ((x_pos+(radius) >= mousex-30) && (x_pos+(radius) <= mousex-20)){
score++;
difficulty();
x_speed=-2;
java.awt.Toolkit.getDefaultToolkit().beep();
}
if ((x_pos+(radius) >= mousex-20) && (x_pos+(radius) <= mousex)){
score++;
difficulty();
x_speed=-1;
java.awt.Toolkit.getDefaultToolkit().beep();
}
if ((x_pos+(radius) >= mousex) && (x_pos+(radius) <= mousex+20)){
score++;
difficulty();
x_speed=1;
java.awt.Toolkit.getDefaultToolkit().beep();
}
if ((x_pos+(radius) >= mousex+20) && (x_pos+(radius) <= mousex+30)){
score++;
difficulty();
x_speed=2;
java.awt.Toolkit.getDefaultToolkit().beep();
}
}
}
}
if (game==false){
x_pos=mousex;
y_pos= 279-radius;
}
repaint();
try
{
Thread.sleep (5);
}
catch (InterruptedException ex)
{
// do nothing
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void update (Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
dbg.setColor (getForeground());
paint (dbg);
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
if (life>0){
g.drawImage(background,0,0,this);
g.setColor (Color.cyan);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
g.setColor (Color.blue);
g.fillRect (mousex-30, 280, 60, 5);
g.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
g.setColor (Color.cyan);
g.drawString ("Score: "+score, 5,20);
g.drawString ("Life: "+life, 330,20);
counter=0;
for (int y=0; y<5; y++){
for (int x=0; x<10; x++){
if (bricks[counter]==1){
g.setColor(Color.blue);
g.fillRect(x*40, 30+(y*10),40,10);
g.setColor(Color.black);
g.drawRect(x*40, 30+(y*10),40,10);
}
counter++;
}
}
}
if (life==0){
g.setColor (Color.black);
g.fillRect (0,0,400,400);
g.setColor (Color.white);
g.setFont (new Font("Times New Roman", Font.BOLD, 48));
g.drawString ("GAME OVER", 50, 150);
g.setFont (new Font("Times New Roman", Font.PLAIN, 12));
g.drawString ("To Try Again, Press Screen", 130, 160);
g.drawString ("Your Score Was: "+score, 155, 170);
}
}
public void death ()
{
game=false;
y_pos= 279-radius;
life--;
}
public void difficulty()
{
y_speed=-1;
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
public class FinalFlappy implements ActionListener, MouseListener,
KeyListener
{
public static FinalFlappy finalFlappy;
public final int WIDTH = 800, HEIGHT = 800;
public FinalFlappyRend renderer;
public Rectangle bee;
public ArrayList<Rectangle> rect_column;
public int push, yMotion, score;
public boolean gameOver, started;
public Random rand;
public FinalFlappy()
{
JFrame jframe = new JFrame();
Timer timer = new Timer(16, this);
renderer = new FinalFlappyRend();
rand = new Random();
jframe.add(renderer);
jframe.setTitle("Flappy Bee");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(WIDTH, HEIGHT);
jframe.addMouseListener(this);
jframe.addKeyListener(this);
jframe.setResizable(false);
jframe.setVisible(true);
bee = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 40, 40);
rect_column = new ArrayList<Rectangle>();
addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
timer.start();
}
public void addColumn(boolean start)
{
int space = 300;
int width = 60;
int height = 50 + rand.nextInt(300);
if (start)
{
rect_column.add(new Rectangle(WIDTH + width + rect_column.size() * 300, HEIGHT - height - 120, width, height));
rect_column.add(new Rectangle(WIDTH + width + (rect_column.size() - 1) * 300, 0, width, HEIGHT - height - space));
}
else
{
rect_column.add(new Rectangle(rect_column.get(rect_column.size() - 1).x + 600, HEIGHT - height - 120, width, height));
rect_column.add(new Rectangle(rect_column.get(rect_column.size() - 1).x, 0, width, HEIGHT - height - space));
}
}
public void jump()
{
if (gameOver)
{
bee = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 40, 40);
rect_column.clear();
yMotion = 0;
score = 0;
addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
gameOver = false;
}
if (!started)
{
started = true;
}
else if (!gameOver)
{
if (yMotion > 0)
{
yMotion = 0;
}
yMotion -= 10;
}
}
#Override
public void actionPerformed(ActionEvent e)
{
int speed = 10;
push++;
if (started)
{
for (int i = 0; i < rect_column.size(); i++)
{
Rectangle column = rect_column.get(i);
column.x -= speed;
}
if (push % 2 == 0 && yMotion < 15)
{
yMotion += 2;
}
for (int i = 0; i < rect_column.size(); i++)
{
Rectangle column = rect_column.get(i);
if (column.x + column.width < 0)
{
rect_column.remove(column);
if (column.y == 0)
{
addColumn(false);
}
}
}
bee.y += yMotion;
for (Rectangle column : rect_column)
{
if (column.y == 0 && bee.x + bee.width / 2 > column.x + column.width / 2 - 10 && bee.x + bee.width / 2 < column.x + column.width / 2 + 10)
{
score++;
}
if (column.intersects(bee))
{
gameOver = true;
if (bee.x <= column.x)
{
bee.x = column.x - bee.width;
}
else
{
if (column.y != 0)
{
bee.y = column.y - bee.height;
}
else if (bee.y < column.height)
{
bee.y = column.height;
}
}
}
}
if (bee.y > HEIGHT - 120 || bee.y < 0)
{
gameOver = true;
}
if (bee.y + yMotion >= HEIGHT - 120)
{
bee.y = HEIGHT - 120 - bee.height;
gameOver = true;
}
}
renderer.repaint();
}
public void paintColumn(Graphics g, Rectangle column)
{
g.setColor(Color.green.darker());
g.fillRect(column.x, column.y, column.width, column.height);
g.fillRect(column.x-20, column.y+column.height-10, column.width+40, 10);
g.fillRect(column.x-20, column.y-10, column.width+40, 10);
}
public void repaint(Graphics g)
{
g.setColor(new Color(153,204,255));
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(new Color(255,255,255));
g.fillOval(50, 50, 100, 100);
g.setColor(Color.YELLOW);
g.fillOval(600, 50, 100, 100);
g.setColor(new Color(156,93,82));
g.fillRect(0, HEIGHT - 120, WIDTH, 120);
g.setColor(new Color(128,255,0));
g.fillRect(0, HEIGHT - 120, WIDTH, 20);
g.setColor(Color.YELLOW);
g.fillRect(bee.x, bee.y, bee.width, bee.height);
for (Rectangle column : rect_column)
{
paintColumn(g, column);
}
g.setColor(Color.white);
g.setFont(new Font("Times New Roman", 1, 100));
if (!started)
{
g.drawString("Push A to start", 100, HEIGHT / 2 - 50);
}
if (gameOver)
{
g.drawString("Game Over", 100, HEIGHT / 2 - 50);
g.drawString("A to replay", 100, HEIGHT / 2 + 90);
}
}
public static void main(String[] args)
{
finalFlappy = new FinalFlappy();
}
#Override
public void mouseClicked(MouseEvent e)
{
jump();
}
#Override
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_A)
{
jump();
}
}
#Override
public void mousePressed(MouseEvent e)
{
}
#Override
public void mouseReleased(MouseEvent e)
{
}
#Override
public void mouseEntered(MouseEvent e)
{
}
#Override
public void mouseExited(MouseEvent e)
{
}
#Override
public void keyTyped(KeyEvent e)
{
}
#Override
public void keyPressed(KeyEvent e)
{
}
}
import java.awt.Graphics;
import javax.swing.JPanel;
public class FinalFlappyRend extends JPanel
{
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
FinalFlappy.finalFlappy.repaint(g);
}
}
I am working on making a Flappy bird game and I am stuck on how to make and display a timer that updates every second onto the screen
How do I make it start as the game starts and end as the game over pops up?
There are a few ways you might achieve what you're asking. The important thing to remember is, any solution is going to have a degree of drift, meaning that it's unlikely to absolutely accurate, the degree of drift will depend on a lot of factors, so just beware.
You could use a Swing Timer
It's among the safest means for updating the UI on a regular basis, it's also useful if your main loop is already based on a Swing Timer
See How to Use Swing Timers for more details
You could...
Maintain some kind of counter within in your main loop. This assumes that you're using a separate thread (although you can do the same thing with a Swing Timer) and are simply looping at some consistent rate
long tick = System.nanoTime();
long lastUpdate = -1;
while (true) {
long diff = System.nanoTime() - tick;
long seconds = TimeUnit.SECONDS.convert(diff, TimeUnit.NANOSECONDS);
if (seconds != lastUpdate) {
lastUpdate = seconds;
updateTimerLabel(seconds);
}
Thread.sleep(100);
}
This basically runs a while-loop, which calculates the difference between a given point in time (tick) and now, if it's a "second" difference, it then updates the UI (rather than constantly updating the UI with the same value)
The updateTimerLabel method basically updates the label with the specified time, but does so in a manner which is thread safe
protected void updateTimerLabel(long seconds) {
if (EventQueue.isDispatchThread()) {
timerLabel.setText(Long.toString(seconds));
} else {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
updateTimerLabel(seconds);
}
});
}
}
To make and display a timer that updates every second, Put this code in your main class:
private Timer timer = new Timer();
private JLabel timeLabel = new JLabel(" ", JLabel.CENTER);
timer.schedule(new UpdateUITask(), 0, 1000);
private class UpdateUITask extends TimerTask {
int nSeconds = 0;
#Override
public void run() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
timeLabel.setText(String.valueOf(nSeconds++));
}
});
}
}
I am making a java sliding puzzle for my project, and i have managed to crop the images and making it move to a blank space, however i am still puzzled on how to know whether the user got the correct solution or not. i wanted to congratulate the user when he gets the correct pattern.
import java.applet.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class SlidePuzz extends Applet implements MouseMotionListener,MouseListener {
int gridDim = 3;
int animal = -1;
int pieceDim = 0;
int a; int b;
int gapX = -1; int gapY = -1;
int moveX = -1; int moveY = -1;
Image[][] pieces = new Image[7][7];
int[][] placement = new int[7][7];
String [] images = {"mouse","cat","dog"};
String stage = "grid";
Image img;
public void init() {
setSize(420, 420);
addMouseMotionListener(this);
addMouseListener(this);
}
public void paint(Graphics g) {
if(stage=="grid") {
g.setColor(Color.lightGray);
g.fillRect(0,0,60*gridDim,60*gridDim);
g.setColor(Color.white);
g.fillRect(60*gridDim,0,(7-gridDim)*60,420);
g.fillRect(0,60*gridDim,60*gridDim,(7-gridDim)*60);
g.setColor(Color.black);
for (int i=1; i<7; i++) {
g.drawLine(i*60,0,i*60,460);
g.drawLine(0,i*60,460,i*60);
}
g.setColor(Color.black);
g.fillRect(120,0,180,20);
g.setColor(Color.white);
g.drawString("click to choose "+gridDim+"x"+gridDim+" grid", 145, 15);
} else if (stage=="animal") {
g.setColor(Color.white);
g.fillRect(0,0,420,420);
g.setColor(Color.black);
g.drawString("Please click an animal...", 80, 100);
g.setFont(new Font("Courier New", Font.BOLD, 22));
g.drawString("Mouse", 80, 150);
g.drawString("Cat", 80, 200);
g.drawString("Dog", 80, 250);
} else if (stage=="game") {
if (gapX<0) {
g.setColor(Color.white);
g.fillRect(0,0,420,420);
g.setColor(Color.black);
g.setFont(new Font("Courier New", Font.PLAIN, 12));
g.drawString("Loading image...", 120, 50);
tr = new MediaTracker(this);
img = getImage(getCodeBase(), images[animal]+".jpg");
tr.addImage(img,0);
for(int x=0; x<gridDim; x++) {
for(int y=0; y<gridDim; y++) {
pieces[x][y] = createImage(new FilteredImageSource(img.getSource(),
new CropImageFilter(x*pieceDim, y*pieceDim, pieceDim, pieceDim)));
placement[x][y] = (x==gridDim-1 && y==gridDim-1) ? -2 : -1;
tr.addImage(pieces[x][y],0);
}
}
Random r = new Random();
Boolean placed;
for(int x=0; x<gridDim; x++) {
for(int y=0; y<gridDim; y++) {
// if its the gap, no need to draw anything
if (placement[x][y]==-2) break;
// keep looping until piece is selected thats not already drawn
do {
do {
a = r.nextInt(gridDim);
b = r.nextInt(gridDim);
} while (a==gridDim-1 && b==gridDim-1);
placed = false;
for(int c=0; c<gridDim; c++) {
for(int d=0; d<gridDim; d++) {
if(placement[c][d]==a*10+b) placed=true;
}
}
} while (placed);
// draw on the screen and record what's gone here
g.drawImage(pieces[a][b], x*pieceDim, y*pieceDim, this);
placement[x][y] = a*10+b;
}
}
// record where the gap is
gapX = gridDim-1; gapY = gridDim-1;
// a piece needs to be moved
} else if (moveX>=0){
g.setColor(Color.white);
g.fillRect(moveX*pieceDim, moveY*pieceDim, pieceDim, pieceDim);
b = placement[moveX][moveY]%10;
a = (placement[moveX][moveY]-b)/10;
g.drawImage(pieces[a][b], gapX*pieceDim, gapY*pieceDim, this);
placement[gapX][gapY] = a*10+b;
placement[moveX][moveY] = -2;
gapX = moveX; gapY = moveY;
moveX = -1; moveY = -1;
} else {
for(int x=0; x<gridDim; x++) {
for(int y=0; y<gridDim; y++) {
if (placement[x][y]==-2) continue;
b = placement[x][y]%10;
a = (placement[x][y]-b)/10;
g.drawImage(pieces[a][b], x*pieceDim, y*pieceDim, this);
}
}
}
}
}
public void mouseMoved(MouseEvent me) {
if(stage=="grid") {
int mousePos = me.getX()>me.getY() ? me.getX() : me.getY();
int oldGridSize = gridDim;
gridDim = (mousePos/60)+1;
if(gridDim<3) gridDim=3;
if (gridDim!=oldGridSize) repaint();
}
}
public void mousePressed (MouseEvent me) {
int x = me.getX();
int y = me.getY();
if(stage=="grid") {
stage = "animal";
repaint();
} else if(stage=="animal") {
if(x<150 && x>80 && y<150 && y>130) animal=0;
else if(x<150 && x>80 && y<200 && y>180) animal=1;
else if(x<150 && x>80 && y<250 && y>130) animal=2;
if (animal>-1) {
stage="game";
pieceDim = 420/gridDim;
repaint();
}
} else if(stage=="game") {
x /= pieceDim;
y /= pieceDim;
Boolean right = (x-1==gapX && y==gapY);
Boolean left = (x+1==gapX && y==gapY);
Boolean down = (x==gapX && y-1==gapY);
Boolean up = (x==gapX && y+1==gapY);
if (right || left || down || up) {
moveX = x;
moveY = y;
repaint();
}
}
}
public void update(Graphics g) { paint(g); }
public void mouseDragged (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
public void mouseEntered (MouseEvent me) {}
public void mouseClicked (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
}
I am working on a Main Menu for a game and whenever the applet starts running it draws the applet 4 times before starting the program itself it keeps repainting it i added textures to the program so i don't know whats the problem
public class MainMenu extends Applet implements MouseListener, Runnable {
int xpos;
int ypos;
private Image menu = null;
private Image play = null;
private Image exit = null;
int rect1xco, rect1yco, rect1width, rect1height;
int rect2xco, rect2yco, rect2width, rect2height;
boolean mouseEntered;
boolean rect1Clicked;
boolean rect2Clicked;
public void init() {
rect1xco = 590;
rect1yco = 300;
rect1width = 150;
rect1height = 70;
rect2xco = 590;
rect2yco = 400;
rect2width = 150;
rect2height = 70;
addMouseListener(this);
}
public void paint(Graphics g) {
this.setSize(1350, 650);
if (menu == null)
menu = getImage("menu.JPG");
if (play == null)
play = getImage("Button.png");
if (exit == null)
exit = getImage("Button.png");
g.drawImage(menu, 0, 0, getWidth(), getHeight(), this);
g.drawImage(play, 590, 300, 150, 70, this);
g.drawImage(exit, 590, 400, 150, 70, this);
Font Blockt = new Font("INFECTED", Font.ITALIC, 200);
g.setFont(Blockt);
g.setColor(Color.yellow);
g.drawString("DX BALL", 360, 200);
g.getFontMetrics();
Font DIGITAL = new Font("Blockt", Font.ITALIC, 30);
g.setFont(DIGITAL);
g.setColor(Color.black);
g.drawString("Play", 635, 340);
g.drawString("Exit", 635, 440);
if (rect1Clicked) {
}
if (rect2Clicked) {
System.exit(1);
}
}
public void mousePressed(MouseEvent me) {
if (rect1Clicked == true) {
repaint();
play = getImage("ButtonClicked.png");
}
if (rect2Clicked == true) {
repaint();
exit = getImage("ButtonClicked.png");
}
}
public void mouseClicked(MouseEvent me) {
xpos = me.getX();
ypos = me.getY();
rect1Clicked = false;
if (xpos > rect1xco && xpos < rect1xco + rect1width && ypos > rect1yco
&& ypos < rect1yco + rect1height) {
rect1Clicked = true;
System.out.println("Start button pressed"); // start the game
} else
rect1Clicked = false;
if (xpos > rect2xco && xpos < rect2xco + rect1width && ypos > rect2yco
&& ypos < rect2yco + rect2height)
rect2Clicked = true;
else
rect2Clicked = false;
}
public void mouseReleased(MouseEvent me) {
play = getImage("Button.png");
exit = getImage("Button.png");
repaint();
}
public void mouseEntered(MouseEvent me) {
mouseEntered = true;
}
public void mouseExited(MouseEvent me) {
mouseEntered = false;
}