Java问题,图片显示不了。

代码如下:importjavax.imageio.ImageIO;importjavax.swing.*;importjava.awt.*;importjava.awt.... 代码如下:import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
public class AppPanel extends JPanel {
//variables and components==========================
private JPanel imageBox;
private JPanel toolPane;
private JLabel counterLabel;
private int counter;
private boolean showWholeImage;
private JButton selectButton;
private JButton breakButton;
private JButton viewImageButton;
private Image[] images;
private Image wholeImage;
private JFileChooser chooser;
//constructor=============================
public AppPanel(){
//initial variables
chooser = new JFileChooser();
images=new BufferedImage[24];
wholeImage=null;
showWholeImage = true;
counter=0;
setLayout(new BorderLayout());
//initial components
imageBox = new JPanel();
imageBox.setBackground(Color.WHITE);
toolPane = new JPanel();
counterLabel = new JLabel("操作次数:"+counter);
viewImageButton = new JButton("显示原图");
selectButton = new JButton("选择图片");
selectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(JFileChooser.APPROVE_OPTION==chooser.showOpenDialog(null)){
try{
wholeImage = ImageIO.read(chooser.getSelectedFile());
showWholeImage=true;
repaint();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "打开文件失败");
}
}
}
});
breakButton = new JButton("打乱图片");
toolPane.add(counterLabel);
toolPane.add(selectButton);
toolPane.add(breakButton);
toolPane.add(viewImageButton);
add(toolPane,BorderLayout.SOUTH);
add(imageBox);
}
//paintComponents
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics g2d = imageBox.getGraphics();
if(showWholeImage && wholeImage!=null){
g2d.drawImage(wholeImage,0,0,imageBox);
}
else{
int x =0,y=0;
for(int i=0;i<24;i++){
g2d.drawImage(images[i],x, y, null);
}
}
}
}
展开
 我来答
legendmoon001
2012-05-13 · 超过14用户采纳过TA的回答
知道答主
回答量:57
采纳率:0%
帮助的人:30.6万
展开全部
问题应该是 你的paint方法 画完后又被其它的paint覆盖掉了,请参考下面修改的代码
把画板封装了一个新类,并在新类里面重新paint方法

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class AppPanel extends JPanel {
// variables and components==========================
private JPanel imageBox;
private JPanel toolPane;
private JLabel counterLabel;
private int counter;
private boolean showWholeImage;
private JButton selectButton;
private JButton breakButton;
private JButton viewImageButton;
private Image[] images;
private Image wholeImage;
private JFileChooser chooser;

public static void main(String[] args) {
JFrame f = new JFrame();
f.add(new AppPanel());

f.setBounds(200, 300, 400, 300);
f.setVisible(true);
}

// constructor=============================
public AppPanel() {
// initial variables
chooser = new JFileChooser();
images = new BufferedImage[24];
wholeImage = null;
showWholeImage = true;
counter = 0;
setLayout(new BorderLayout());
// initial components
imageBox = new paintPanel();
imageBox.setBackground(Color.WHITE);
toolPane = new JPanel();
counterLabel = new JLabel("操作次数:" + counter);
viewImageButton = new JButton("显示原图");
selectButton = new JButton("选择图片");
selectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if (JFileChooser.APPROVE_OPTION == chooser.showOpenDialog(null)) {
try {
wholeImage = ImageIO.read(chooser.getSelectedFile());
showWholeImage = true;
imageBox.repaint();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "打开文件失败");
}
}
}
});
breakButton = new JButton("打乱图片");
toolPane.add(counterLabel);
toolPane.add(selectButton);
toolPane.add(breakButton);
toolPane.add(viewImageButton);
add(toolPane, BorderLayout.SOUTH);
add(imageBox, BorderLayout.CENTER);
}

protected void paintImage(Graphics g) {
Graphics g2d = imageBox.getGraphics();
if (showWholeImage && wholeImage != null) {
g2d.drawImage(wholeImage, 0, 0, imageBox);
} else {
int x = 0, y = 0;
for (int i = 0; i < 24; i++) {
g2d.drawImage(images[i], x, y, null);
}
}
}

private class paintPanel extends JPanel {

public void paint(Graphics g) {
super.paint(g);
System.out.println("paint");
if (showWholeImage && wholeImage != null) {
g.drawImage(wholeImage, 0, 0, imageBox);
} else {
int x = 0, y = 0;
for (int i = 0; i < 24; i++) {
g.drawImage(images[i], x, y, null);
}
}
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式