|  
               
                |  |  | 
                    
                    
                    
                 |  
 
	
	
		
	
	
  | 
    
      
                |  | New Member |  | 
 
                  
                      Dec 1, 2005, 03:47 AM
                  
                 |  |  
  
    | 
        
        
        
       
        
        How do you... 
       
                  
        Make an object move in JCreator. E .g I have to get an object to move around the screen and the user has to click on it before it moves then the amount of times is counted up and displayed at the end of the programme. If you could help that would be great
 Thanks
 |  
    |  |  
	
		
	
	
  | 
    
      
              |  | Ultra Member |  | 
 
                  
                      Dec 1, 2005, 05:12 AM
                  
                 |  |  
  
    | 
        
        
        
       
                  
        OK, first I take it by "in JCreator" you mean "in Java".  JCreator is just an IDE like Eclipse or JBuilder.  Java is the language that actually does everything. 
Anyway, what you need to do is quite simple.
 
You'll need a Frame with a button on it.  You'll then want a thread that will sleep for a set amount of time.  When it wakes, it can just move the button by changing setBounds().  To implement a score, set an ActionListener on the button so that when it's clicked it increments a variable by one.
 
For various code examples for threads, frames etc. check here: http://javaalmanac.com/ 
Also the Java API documents are your friend: http://java.sun.com/j2se/1.5.0/docs/api/index.html 
I can't give you code to work from just in case this is a school project (we can't do you work for you) but if you have some code you want me to look at - I can review it - just post it here.
     |  
    |  |  
	
		
	
	
  | 
    
      
                |  | New Member |  | 
 
                  
                      Dec 1, 2005, 05:25 AM
                  
                 |  |  
  
    | 
        
        
        
       
                  
        OK thanks I'll have a go, I have only been learning Java for about 3 weeks and it is kind of confusing me a lot. I'll try the button then if I get stuck I'll post the code on here. Thanks for your help
     |  
    |  |  
	
		
	
	
  | 
    
      
              |  | Ultra Member |  | 
 
                  
                      Dec 1, 2005, 05:48 AM
                  
                 |  |  
  
    | 
        
        
        
       
                  
        Yeah, I'd take it one step at a time.  Just try creating a JFrame first with a button in the middle of it.  When you get that far, let me know and I'll give you some code for the thread part.
     |  
    |  |  
	
		
	
	
  | 
    
      
                |  | New Member |  | 
 
                  
                      Dec 4, 2005, 10:56 AM
                  
                 |  |  
  
    | 
        
        
        
       
                  
        I have done it a little differently to how you suggested it. I am struggling with getting the thread to stop. Here is my code if you could point out what I need to do thent hat would be great
 Thanks
 
 import java.awt.*;
 import java.applet.*;
 import java.awt.image.*;
 import java.awt.event.*;
 
 public class Bat6 extends Applet implements Runnable,MouseListener {
 Image ratImage;//new image
 Thread runner;//new thread
 int thisPosX = (int)(java.lang.Math.random() * 200);//random x position for rat
 int thisPosY = (int)(java.lang.Math.random() * 200);//random y position for rat
 Rectangle ratRect = new Rectangle(thisPosX,thisPosY,144,83);//new rectangle to contain the rat
 
 
 public void init() {
 ratImage= getImage(getCodeBase(),"rat.gif");// had to make the rat smaller
 addMouseListener(this);// add an action listener to the current object
 }
 
 public void start() {
 runner = new Thread(this);//new thread
 runner.start();//start thread
 }
 
 public void run() {
 for (int I=0;i<5;i++) {//for 5 times
 try {
 Thread.sleep(2000);//wait for 2 seconds
 }
 catch (InterruptedException e) {//catch any exceptions
 }
 
 thisPosX = (int)(java.lang.Math.random() * 200);//regenerate x position
 thisPosY = (int)(java.lang.Math.random() * 200);//regenerate y position
 ratRect = new Rectangle(thisPosX,thisPosY,144,83);//recreates the rectangle around the rat, doesn't need drawing. No longer 1 pixel bigger than rat since only needed that for drawing so rat didn't overlap.
 repaint();//repaint rat
 }
 }
 
 public void paint(Graphics g) {
 g.drawImage(ratImage,thisPosX,thisPosY,this);
 }
 
 public void mousePressed(MouseEvent evt){
 if (ratRect.contains (evt.getX(),evt.getY())){//if the click is within the rat's rectangle
 thisPosX = (int)(java.lang.Math.random() * 200);//regenerate x position
 thisPosY = (int)(java.lang.Math.random() * 200);//regenerate y position
 ratRect = new Rectangle(thisPosX,thisPosY,144,83);//recreates the rectangle around the rat, doesn't need drawing. No longer 1 pixel bigger than rat since only needed that for drawing so rat didn't overlap.
 repaint();//repaint rat
 }
 }
 
 public void mouseReleased(MouseEvent evt){//all methods of mouse listener must be implemented
 }
 public void mouseExited(MouseEvent evt){
 }
 public void mouseEntered(MouseEvent evt){
 }
 public void mouseClicked(MouseEvent evt){
 }
 }
 |  
    |  |  
	
		
	
	
  | 
    
      
              |  | Ultra Member |  | 
 
                  
                      Dec 7, 2005, 02:50 AM
                  
                 |  |  
  
    | 
        
        
        
       
                  
        Interesting.  In Firefox, your applet does what I expect it to - run 5 times then stop moving the image.  But in Internet Explorer it runs continuously.  I'll look into what IE is doing to keep the thread looping.  I don't think it's your Java code.
     |  
    |  |  
	
		
	
	
  | 
    
      
                |  | New Member |  | 
 
                  
                      Jan 24, 2006, 07:39 AM
                  
                 |  |  
  
    | 
        
        
        
       
                  
        Hi, Just one question to finish my project. I want to make the type and numbers bigger once you press pick the numbers. If you could help me that would be great
 Thanks.
 
 Here the code:
 
 import java.awt.*;
 import java.awt.event.*;
 import java.applet.*;
 import java.awt.image.*;
 
 public class Lottery9 extends Applet implements ActionListener {
 
 Image lotteryImage; //global variable global defintions before the init is declared
 
 
 //global variable global defintions before the init is declared
 Button newButton;
 
 // Define the buttons as an array
 Button[] myButton;
 
 // These are the labels on the buttons themselves
 String[] myButtonLabel = {"Pick the Numbers","About","Reset",};
 
 // These are the responses from the button presses
 String[] myButtonResponse = {"", "Made by Candidate Number: 530009254",""};
 
 //Create somewhere to remember which button was last pressed
 Button lastButton = null;
 
 // Add a label to write a response into - note the extra spaces here...
 Label responseLabel = new Label("Press a button                          ");
 
 //create an int for the number of balls
 int numballs = 7;
 
 //create a new array of ints to hold the numbers
 int[] thisPos= new int[numballs];
 
 
 
 public void init() {
 this.setLayout(new GridLayout(8,3,15,6));
 ImageFilter cardFilter;
 setBackground (Color.lightGray);
 lotteryImage= getImage(getCodeBase(),"lottery.gif");
 
 // Add the response label to the window first
 add(responseLabel);
 
 // Create the array of buttons
 myButton = new Button[3];
 
 
 for (int I=0; I<3; I++) {
 // Initialise the buttons and creates a loop
 myButton[i] = new Button(myButtonLabel[i]);
 myButton[i].addActionListener(this);
 
 // Now add the buttons
 add(myButton[i]);
 }
 }
 
 public int checknum(){
 //create a new function that generates new numbers and checks they are unique
 int newnumber = 0;
 //create an int that will be our new value
 for(int I=0;i<thisPos.length;i++){
 //for each item in the array of values
 while((thisPos[i] == newnumber)||(newnumber == 0)){
 //while the new number equals an item in the array currently or zero If the number is alreaded generated it will generate a new number or if the number is 0 a news number will be generated tooo.
 newnumber = (int)(java.lang.Math.random() * 49);
 //generate a new number
 
 }
 }
 return newnumber;
 //return the new number
 }
 
 public void paint(Graphics g) {
 
 //generate number and output it here
 myButtonResponse[0]="Your random numbers are:   ";
 //create a new response string
 for(int I=0; I<thisPos.length; I++){
 //for each of items in the array
 thisPos[i] = checknum();
 //set their value to a new number
 }
 
 int endrange= thisPos.length - 1;// length of array -1
 //calculate what the last item in the list is
 java.util.Arrays.sort(thisPos, 0, endrange);
 //sort the array into numerical order, except the last item which is the Bonus Ball
 
 for(int I=0; I<thisPos.length; I++){
 myButtonResponse[0]+= thisPos[i]+" ";
 }
 //append the new number to the response string
 
 
 // Change the label text according to which button was pressed
 for (int I=0; I<3; I++) {
 if (lastButton==myButton[i]) {
 responseLabel.setText(myButtonResponse[i]);
 g.drawImage(lotteryImage, 120, 140, this);//this referes to the current object, refer to the paint method
 }
 }
 }
 
 
 public void actionPerformed(ActionEvent e) {
 // actionPerformed runs when any event occurs
 if (e.getSource() instanceof Button) {
 
 //set lastButton to whichever one it was
 for (int I=0; I<3; I++) {
 if (e.getSource() == myButton[i]) {
 lastButton = myButton[i];
 
 }
 }
 
 // force the window to be repainted
 repaint();
 
 }
 }
 }
 |  
    |  |  
 
 
 
  
    | Question Tools | Search this Question |  
    |  |  |  
 Add your answer here.
 View more  questions
Search
 
 |