Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
little help please Posted by nabigh on 15 Apr 2012 at 2:57 PM
hi guys
I am trying to make a java program that whenever I make mouse click,a ball is drawn and goes up until it vanishes (goes out of range)
so far I was able to make the ball goes up,but I couldn't do it within the mouseclicked method

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.applet.*;
public class circle extends Applet implements Runnable,MouseListener{
Graphics g;
Rectangle r=new Rectangle(0,0,getWidth(),getHeight());
int x=500;
int y=500;
public void start(){
Thread t=new Thread(this);
t.start();
}
public circle(){

addMouseListener(this);
}

public void paint(Graphics g){
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.drawOval(x, y, 50, 50);
}


public void run() {
int dx=0, dy=20;
while (true) {
for(int i=0; i<5; i++) {
if (false)
return;
x-=dx;
y-=dy;
repaint();
try {
Thread.sleep(150);
}catch(InterruptedException e) {
e.printStackTrace();
}
}

}
}

public void drawCircle(int x, int y) {
Graphics g = this.getGraphics();
g.drawOval(x, y, x, y);

g.fillOval(x, y, 2, 2);
}
public void mouseClicked(MouseEvent e) {
if (r.contains(e.getX(), e.getY())){
g.drawOval(0,0, 50, 50);
repaint();
}

}

@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

}
}



please help
Thread Tree
nabigh little help please on 15 Apr 2012 at 2:57 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.