简体   繁体   中英

How can I use the return statement from one method in another?

I want to call the return statement tempimage in load_picture by passing it to showWindow, but I'm not sure how. heres a snippet of my code. edit: i guess what I'm trying to say is, I'm not exactly sure what to do with the hardcoded "picture1.gif". I understand that I need to call a method to load the image, but I'm not too sure what to put in place of it. :

package project3; 

import java.util.Scanner; 
import javax.swing.;
import java.awt.; 
import java.net.*;

public class Project3 {
    //initializing global
static Project3 theobject = new Project3();
final static int MIN_NUMBER=1; 
final static int MAX_NUMBER=8; 
static int image_number=1;
static Image theimage;
// This routine will load an image into memory, non-static requires an object
// It expects the name of the image file name and a JFrame  passed to it
// It will assume an Internet conection is available
// It can only be called AFTER the program object has been created
// It will return a type Image variable, call it like this:   theimage = object.load_picture("picture1.gif", frame); 
// (hard code 'picture1.gif' only when testing - USE a method or variable for 'real' call)
// This code requires you to do an 'import java.awt.*' and an 'import java.net.*'
// Note: this method is using parameter and return type for input/output

// This routine will load an image into memory, non-static requires an object
// It expects the name of the image file name and a JFrame  passed to it
// It will assume an Internet conection is available
// It can only be called AFTER the program object has been created
// It will return a type Image variable, call it like this:   theimage = object.load_picture("picture1.gif", frame); 
// (hard code 'picture1.gif' only when testing - USE a method or variable for 'real' call)
// This code requires you to do an 'import java.awt.*' and an 'import java.net.*'
// Note: this method is using parameter and return type for input/output

public Image load_picture(String imagefile, JFrame theframe)
{

Image tempimage;
// Create a MediaTracker to inform us when the image has
// been completely loaded.
MediaTracker tracker;
tracker = new MediaTracker(theframe);

// getImage() returns immediately. The image is not
// actually loaded until it is first used. We use a
// MediaTracker to make sure the image is loaded
// before we try to display it.

String startURL;
if (imagefile.startsWith("http"))
   startURL = "";
else
   startURL = "http://www.canyons.edu/departments/comp_sci/ferguson/cs111/images/";

URL myURL=null;
try
{
myURL = new URL(startURL + imagefile);
}
catch(MalformedURLException e) {
  System.out.println("Error caught " + e.toString());
}

//tempimage = getImage(myURL);   // JApplet version
tempimage = Toolkit.getDefaultToolkit().getImage(myURL); // stand alone program version


// Add the image to the MediaTracker so that we can wait for it

tracker.addImage(tempimage, 0);
try { tracker.waitForID(0); }
catch ( InterruptedException err) { System.err.println(err); }

return tempimage;
}

// This class/method uses a  global variable that MUST be set before calling/using
// note: You can not call the paint routine directly, it is called when frame/window is shown
// look up the repaint() routine in the book
// Review Listings 8.5 and 8.6
//
public static class MyPanel extends JPanel {
 public void paintComponent (Graphics g) {
     JPanel panel= new JPanel();
    int xpos,ypos;
    super.paintComponent(g);
    // set the xpos and ypos before you display the image
    xpos = 300; // you pick the position
    ypos = 200; // you pick the position
    if (theimage != null) {
        g.drawImage(theimage,xpos,ypos,this);
        // note: theimage global variable must be set BEFORE paint is called
    }
 }
}
public static void showWindow( String filename ) {
    // create, size and show a GUI window frame, you may need to click on taskbar to see window
    //display the filename in the title of the window frame, otherwise the window will be blank (for now)

JFrame frame1= new JFrame();
theimage = theobject.load_picture("picture1.gif", frame1);
//"picture1.gif" is hardcoded, I want to call this using a method
frame1.setTitle(filename);
frame1.setSize(440,302);
frame1.setLocation(400,302);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
}

Any help is appreciated. Thanks

The value that is returned by the load_picture method can be sent directly to the showWindow method, or you can assign it to a variable:

String filename = "your/filename";
JFrame theFrame = new JFrame();
Project3 project = new Project3();
MyPanel.showWindow(project.load_picture(filename, theFrame);

From within the showWindow method, just call the load_picture method as follows:

Image tempImage = load_picture(filename, frame1);

From here you can do anything you like with the tempImage object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM