mardi 27 octobre 2015

Create your own button

Locate an image from the web and draw it to the screen. This image will function as a button. If the user ever clicks anywhere on the image, then the program should output “You clicked on the image”. if the user clicks anywhere else in the Applet window, the program should output “You clicked outside the image”. Use if or if-else statements and the logical operators where necessary. Make your program works in both the x and y direction. Here's what i have but doesn't compile (I'm horrible at coding) Please help (simple code.)

    import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
public class ClickToRight extends Applet implements MouseListener{
String image = new String ("Ballislife.PNG");
int x,y;
public void init(){
x=700; y=400;
addMouseListener(this);
}
public void paint(Graphics g){
this.resize (1400,800);
g.drawLine(0, 400, 1400, 400);
g.drawLine(700, 0, 700, 800);

if(x>700 && y<400){
    g.drawLine("You clicked on image");

g.drawImage(getImage(getCodeBase(), "Ballislife.png", 75, 75, this);

public void mouseClicked(MouseEvent me) {
x =me.getX();
y =me.getY();
repaint();
}

public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}

Aucun commentaire:

Enregistrer un commentaire