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

任何帮助表示赞赏。谢谢

4

2 回答 2

1

方法返回的值load_picture可以直接发送给showWindow方法,也可以分配给变量:

String filename = "your/filename";
JFrame theFrame = new JFrame();
Project3 project = new Project3();
MyPanel.showWindow(project.load_picture(filename, theFrame);
于 2012-10-16T02:38:06.960 回答
0

在 showWindow 方法中,只需调用 load_picture 方法,如下所示:

Image tempImage = load_picture(filename, frame1);

从这里你可以对 tempImage 对象做任何你喜欢的事情。

于 2012-10-16T02:37:52.157 回答