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();
}}
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();
}}
解决方案 »
- 请教个新手问题,关于System.out.print("\t")的
- int和byte
- 求指导,写的程序右下边报错
- html如何引用css和javascript(相对路径)
- 大家帮忙写个程序
- JTextField中的数字
- 为何在login.jsp表单处理switch.jsp中执行JSP:forward page="A.jsp"后,IE地址栏中为.../switch.jsp,不是.../A.jsp?
- java中如何显示模态对话框和非模态对话框?
- 不知是不是刷新的问题,显示不出来
- getActionCommand()是什么东东?
- 请教一个applet读数据库的问题
- java有没有方法从内存中去掉某些已经加载的类?
没看到Jframe.add(JPanel)
我不知道在JB里面这样写也可以?
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的确能显示出来,但是图片显示不出来。
{
把这里面换成 输出一行字看能不能输出 。测试一下。
}
zoom= // 这里是计算缩放比例的一些方法
这两行怎么不贴出来,这里出问题的几率很大。
ImageToShow= //这里是得到Image的一些方法
zoom= // 这里是计算缩放比例的一些方法
这个部分的代码我觉得不是问题的关键,所以没有贴出来。
jbuilder做的app,界面相应的代码和事件触发等等代码量很大,我就把和这个问题可能相关的代码整理以后贴出来了。
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 {
}
}