2012-10-16 55 views
1

我想通過將它傳遞給showWindow來調用load_picture中的return語句tempimage,但我不知道如何。繼承人我的代碼片段。編輯: 我想我想說的是,我不完全確定如何處理硬編碼「picture1.gif」。我知道我需要調用一個方法來加載圖像,但我不太清楚應該放置什麼。 :如何在另一個方法中使用返回語句?

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); 
} 

任何幫助表示讚賞。由於

+0

你不*調用語句*。語句在一段代碼中按順序執行。在這種情況下,'return tempimage'將在方法結束時執行。 –

回答

0

從的ShowWindow方法中,只需調用load_picture方法如下:

Image tempImage = load_picture(filename, frame1); 

從這裏你可以做任何你與tempImage對象喜歡。

1

由該load_picture方法返回的值可以直接發送到showWindow方法,也可以將其分配給一個變量:

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