getImage()
drawImage(Image img,int x,int y,int width,int height,ImageObserver observer)

解决方案 »

  1.   

    补充一下:当前路径下的图片:aa.gif,我要在applet中的一个Panel容器(比如:panel2)中显示它.
    谢谢回答!!
      

  2.   

    我这样写:
           img1=this.getImage(this.getCodeBase(),"1.jpg");
           img2=this.getImage(this.getCodeBase(),"2.jpg");
    出错。
      

  3.   

    图片的路径应该与HTML的路径为准
      

  4.   

    用ImageIcon对象调用图片。
    ImageIcon image1 = new ImageIcon(程序文件路径.class.getResource("aa.gif"));this.setIcon(image1);你试一下,基本方法就这样,当然方法很多落,我没试过在一个jPanel中显示,但如果这样不行,你可以放一个JLABEL,社成borderlaout,也就是让这个jlabel充满整个jpanel,然后jLabel1.setIcon(image1),旧可以了!
      

  5.   

    import java.applet.Applet;
    import java.awt.*;public class advertise extends Applet{
       private Label prompt1, prompt2;
       private double t;
       private Choice choicePages;
       private TextField inputTime;
       private boolean judge = false;
       private Image pic[];
       private int totalImages = 30, currentImage = 0, sleepTime = 1000;
       private Graphics gContext;
       private Image buffer;   public void init(){
          prompt1 = new Label("Please choice how many advertisement page do you want to see:");
          prompt2 = new Label("Enter how much time do you want to see each page:");
          choicePages = new Choice();
          inputTime = new TextField("1.0",4);
          pic = new Image[totalImages];
          buffer = createImage(800, 600);
          gContext = buffer.getGraphics();
          gContext.setColor(Color.white);
          gContext.fillRect(0, 0, 800, 600);      for(int i = 0; i<=29; i++){
             pic[i] = getImage(getDocumentBase(), "PC"+(i+1)+".jpg");
             choicePages.addItem(""+(i+1));
          }
          add(prompt1);
          add(choicePages);
          add(prompt2);
          add(inputTime);
          }   public void start(){
          gContext.drawImage(pic[0], 0, 0, this);
          currentImage = 1;
       }   public void paint(Graphics g){
          if(judge){
          g.drawImage(buffer, 80, 60, this);
          gContext.fillRect(0, 0, 800, 600);
          gContext.drawImage(pic[currentImage], 0, 0, this);      postEvent(new Event(this, Event.MOUSE_ENTER,""));
          currentImage = ++currentImage%totalImages;
          try{
             Thread.sleep(sleepTime);
          }
          catch(InterruptedException e){
             showStatus(e.toString());
          }
          repaint();
         }
       }
       
       public boolean action(Event e, Object o){
          if(e.target instanceof Choice)
          totalImages = Integer.parseInt(choicePages.getSelectedItem());
          if(e.target instanceof TextField)
         { t = Double.parseDouble(inputTime.getText());
          sleepTime = (int)t*1000;}
          
          judge = true;
          
          return true;
    }
          
        public void update(Graphics g){
          paint(g);
       }      
    }
      

  6.   

    Image img=this.getImage(this.getCodeBase(),"1.jpg");
    你要把1.jpg图片文件与applet的.class文件放在同一目录下
      

  7.   

    好象不行,代码如下:
    出错的地方我打有“?”
    package event;
        import java.awt.event.*;
        import java.awt.*;
        import java.applet.*;
        import java.util.Vector;
        import javax.swing.*;    public class DrawTest extends Applet{
          DrawPanel panel;
          DrawControls controls;
               public void init() {
            setLayout(new BorderLayout());
            this.setSize(780,400);
            panel = new DrawPanel();
            controls = new DrawControls(panel);
            add("Center", panel);
            add("North",controls);
          }      public void destroy() {
            remove(panel);
            remove(controls);
          }      public static void main(String args[]) {
            Frame f = new Frame("DrawTest");
            DrawTest drawTest = new DrawTest();
            drawTest.init();
            drawTest.start();        f.add("Center", drawTest);
            f.setSize(300, 300);
            f.show();
          }
          public String getAppletInfo() {
            return "A simple drawing program.";
          }
        }    //画图控制区域
        class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
          public static final int LINES = 0;
          public static final int POINTS = 1;
          int    mode = LINES;
          Vector lines = new Vector();
          Vector colors = new Vector();
          int x1,y1;
          int x2,y2;      public DrawPanel() {
            setBackground(Color.white);
            addMouseMotionListener(this);
            addMouseListener(this);
          }      public void setDrawMode(int mode) {
            switch (mode) {
              case LINES:
              case POINTS:
                this.mode = mode;
                break;
              default:
                throw new IllegalArgumentException();
            }
          }
          public void mouseDragged(MouseEvent e) {
            e.consume();
            switch (mode) {
              case LINES:
                x2 = e.getX();
                y2 = e.getY();
                break;
              case POINTS:
              default:
                colors.addElement(getForeground());
                lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
              x1 = e.getX();
              y1 = e.getY();
              break;
            }
            repaint();
          }      public void mouseMoved(MouseEvent e) {
          }      public void mousePressed(MouseEvent e) {
            e.consume();
            switch (mode) {
              case LINES:
                x1 = e.getX();
                y1 = e.getY();
                x2 = -1;
                break;
              case POINTS:
              default:
                colors.addElement(getForeground());
              lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
              x1 = e.getX();
              y1 = e.getY();
              repaint();
              break;
            }
          }      public void mouseReleased(MouseEvent e) {
            e.consume();
            switch (mode) {
              case LINES:
                colors.addElement(getForeground());
                lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
                x2 = -1;
                break;
              case POINTS:
              default:
                break;
            }
            repaint();
          }      public void mouseEntered(MouseEvent e) {
          }      public void mouseExited(MouseEvent e) {
          }      public void mouseClicked(MouseEvent e) {
          }      public void paint(Graphics g) {
            int np = lines.size();        /* draw the current lines */
            g.setColor(getForeground());
            for (int i=0; i < np; i++) {
              Rectangle p = (Rectangle)lines.elementAt(i);
              g.setColor((Color)colors.elementAt(i));
              if (p.width != -1) {
                g.drawLine(p.x, p.y, p.width, p.height);
              } else {
                g.drawLine(p.x, p.y, p.x, p.y);
              }
            }
            if (mode == LINES) {
              g.setColor(getForeground());
              if (x2 != -1) {
                g.drawLine(x1, y1, x2, y2);
              }
            }
          }
        }    //控制菜单代码
        class DrawControls extends Panel implements ItemListener {
          DrawPanel target;      public DrawControls(DrawPanel target) {
            this.target = target;
            this.setSize(780,400);
            setLayout(new FlowLayout());
            setBackground(Color.lightGray);
            target.setForeground(Color.red);
            Image img1=this.getImage(this.getCodeBase(),"1.jpg");//该行出错,报红叉????????????????
                    CheckboxGroup group = new CheckboxGroup();
             Checkbox b;
            //
            add(b = new Checkbox(null, group, false));
            b.addItemListener(this);
            b.setForeground(Color.red);
            //
            add(b = new Checkbox(null, group, false));
            b.addItemListener(this);
            b.setForeground(Color.green);
            //
            add(b = new Checkbox(null, group, false));
            b.addItemListener(this);
            b.setForeground(Color.blue);
            //
            add(b = new Checkbox(null, group, false));
            b.addItemListener(this);
            b.setForeground(Color.pink);
            //
            add(b = new Checkbox(null, group, false));
            b.addItemListener(this);
            b.setForeground(Color.orange);
            //
            add(b = new Checkbox(null, group, true));
            b.addItemListener(this);
            b.setForeground(Color.black);
            //
            target.setForeground(b.getForeground());
            Choice shapes = new Choice();
            shapes.addItemListener(this);
            shapes.addItem("Lines");
            shapes.addItem("Points");
            shapes.setBackground(Color.lightGray);
            add(shapes);
          }      public void paint(Graphics g) {
            Rectangle r = getBounds();
            g.setColor(Color.lightGray);
            g.draw3DRect(0, 0, r.width, r.height, false);        int n = getComponentCount();
            for(int i=0; i<n; i++) {
              Component comp = getComponent(i);
              if (comp instanceof Checkbox) {
                Point loc = comp.getLocation();
                Dimension d = comp.getSize();
                g.setColor(comp.getForeground());
                g.drawRect(loc.x-1, loc.y-1, d.width+1, d.height+1);
              }
            }
          }      public void itemStateChanged(ItemEvent e)
          {
             if (e.getSource() instanceof Checkbox) {
                target.setForeground(((Component)e.getSource()).getForeground());
              } else if (e.getSource() instanceof Choice) {
                String choice = (String) e.getItem();
                if(choice.equals("Lines")){
                  target.setDrawMode(DrawPanel.LINES);
                }else if (choice.equals("Points")) {
                 target.setDrawMode(DrawPanel.POINTS);
                }
              }
          }    }
      

  8.   

    呵呵,Panel中没有getImage的方法。
      

  9.   

    补充一下:你的画图片的类又是继承自Panel,而Panel中没有getImage方法。
      

  10.   

    你回复真慢。试试这个。
    URL imageUrl = null;
    Image image;
    try{
        URL imageUrl = Class.forName("ClassName").getResource("res\\play.gif");
    } catch (ClassNotFoundException ex) {ex.printStackTrace();}
     image = this.getToolkit().getImage(imageUrl);取得了image你爱怎么干就怎么干。
      

  11.   

    Image img=java.awt.Toolkit.getDefaultToolkit().getImage("xxx.gif");