我想将JPanel转换为图像.我使用以下方法:
public BufferedImage createImage(){
int w = getWidth();
int h = getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
paint(g);
return bi;
}
Run Code Online (Sandbox Code Playgroud)
但问题是JPanel包含在JScrollPane中.因此,当我将jpanel转换为图像时,图像仅包含jpanel中可见的部分,并且隐藏在滚动窗格内部的部分不包含在图像中.
是否有任何解决方案可以将JPanel的全部内容转换为图像?
我正在开发一个java程序,以便在注册时使用网络摄像头捕获员工图像.我可以毫无问题地获得图片,并将其保存在我的C:驱动器中,但在检索图像时,只有部分图像显示在标签上.有没有办法在保存之前重新调整JPEG大小?还是在显示之前?喜欢缩小它没有质量损失....
非常感谢Cheerz!:)!
好的伙计们......这里有: - 我已经按照我使用它们的方式对代码进行了评论.
//This method will capture the image from the interface and save it under the unique employee ID
public String captureImage(int picId){
FrameGrabbingControl ControlFG = (FrameGrabbingControl)
broadcast.getControl("javax.media.control.FrameGrabbingControl");
Buffer buffer = ControlFG.grabFrame();
BufferToImage image = new BufferToImage((VideoFormat)buffer.getFormat());
img = image.createImage(buffer);
path="c:\\employee"+picId+".jpg";
saveJPG(img,path);//method will save the image
return path;
}
public void saveJPG(Image img, String s){***//method will save the image***
System.out.println(s);
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img,null,null);
FileOutputStream out = null;
try{ …Run Code Online (Sandbox Code Playgroud) 我正在编写一个gui,我想使用嵌入一些图片,但在将它嵌入我的主程序之前,我编写了该代码来测试它:
public class guikopie extends javax.swing.JFrame{
public guikopie() {
a = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
a.setIcon(new javax.swing.ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulpen.jpg"));
add(a);//here i add it to the jlabel
pack();
}
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new guikopie().setVisible(true);
}
});
}
private javax.swing.JLabel a;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么这段代码不显示图片?
我是Java新手.我只是想加载图像作为背景JFrame.我想要做的是从C驱动器(这不是我的工作区)获取图像所以我做了什么Board.java:
ImageIcon i = new ImageIcon("C:/image.png");
img =i.getImage();
Run Code Online (Sandbox Code Playgroud)
并尝试绘制这样的东西:
public void paint(Graphics g )
{
super.paint(g);
Graphics2D g2d= (Graphics2D) g;
g2d.drawImage(img, 0, 100, null);
}
Run Code Online (Sandbox Code Playgroud)
然后我就像这样在我的主要课堂上打电话
public static void main(String[] args)
{
JFrame frame= new JFrame(" Game") ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 365);
frame.setVisible(true);
frame.add(new Board());
}
Run Code Online (Sandbox Code Playgroud)
但我没有显示任何图像,这是合法的添加方式Image吗?
我创建了一个测试版本来简化我为你们提出的问题:
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel l = new JLabel("hello");
JPanel cp;
public Test(){
setContentPane(cp = new JPanel());
cp.add(l);
setSize(200, 200);
setVisible(true);
}
public void paint(Graphics g){
//do absolutely nothing
}
}
Run Code Online (Sandbox Code Playgroud)
当你启动程序时,你会看到一个完美的空白窗口.但是,如果删除paint方法,JLabel会显示出来!(我不得不长时间搜索这个bug,真的).现在,我如何启用一个窗口来使用图形和定期绘制组件?提前致谢