Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecated method, what to use instead? [closed]

The method show(); in class Windows for java.awt in deprecated. What can I use instead?

package adventure;
import java.awt.*;

import java.awt.image.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
import java.applet.*;
// Infogar testkommentar mvh Holger 2012-06-20 kl 19.03
public class Adventure extends Frame  {
    private static final long serialVersionUID=100L;
    public Adventure() {        
    setSize(850, 440);    
    World world = new DungeonWorld ( this );        

    Person me = new Person( world, "You", null );    
    show();
    me.goTo("Dungeon");     
    add( new Player( world, me ) );    
    addWindowListener(new MyWindowAdapter ());    
    }

    class MyWindowAdapter extends WindowAdapter {

    public void windowClosing (WindowEvent e) {
        System.exit(0);
    }
    }


    // Load an image from the net, making sure it has already been
    // loaded when the method returns
    public Image loadPicture ( String imageName ) {
    Image im = null;

    // Load the image from the net
    try {
        URL imageSource = new URL( "http://www...xxx/"
                       + imageName );

        try {
        im = createImage( (ImageProducer) imageSource.getContent());
        } catch (IOException e) {}

    } catch (MalformedURLException e ) { }

    // Wait to ensure that the image is loaded
    MediaTracker imageTracker = new MediaTracker( this );
    imageTracker.addImage( im, 0 );
    try {
        imageTracker.waitForID( 0 );
    }
    catch( InterruptedException e ) { }

    return im;
    }


    // Load and play a sound from /usr/local/hacks/sounds/

    public void playSound (String name) {
    URL u = null;

    try {
        u = new URL("file:" +  "/usr/local/hacks/sounds/" + name + ".au");
    } catch (MalformedURLException e ) { }

    AudioClip a = Applet.newAudioClip(u);
    a.play();
    }

    public static void main (String[] args) {       
    System.out.println("test"); 
      new Adventure();
    }
}
like image 464
Niklas Rosencrantz Avatar asked Jun 21 '12 08:06

Niklas Rosencrantz


1 Answers

Let's read the Java API for Window#show(): here

@Deprecated
public void show()

Deprecated. As of JDK version 1.5, replaced by setVisible(boolean).

Makes the Window visible. If the Window and/or its owner are not yet displayable, both are made displayable. The Window will be validated prior to being made visible. If the Window is already visible, this will bring the Window to the front.

So you should use Window#setVisible(boolean) - for show() use setVisible(true).

EDIT

In some environments just replacing show() with setVisible(true) changes the behavior of the application. This happens, when you wrote a subclass of Window that overrides show() (same for hide()).

So in your code example setVisible(true) does exactly the same as show(). But in general, just be certain, that no one overrides show() which wouldn't be executed anymore when using setVisible(true). In such a case, you have to change the overriden methods, too.

like image 84
Bertram Nudelbach Avatar answered Oct 11 '22 04:10

Bertram Nudelbach