如何返工我的Java applet代码使用一个JPanel,而不是一个JFrame的?(How do

2019-10-17 12:16发布

我只是学习Java的一个.NET程序员。 我一直在这个问题上,过去2周...

这里是我的代码,以显示我的保安系统摄像头和更新的小应用程序运行在一个JFrame的图片:(我需要这个代码在一个JPanel工作转换,而不是!)

public class Streamer extends JApplet {
String PATH = "C:/Security/CamCap/";
Integer UPDATE_INTERVAL = 100;
String CAM1FILE = "current1.png";
String CAM2FILE = "current2.png";
String CAM3FILE = "current3.png";
String CAM4FILE = "current4.png";
String CAM5FILE = "current5.png";
String CAM6FILE = "current6.png";
String CAM7FILE = "current7.png";
String CAM8FILE = "current8.png";
String LOGOFILE = "logo.png";

private String path1 = PATH + CAM1FILE;
private String path2 = PATH + CAM2FILE;
private String path3 = PATH + CAM3FILE;
private String path4 = PATH + CAM4FILE;
private String path5 = PATH + CAM5FILE;
private String path6 = PATH + CAM6FILE;
private String path7 = PATH + CAM7FILE;
private String path8 = PATH + CAM8FILE;
private String pathLogo = PATH + LOGOFILE;

JFrame frame = new JFrame("@Home - Live Video");
boolean loaded = false;

JLabel label1, label2, label3, label4, label5, label6, label7, label8, labelLogo;


public void init()
{
    frame.getContentPane().setBackground(Color.BLACK);
    frame.setMinimumSize(new Dimension(1087, 777));
    frame.setLayout(new GridLayout(3, 3, 2, 2));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1090,780);
    frame.getContentPane().setSize(1087, 777);
    frame.setLocationRelativeTo ( null );
    frame.setVisible(true); 


    //INITIALIZE CAMERA 1
ImageIcon icon1 = new ImageIcon();
    loaded = false;
    while(!loaded)
    {
        try {
            icon1 = new ImageIcon(ImageIO.read(new File(path1)));
            loaded = true;
        } catch (Exception ex) { loaded = false; }
    }

    label1 = new JLabel(icon1);
    frame.add(label1);


    //INITIALIZE CAMERA 2
ImageIcon icon2 = new ImageIcon();
    loaded = false;
    while(!loaded)
    {
        try {
            icon2 = new ImageIcon(ImageIO.read(new File(path2)));
            loaded = true;
        } catch (Exception ex) { loaded = false; }
    }

    label2 = new JLabel(icon2);
    frame.add(label2);


    //INITIALIZE CAMERA 3
ImageIcon icon3 = new ImageIcon();
    loaded = false;
    while(!loaded)
    {
        try {
            icon3 = new ImageIcon(ImageIO.read(new File(path3)));
            loaded = true;
        } catch (Exception ex) { loaded = false; }
    }

    label3 = new JLabel(icon3);
    frame.add(label3);



    //INITIALIZE CAMERA 4
ImageIcon icon4 = new ImageIcon();
    loaded = false;
    while(!loaded)
    {
        try {
            icon4 = new ImageIcon(ImageIO.read(new File(path4)));
            loaded = true;
        } catch (Exception ex) { loaded = false; }
    }

    label4 = new JLabel(icon4);
    frame.add(label4);


    //INITIALIZE CAMERA 5
ImageIcon icon5 = new ImageIcon();
    loaded = false;
    while(!loaded)
    {
        try {
            icon5 = new ImageIcon(ImageIO.read(new File(path5)));
            loaded = true;
        } catch (Exception ex) { loaded = false; }
    }

    label5 = new JLabel(icon5);
    frame.add(label5);


    //INITIALIZE CAMERA 6
ImageIcon icon6 = new ImageIcon();
    loaded = false;
    while(!loaded)
    {
        try {
            icon6 = new ImageIcon(ImageIO.read(new File(path6)));
            loaded = true;
        } catch (Exception ex) { loaded = false; }
    }

    label6 = new JLabel(icon6);
    frame.add(label6);


    //INITIALIZE CAMERA 7
ImageIcon icon7 = new ImageIcon();
    loaded = false;
    while(!loaded)
    {
        try {
            icon7 = new ImageIcon(ImageIO.read(new File(path7)));
            loaded = true;
        } catch (Exception ex) { loaded = false; }
    }

    label7 = new JLabel(icon7);
    frame.add(label7);


    //INITIALIZE CAMERA 8
ImageIcon icon8 = new ImageIcon();
    loaded = false;
    while(!loaded)
    {
        try {
            icon8 = new ImageIcon(ImageIO.read(new File(path8)));
            loaded = true;
        } catch (Exception ex) { loaded = false; }
    }

    label8 = new JLabel(icon8);
    frame.add(label8);


    //INITIALIZE LOGO
ImageIcon iconLogo = new ImageIcon();
        try {
            iconLogo = new ImageIcon(ImageIO.read(new File(pathLogo)));
            loaded = true;
        } catch (Exception ex) { loaded = false; }

    labelLogo = new JLabel(iconLogo);
    frame.add(labelLogo);

    frame.setVisible(true);
    run();
}

public void run()
{
    while(true)
    {
        try {
            ImageIcon icon1 = new ImageIcon(ImageIO.read(new File(path1)));
            label1.setIcon(icon1);
            label1.repaint();
        } catch (Exception ex) {}

        try {
            ImageIcon icon2 = new ImageIcon(ImageIO.read(new File(path2)));
            label2.setIcon(icon2);
            label2.repaint();
        } catch (Exception ex) {}

        try {
            ImageIcon icon3 = new ImageIcon(ImageIO.read(new File(path3)));
            label3.setIcon(icon3);
            label3.repaint();
         } catch (Exception ex) {}

        try {
            ImageIcon icon4 = new ImageIcon(ImageIO.read(new File(path4)));
            label4.setIcon(icon4);
            label4.repaint();
        } catch (Exception ex) {}

        try {
            ImageIcon icon5 = new ImageIcon(ImageIO.read(new File(path5)));
            label5.setIcon(icon5);
            label5.repaint();
        } catch (Exception ex) {}

        try {
            ImageIcon icon6 = new ImageIcon(ImageIO.read(new File(path6)));
            label6.setIcon(icon6);
            label6.repaint();
        } catch (Exception ex) {}

        try {
            ImageIcon icon7 = new ImageIcon(ImageIO.read(new File(path7)));
            label7.setIcon(icon7);
            label7.repaint();
        } catch (Exception ex) {}

        try {
            ImageIcon icon8 = new ImageIcon(ImageIO.read(new File(path8)));
            label8.setIcon(icon8);
            label8.repaint();
        } catch (Exception ex) {}

        try {            
            Thread.sleep(UPDATE_INTERVAL);
        } catch (Exception ex) {}
        }
    }
}

