2 Replies - 57125 Views - Last Post: 29 February 2008 - 02:53 AM Rate Topic: -----

#1 m514  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-February 08

How to use the repaint method in an applet

Posted 26 February 2008 - 10:27 AM

I need help on using repaint , I don`t know exactly how to use it and don`t know exactly what threads are used for , If anyone could try to explain! I need to have the user click on the start button and be able to go to another window with different buttons. I asked around and they told me to use repain, but am not sure of how it works, I need help!!!

import java.awt.*; 
import java.applet.*; 
// These classes are for Url's. 
import java.net.*; 
// import an extra class for the ActionListener 
import java.awt.event.*; 

public class Images extends Applet implements ActionListener
{ 
 // Your image name; 
     Image my_jpg; 
 // button to click
 	Button startButton;

 // The applet base URL 
     URL base; 

 // This object will allow you to control loading 
     MediaTracker mt; 

     public void init()  
     { 
    
        //setLayout(null); 
           setLayout(null); 
        startButton = new Button("Start"); 
       	startButton.setBounds(500,800,100,30); 
        add(startButton);
        startButton.addActionListener(this); 


  // initialize the MediaTracker 
          mt = new MediaTracker(this); 

         try { 
   // getDocumentbase gets the applet path. 
               base = getDocumentBase(); 
         } 
         catch (Exception e) {} 

  // Here we load the image.  
          my_jpg = getImage(base,"s.jpg"); 

  // tell the MediaTracker to kep an eye on this image, and give it ID 1; 
          mt.addImage(my_jpg,1); 

  // now tell the mediaTracker to stop the applet execution 
  // (in this example don't paint) until the images are fully loaded. 
  // must be in a try catch block. 

         try { 
               mt.waitForAll(); 
          } 
          catch (InterruptedException  e) {} 

  // when the applet gets here then the images is loaded. 

     } 

     public void paint(Graphics g)  
     { 
  // now we are going to draw the gif on the screen 
          
          g.drawImage(my_jpg,20,20,this); 

  // you can resize the image easily  // g.drawImage(my_jpg,20,140,30,40,this); 
  

     } 
     	
     	 public void actionPerformed(ActionEvent evt)  
         { 
  // Here we will ask what component called this method 
              if (evt.getSource() == startButton)  
   // So it was the okButton, then let's perform his actions 
   // Let the applet perform Paint again. 
   // That will cause the aplet to get the text out of the textField 
   // again and show it. 
                  repaint(); 

} 
}


*edit: Please use code tags in the future, thanks! :code:

This post has been edited by Martyr2: 26 February 2008 - 10:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to use the repaint method in an applet

#2 zakmobl  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 27-February 08

Re: How to use the repaint method in an applet

Posted 27 February 2008 - 04:05 PM

The repaint method is the way for you to explicitly call the paint method in your applet. If I understand your question correctly, this is not the solution that your are looking for. What you might want to do is move the contents of your applet using the setbounds and disable/reenable anything that is not used in both by use of the setvisible method. I hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 friendster2  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 244
  • Joined: 29-February 08

Re: How to use the repaint method in an applet

Posted 29 February 2008 - 02:53 AM

There are two ways paint() calls are generated:

1. spontaneous painting, initiated by the environment
2. programmer generated calls via repaint() and update()

The repaint() Method

The repaint() method is the one invoked by a program to do drawing. Their are 4 versions of this method but the one with no arguments is usually used. Drawing via repaint() most often takes place in response to user input.

repaint() ==> update() ==> paint()

repaint() does not invoke paint() directly. It schedules a call to an intermediate method, update(). Finally, update() calls paint().

The reason for this complexity is Java's support for concurrent programming. Java can run a form of parallelism which makes it seem that many things are happening simultaneously. It does this using threads which are like light weight processes. See the section on Threads.

The power of concurrency is bought at some cost.
Using repaint() can be tricky for at least three reasons.

1. the interaction between repaint() and the spontaneous painting done by the GUI thread
2. the fact that repaint() just asks the thread system to schedule a call to update()/paint() and then exits. The repaint() method is asynchronous.
3. the problem of preventing your drawing from being erased when being updated.


note:--source is http://www.ryerson.c...40/repaint.html

This post has been edited by friendster2: 29 February 2008 - 02:58 AM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1