05-19-2013, 04:38 AM
I'm trying to learn java from your video tutorials and i'm on part 19 now when executing your program it does not find any mouse so nothing happens when the mouse is pressed.
[code2=java]import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class mouselistener
extends Applet
implements MouseListener{
private Graphics globalGraphics = null;
public void inint()
{
this.addMouseListener(this);
}
public void paint(Graphics g)
{
this.setSize(new Dimension(800, 600));
globalGraphics = g.create();
}
public void drawDot(int x, int y)
{
//define r, g, b color randomizer
int r = (int) (Math.random() * 255);
int g = (int) (Math.random() * 255);
int b = (int) (Math.random() * 255);
System.out.println("test");
Color randomColor = new Color(r,g,b);
globalGraphics.setColor(randomColor);
globalGraphics.fillRect(x, y, 10, 10);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
System.out.println("Mouse have entered!");
}
@Override
public void mouseExited(MouseEvent arg0) {
System.out.println("Mouse have gone");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Click");
int mouseX = e.getX();
int mouseY = e.getY();
drawDot(mouseX, mouseY);
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}[/code2]
[code2=java]import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class mouselistener
extends Applet
implements MouseListener{
private Graphics globalGraphics = null;
public void inint()
{
this.addMouseListener(this);
}
public void paint(Graphics g)
{
this.setSize(new Dimension(800, 600));
globalGraphics = g.create();
}
public void drawDot(int x, int y)
{
//define r, g, b color randomizer
int r = (int) (Math.random() * 255);
int g = (int) (Math.random() * 255);
int b = (int) (Math.random() * 255);
System.out.println("test");
Color randomColor = new Color(r,g,b);
globalGraphics.setColor(randomColor);
globalGraphics.fillRect(x, y, 10, 10);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
System.out.println("Mouse have entered!");
}
@Override
public void mouseExited(MouseEvent arg0) {
System.out.println("Mouse have gone");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("Click");
int mouseX = e.getX();
int mouseY = e.getY();
drawDot(mouseX, mouseY);
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}[/code2]