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 に答える