我尝试了一切,我不能找到一个办法把这段代码转换到一个JPanel内工作。 我需要在我的小程序,使用一个JPanel,这样我的相机并不在一个新的,单独的窗口(JFrame的)出现。

有谁知道如何把这段代码转换(只是一小部分)来绘制和刷新我的图片在一个JPanel?

提前致谢)!

Answer 1:

使用在使用动画一个Swing定时器一个JLabel的GUI的一个示例,并且被显示在一个JApplet的:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class SpriteAnimationApplet extends JApplet {
   private static final String SPRITE_SHEET_SPEC = "http://www.funorb.com/img/images/game/"
         + "central/dev_diary/sprite_sheet_full.gif";
   private static final int SPRITE_ROWS = 8; // an 8 x 8 sprite sheet

   @Override
   public void init() {
      try {
         final Icon[] icons = SpriteIO.getSprites(SPRITE_SHEET_SPEC, SPRITE_ROWS);
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               SpriteAnimationPanel spritePanel = new SpriteAnimationPanel(icons);
               getContentPane().add(spritePanel);
               spritePanel.startAnimation();
            }
         });
      } catch (InvocationTargetException e) {
         e.printStackTrace();
         System.exit(-1);
      } catch (InterruptedException e) {
         e.printStackTrace();
         System.exit(-1);
      } catch (MalformedURLException e) {
         e.printStackTrace();
         System.exit(-1);
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }
   }

}

class SpriteIO {
   public static Icon[] getSprites(String spriteSheetSpec, int spriteRows)
         throws MalformedURLException, IOException {
      Icon[] icons = new Icon[spriteRows * spriteRows];
      URL spriteSheetUrl = new URL(spriteSheetSpec);
      BufferedImage spriteSheet = ImageIO.read(spriteSheetUrl);
      double wD = (double) spriteSheet.getWidth() / spriteRows;
      double hD = (double) spriteSheet.getHeight() / spriteRows;
      int w = (int) wD;
      int h = (int) hD;
      for (int i = 0; i < spriteRows; i++) {
         for (int j = 0; j < spriteRows; j++) {
            int x = (int) (i * wD);
            int y = (int) (j * hD);
            BufferedImage img = spriteSheet.getSubimage(x, y, w, h);

            icons[j * spriteRows + i] = new ImageIcon(img);
         }
      }
      return icons;
   }
}

@SuppressWarnings("serial")
class SpriteAnimationPanel extends JPanel {
   private static final int TIMER_DELAY = 200;
   private Icon[] icons;
   private JLabel animationLabel = new JLabel();

   public SpriteAnimationPanel(Icon[] icons) {
      this.icons = icons;
      setLayout(new BorderLayout());
      add(animationLabel );
   }

   public void startAnimation() {
      Timer spriteTimer = new Timer(TIMER_DELAY, new ActionListener() {
         private int iconIndex = 0;

         @Override
         public void actionPerformed(ActionEvent arg0) {
            animationLabel.setIcon(icons[iconIndex]);
            iconIndex++;
            iconIndex %= icons.length;
         }
      });
      spriteTimer.start();
   }
}


Answer 2:

ImageIcon icon5 = new ImageIcon(ImageIO.read(new File(path5)));

Applet的资源通常是由小应用程序的运行时类路径或家庭服务器加载。 避免使用File在applet的对象,除非他们通过从用户提供JFileChooser (不是这里的情况)。

ImageIcon icon5 = new ImageIcon(this.getClass().getResource(href5));

网址资源可相对于小程序的文件或代码库来形成。

提示

如果有疑问,打印出来!

对于每一个catch转储堆栈跟踪。

} catch (Exception ex) {
    ex.printStackTrace();
    //..

留意输出

确保Java控制台时,浏览器启动一个applet中。

使用Swing的执行动画Timer

通过HFOE如所讨论/所示。

图片缓存

在小应用程序图像通常缓存。 看来小程序加载图像多次,希望每次(安全摄像机的最后一帧),它是不同的。 要解决这个问题,加载图像作为byte[]并把它变成一个ByteArrayInputStream 。 然后从使用流加载图像ImageIO

由于JRE不能映射byte[]的图像到File路径或URL ,它不能被高速缓存。



文章来源: How do I rework my Java applet code to use a JPanel instead of a JFrame?