0

Hey all,

so now im trying to code a simple game where each time an image is clicked on it is randomly regenerated somewhere else on the JFrame. I'm having some trouble with the logic though. Any help would be appreciated, thanks! heres all the code, theres 3 classes

package Lab10Part2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.net.URL;

import javax.swing.JApplet;

public class MyMouse1 extends JApplet {
   
	// Constants

    /**
	 * 
	 */
	private static final long serialVersionUID = 8665592237284336579L;


	protected static final int SIZE = 400;     // size of the applet
    
	
    // Instance variables
    protected Image micky;
    protected int mickyX, mickyY;
    protected MediaTracker mt;
    protected URL base;

    public void init() {
	setSize(SIZE,SIZE);
	micky = null;
	mt = new MediaTracker(this);
	try {
	    base = getDocumentBase();
	}
	catch(Exception e){}
	micky = getImage(base,"mighty_mouse.jpg");
	mt.addImage(micky, 1);
	try {
	    mt.waitForAll();
	}
	catch(InterruptedException ie) {}
	mickyX = 100;
	mickyY = 100;
    }
	
    public void paint(Graphics g) {
	super.paint(g);
	g.setColor(Color.white);
	g.fillRect(0,0,getWidth(),getHeight());
	g.drawImage(micky, mickyX, mickyY, this);
    }
}
package Lab10Part2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.URL;

import javax.swing.JFrame;

public class MyMouse2 extends JFrame {
	// Constants

    /**
	 * 
	 */
	private static final long serialVersionUID = 8665592237284336579L;


	protected static final int SIZE = 400;     // size of the applet
    
	
    // Instance variables
    protected Image micky;
    protected int mickyX, mickyY;
    protected MediaTracker mt;
    protected URL base;
    public MyMouse2() {
    	init();
    	setVisible(true);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void init() {
	setSize(SIZE,SIZE);
	micky = null;
	mt = new MediaTracker(this);
	Toolkit tk = Toolkit.getDefaultToolkit();
	micky = tk.getImage("mighty_mouse.jpg");
	mt = new MediaTracker(this);
	try {
	    mt.waitForAll();
	}
	catch(Exception e){}
	mt.addImage(micky, 1);
	try {
	    mt.waitForAll();
	}
	catch(InterruptedException ie) {}
	mickyX = 100;
	mickyY = 100;
    }
	
    public void paint(Graphics g) {
	super.paint(g);
	g.setColor(Color.white);
	g.fillRect(0,0,getWidth(),getHeight());
	g.drawImage(micky, mickyX, mickyY, this);
    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyMouse2();
	}

}
package Lab10Part2;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class MyMouse2Clickable extends MyMouse2 implements MouseListener{
	protected Random rng = new Random();

	/**
	 * 
	 */
	private static final long serialVersionUID = 5129912116688150129L;

	public void init() {
        super.init();
        addMouseListener(this);
        setSize(2*SIZE,2*SIZE);
}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		  mickyX = rng.nextInt(getWidth()-micky.getWidth(this));
          mickyY = rng.nextInt(getHeight()-micky.getHeight(this));
          repaint();
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0

I'm having some trouble with the logic though

Do you really expect someone to read all that undocumented code, guess exactly what it is supposed to do, then work out in what way it's doing the wrong thing?
You need to explain exactly what problem you want us to solve.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.