File file=null;
Image image=null;
JFileChooser fileChooser1 = new JFileChooser();
if(fileChooser1.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
file = fileChooser1.getSelectedFile(); //在GUI中选择文件
try {
image = ImageIO.read(file); //读入图像到image
//setLayout(new GridLayout(2,0,5,5));
//add(new ImageViewer(image));
Graphics g=null;
super.paintComponents(g);
if(image!=null){
g.drawImage(image,0,0,getWidth(),getHeight(),this); //画出图像
}
} catch (IOException ex) {
Logger.getLogger(graphics.class.getName()).log(Level.SEVERE, null, ex);
}
}
else{
System.out.println("NO");
}
java从GUI中读入图像并显示,求解答
答案:2 悬赏:60
解决时间 2021-03-21 14:28
- 提问者网友:幽瑟玉琼情殇
- 2021-03-21 02:01
最佳答案
- 二级知识专家网友:一池湖水
- 2021-03-21 03:34
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class BackgroundJPanel extends JPanel {
private String imagePath = "";
private Image img = null;
private int widths = 0;
private int heights = 0;
public BackgroundJPanel() {
super();
}
@Override
protected void paintComponent(Graphics g) {
if (img != null) {
MediaTracker media = new MediaTracker(this);
media.addImage(img, 0);
try {
media.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
int width = getWidth();
int height = getHeight();
if (widths != 0) {
width = widths;
}
if (heights != 0) {
height = heights;
}
// logger.debug(width + "," + height);
g.drawImage(img, 0, 0, width, height, this);
}
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
if (!imagePath.isEmpty()) {
img = Toolkit.getDefaultToolkit().getImage(
getClass().getResource(imagePath));
}
this.repaint();
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public int getWidths() {
return widths;
}
public void setWidths(int widths) {
this.widths = widths;
}
public int getHeights() {
return heights;
}
public void setHeights(int heights) {
this.heights = heights;
}
}
这个方法是设置 JPANEL的背景图 你可以参考下
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class BackgroundJPanel extends JPanel {
private String imagePath = "";
private Image img = null;
private int widths = 0;
private int heights = 0;
public BackgroundJPanel() {
super();
}
@Override
protected void paintComponent(Graphics g) {
if (img != null) {
MediaTracker media = new MediaTracker(this);
media.addImage(img, 0);
try {
media.waitForID(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
int width = getWidth();
int height = getHeight();
if (widths != 0) {
width = widths;
}
if (heights != 0) {
height = heights;
}
// logger.debug(width + "," + height);
g.drawImage(img, 0, 0, width, height, this);
}
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
if (!imagePath.isEmpty()) {
img = Toolkit.getDefaultToolkit().getImage(
getClass().getResource(imagePath));
}
this.repaint();
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public int getWidths() {
return widths;
}
public void setWidths(int widths) {
this.widths = widths;
}
public int getHeights() {
return heights;
}
public void setHeights(int heights) {
this.heights = heights;
}
}
这个方法是设置 JPANEL的背景图 你可以参考下
全部回答
- 1楼网友:时光挺欠揍
- 2021-03-21 04:07
你好!
fileChooser1.showOpenDialog(null)
得到的路径、得到Image,在JFrame的paint事件中,绘上去
如有疑问,请追问。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