将图像加载到applet中的JPanel中

时间:2013-05-13 19:08:25

标签: java image swing paintcomponent jfilechooser

基于此主题:Java: Load image from file, edit and add to JPanel 我试图将na图像加载到JPanel但它没有绘制它,所以这里是交易,因为建议我创建了一个名为JImageComponent的新类文件,它扩展自JComponent,如下所示:

package in.co.sneh;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
private BufferedImage img;
    public class JImageComponent  extends JComponent
{
    public JImageComponent (BufferedImage bi)
{
    img = bi;
}

@Override
public void paintComponent(Graphics g)
{
    g.drawImage(img, 0, 0, this);
}
}

然后在applet表单类中,当我单击loadPicture按钮时,执行的操作如下所示:

JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
   int retVal = chooser.showDialog(AppletForm.this, "Attach");
    if (retVal == JFileChooser.APPROVE_OPTION) {
        picFile = chooser.getSelectedFile();
        try {

            image = ImageIO.read(picFile);
            JImageComponent jcomp = new JImageComponent(image);
            Graphics2D g = image.createGraphics();
            jcomp.paintComponents(g);
            // Draw here on the graphics
            //g.dispose();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
            jPanel6.add(jcomp);
            System.out.println("Testing" + picFile);



        } catch (IOException e) {

            e.printStackTrace();
        }

对我来说一切都已到位然而在文件浏览器中选择图像时图像没有显示,有人可以帮我吗? 提前致谢, Romulo Romero

2 个答案:

答案 0 :(得分:3)

将新组件添加到容器后,您需要调用

jPanel6.revalidate();
jPanel6.repaint();

除非签名,否则applet无法从本地文件系统加载资源。

注意: 虽然只有一个JImageComponent会更好,JLabel组件已经提供了使用setIcon切换图像的功能。

答案 1 :(得分:1)

这是一个简单的applet代码,可以帮助你编辑图像......

you can try it

/*Arpana*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class ConvolveApp extends JFrame {
  CPanel displayPanel;

  JButton sharpenButton, blurringButton, edButton, resetButton;

  public ConvolveApp() {
    super();
    Container container = getContentPane();

    displayPanel = new CPanel();
    container.add(displayPanel);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(2, 2));
    panel
        .setBorder(new TitledBorder(
            "Click a Button to Perform the Associated Operation and Reset..."));

    sharpenButton = new JButton("Sharpen");
    sharpenButton.addActionListener(new ButtonListener());
    blurringButton = new JButton("Blur");
    blurringButton.addActionListener(new ButtonListener());
    edButton = new JButton("Edge Detect");
    edButton.addActionListener(new ButtonListener());
    resetButton = new JButton("Reset");
    resetButton.addActionListener(new ButtonListener());

    panel.add(sharpenButton);
    panel.add(blurringButton);
    panel.add(edButton);
    panel.add(resetButton);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
     //setSize(100,100);
    setSize(displayPanel.getWidth()+50, displayPanel.getHeight() );
    setVisible(true); 
  }
  public static void main(String arg[]) {
    new ConvolveApp();
  }

  class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JButton button = (JButton) e.getSource();

      if (button.equals(sharpenButton)) {
        displayPanel.sharpen();
        displayPanel.repaint();
      } else if (button.equals(blurringButton)) {
        displayPanel.blur();
        displayPanel.repaint();
      } else if (button.equals(edButton)) {
        displayPanel.edgeDetect();
        displayPanel.repaint();
      } else if (button.equals(resetButton)) {
        displayPanel.reset();
        displayPanel.repaint();
      }
    }
  }
}

class CPanel extends JLabel {
  Image displayImage;

  BufferedImage biSrc;

  BufferedImage biDest; // Destination image is mandetory.

  BufferedImage bi; // Only an additional reference.

  Graphics2D big;

  CPanel() {
    setBackground(Color.black);
    loadImage();
    setSize(displayImage.getWidth(this), displayImage.getWidth(this)); 
    createBufferedImages();
    bi = biSrc;
  }

  public void loadImage() {
    displayImage = Toolkit.getDefaultToolkit().getImage("Desert.jpg");
    MediaTracker mt = new MediaTracker(this);
    mt.addImage(displayImage, 1);
    try {
      mt.waitForAll();
    } catch (Exception e) {
      System.out.println("Exception while loading.");
    }
    if (displayImage.getWidth(this) == -1) {
      System.out.println("No jpg file");
      System.exit(0);
    }
  }

  public void createBufferedImages() {
    biSrc = new BufferedImage(displayImage.getWidth(this), displayImage
        .getHeight(this), BufferedImage.TYPE_INT_RGB);

    big = biSrc.createGraphics();
    big.drawImage(displayImage, 0, 0, this);

    biDest = new BufferedImage(displayImage.getWidth(this), displayImage
        .getHeight(this), BufferedImage.TYPE_INT_RGB);
  }

  public void sharpen() {
    float data[] = { -1.0f, -1.0f, -1.0f, -1.0f, 9.0f, -1.0f, -1.0f, -1.0f,
        -1.0f };
    Kernel kernel = new Kernel(3, 3, data);
    ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
        null);
    convolve.filter(biSrc, biDest);
    bi = biDest;
  }

  public void blur() {
    float data[] = { 0.0625f, 0.125f, 0.0625f, 0.125f, 0.25f, 0.125f,
        0.0625f, 0.125f, 0.0625f };
    Kernel kernel = new Kernel(3, 3, data);
    ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
        null);
    convolve.filter(biSrc, biDest);
    bi = biDest;
  }

    public void edgeDetect() {
    float data[] = { 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f,
        -1.0f };

    Kernel kernel = new Kernel(3, 3, data);
    ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,
        null);
    convolve.filter(biSrc, biDest);

    bi = biDest;
  }

  public void reset() {
    big.setColor(Color.black);
    big.clearRect(0, 0, bi.getWidth(this), bi.getHeight(this));
    big.drawImage(displayImage, 0, 0, this);
    bi = biSrc;
  }

  public void update(Graphics g) {
    g.clearRect(0, 0, getWidth(), getHeight());
    paintComponent(g);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2D) g;

    g2D.drawImage(bi, 0, 0, this);
  }
}