public class MainFrame extend JFrame{
 Image ImageToShow;
 float zoom;
 JPanel JPanel_Image=new JPanel(){
  {
     setBackground(Color.White);
     setOpaque(true);
  }
  public void paintComponents(Graphics g)
  {
     super.paintComponent(g);
     int iw=ImageToShow.getWidth(this);
     int ih=ImageToShow.getHeight(this);
     int siw=(int)Math.ceil((float)iw*zoom);
     int sih=(int)Math.ceil((float)ih*zoom);
     Dimension sz=getSize();
     int ofx=(sz.width-siw)/2;
     int ofy=(sz.height-sih)/2;
     g.drawImage(ImageToShow,ofx,ofy,ofx+siw,ofy+sih,0,0,iw,ih,this);
  }
 }  private void jbInit() throws Exception{
     ImageToShow= //这里是得到Image的一些方法
     zoom= // 这里是计算缩放比例的一些方法
     JPanel_Image.paintComponents(ImageToShow.getGraphics());
     JPanel_Image.repaint();
  }}

解决方案 »

  1.   

    这程序是在MainFrame框架上画图.试试在JPanel_Image的paintComponents方法中画图.
      

  2.   

    JPanel好像没有加到Jframe里面去
    没看到Jframe.add(JPanel)
    我不知道在JB里面这样写也可以?
      

  3.   

    cfsego(陈传文) 说得比较正确,没有将JPanel加入到Jframe中。
      

  4.   

    刚才写的太急了,其实JFrame.add(JPanel)写的
    public class MainFrame extend JFrame{
     Image ImageToShow;
     float zoom;
     JPanel JPanel_Image=new JPanel(){
      {
         setBackground(Color.White);
         setOpaque(true);
      }
      public void paintComponents(Graphics g)
      {
         super.paintComponent(g);
         int iw=ImageToShow.getWidth(this);
         int ih=ImageToShow.getHeight(this);
         int siw=(int)Math.ceil((float)iw*zoom);
         int sih=(int)Math.ceil((float)ih*zoom);
         Dimension sz=getSize();
         int ofx=(sz.width-siw)/2;
         int ofy=(sz.height-sih)/2;
         g.drawImage(ImageToShow,ofx,ofy,ofx+siw,ofy+sih,0,0,iw,ih,this);
      }
     }
      public MainFrame() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }  private void jbInit() throws Exception{
         contentPane.add(JPanel_Image,new XYConstraints(9, 7, 491, 421));
         ImageToShow= //这里是得到Image的一些方法
         zoom= // 这里是计算缩放比例的一些方法
         JPanel_Image.paintComponents(ImageToShow.getGraphics());
         JPanel_Image.repaint();
      }}大家能帮我再看一下吗,JPanel的确能显示出来,但是图片显示不出来。
      

  5.   

    public void paintComponents(Graphics g)
      {
        把这里面换成 输出一行字看能不能输出 。测试一下。
      }
      

  6.   

    ImageToShow= //这里是得到Image的一些方法
         zoom= // 这里是计算缩放比例的一些方法
    这两行怎么不贴出来,这里出问题的几率很大。
      

  7.   

    编译可以通过的
    ImageToShow= //这里是得到Image的一些方法
         zoom= // 这里是计算缩放比例的一些方法
    这个部分的代码我觉得不是问题的关键,所以没有贴出来。
    jbuilder做的app,界面相应的代码和事件触发等等代码量很大,我就把和这个问题可能相关的代码整理以后贴出来了。
      

  8.   

    我今天又写了一个小例子,就以这个例子作为讨论的话题吧。package untitled1;import java.awt.BorderLayout;
    import java.awt.Dimension;import javax.swing.JFrame;
    import javax.swing.JPanel;
    import com.borland.jbcl.layout.XYLayout;
    import com.borland.jbcl.layout.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.Graphics.*;import com.sucem.io.*;/**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2005</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    public class Frame1 extends JFrame {
        JPanel contentPane;
        XYLayout xYLayout1 = new XYLayout();
        ImgFile img;
        Image image;
        JPanel jPanel1 = new JPanel(){
            public void paintComponents(Graphics g)
            {
                super.printComponents(g);
                g.drawImage(image,0,0,Color.cyan,this);
            }
        };
        public Frame1() {
            try {
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }    /**
         * Component initialization.
         *
         * @throws java.lang.Exception
         */
        private void jbInit() throws Exception {
            contentPane = (JPanel) getContentPane();
            contentPane.setLayout(xYLayout1);
            setSize(new Dimension(400, 300));
            setTitle("Frame Title");
            jPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
            img=new ImgFile("D:\\照片\\a6 003.jpg");
            image=img.GetImage();
            jPanel1.printComponents(image.getGraphics());
            this.repaint();
            contentPane.add(jPanel1, new XYConstraints(4, 4, 382, 286));
        }
    }package com.sucem.io;/**
     * <p>Title: 图片文件信息获取</p>
     *
     * <p>Description: 读取硬盘图像文件,获取相关信息</p>
     *
     * <p>Copyright: Copyright (c) 2005</p>
     *
     * <p>Company: 上海二手车交易市场</p>
     *
     * @author 孙汇洲
     * @version 1.0
     */
    import java.io.*;
    import java.awt.image.*;
    import java.util.*;
    import java.awt.image.*;
    import com.sun.image.codec.jpeg.*;public class ImgFile {
      public ImgFile() {
        try {
          jbInit();
        }
        catch (Exception ex) {
          ex.printStackTrace();
        }
      }  File image;
      double imgfileSizeK;
      int width,height;
      Date fileModifiedDate;
      BufferedImage bufImage;  public ImgFile(String ImageFileName) throws IOException, ImageFormatException,
          FileNotFoundException {
        image=new File(ImageFileName);
        GetImageFileSize();
        GetImageInfo();
        GetImageLastModifiedDate();
      }
      private void GetImageFileSize(){
        long imgfileSize=image.length();
        imgfileSizeK=(double)imgfileSize;
        imgfileSizeK=imgfileSizeK/1024*10;
        long filesize=Math.round(imgfileSizeK);
        imgfileSizeK=(double)filesize/10;
      }
      private void GetImageInfo() throws FileNotFoundException,
          ImageFormatException, IOException {
        InputStream fileIn=new FileInputStream(image);
        JPEGImageDecoder decoder=JPEGCodec.createJPEGDecoder(fileIn);
        bufImage=decoder.decodeAsBufferedImage();
        width=bufImage.getWidth();
        height=bufImage.getHeight();
      }
      private void GetImageLastModifiedDate(){
        fileModifiedDate=new Date(image.lastModified());
      }
      public double GetFileSize(){
        return imgfileSizeK;
      }
      public int Width(){
        return width;
      }
      public int Height(){
        return height;
      }
      public Date FileLastModifiedDate(){
        return fileModifiedDate;
      }
      public BufferedImage GetImage(){
        return bufImage;
      }
      private void jbInit() throws Exception {
      }
    }