これはJFrame
package client.connection;
import java.awt.Dimension;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JFrame;
class DrawFrameRemoteControl extends JFrame
{
private DrawPanelRemoteControl imagePanel;
private ClientRemoteControlConnection clientRemoteControlConnection;
private ObjectInputStream clientInputStream;
private ObjectOutputStream clientOutputStream;
private Dimension imageDimension;
private Dimension serverDimension;
public DrawFrameRemoteControl(Dimension imageDimension,ClientRemoteControlConnection clientRemoteControlConnection,ObjectInputStream clientInputStream,ObjectOutputStream clientOutputStream,Dimension serverDimension)
{
super("Remote Desktop Control");
this.clientRemoteControlConnection=clientRemoteControlConnection;
this.clientInputStream=clientInputStream;
this.clientOutputStream=clientOutputStream;
this.imageDimension=imageDimension;
this.serverDimension=serverDimension;
imagePanel=new DrawPanelRemoteControl(imageDimension);
add(imagePanel);
setSize(imageDimension.width,imageDimension.height);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
void drawNewImageGrayscale(byte[] array)
{
imagePanel.setNewImageGrayscale(array);
imagePanel.repaint();
}
}
そしてこれは拡張JPanelクラスです-
package client.connection;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.MemoryImageSource;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
class DrawPanelRemoteControl extends JPanel
{
private byte[] byteArray=null;
private Image image;
private JLabel imageLabel=new JLabel();
private Dimension imageDimension;
public DrawPanelRemoteControl(Dimension imageDimension)
{
this.imageDimension=imageDimension;
add(imageLabel);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println(".");
if(byteArray!=null)
{
image=getGrayscaleImageFromArray(byteArray,imageDimension.width,imageDimension.height);
imageLabel.setIcon(new ImageIcon(image));
}
}
private Image getGrayscaleImageFromArray(byte[] buffer, int width, int height)
{
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
int[] nBits = { 8 };
ColorModel cm = new ComponentColorModel(cs, nBits, false, true,Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
SampleModel sm = cm.createCompatibleSampleModel(width, height);
DataBufferByte db = new DataBufferByte(buffer, width * height);
WritableRaster raster = Raster.createWritableRaster(sm, db, null);
BufferedImage result = new BufferedImage(cm, raster, false, null);
return result;
}
void setNewImageGrayscale(byte[] array)
{
this.byteArray=array;
this.intArray=null;
}
}
コードのデバッグを試みましたが、imagePanel.repaint()
何度も実行されていますが、プログラムがクラスのpaintComponent()
メソッドに到達することはありません。DrawPanelRemoteControl
なぜこれが起こっているのか誰かが私に何か考えを与えることができますか?imageDimension
オブジェクトと関係がありますか?
追加情報:main()
メソッドでは、DrawFrameRemoteControl
オブジェクトが作成され、そのメソッドは毎秒drawNewImageGrayscale(byte[] arr)
更新されます。main()