我读过更多阅读和阅读只会导致更多的混乱。这是我的问题:我在模型视图控制器设计之后有三个类。我无法从模型加载图像并显示在视图上。我已经读过使用图标,从bufferedimage复制到bufferedimage,在jpanel上显示它但没有一个工作。继承人基本纲要:
型号:
import java.awt.Graphics;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;
class Testee{
final public String desktopImage = "TestPicture.jpg";
private String imageURI;
private static BufferedImage bImage;
private Graphics graphics;
private int xLocation, yLocation;
public Testee(){
}
public void setImageURI(String uri){
imageURI = uri;
}
public Graphics getImageGraphics(){
return graphics;
}
public BufferedImage getImage(){
return bImage;
}
public void setImageBuffer(String imageName){
try{
bImage = ImageIO.read(new File(imageName));
graphics = bImage.createGraphics();
}
catch(Exception ex){}
}
public void clearImage(){
bImage = null;
graphics = null;
}
public void setSelectedLocation(int x, int y){
xLocation = x;
yLocation = y;
}
public int getSelectedX(){
return xLocation;
}
public int getSelectedY(){
return yLocation;
}
public int getPixelCount(BufferedImage image){
if(image != null){
int pixelCount = image.getWidth() * image.getHeight();
return pixelCount;
}
else
return 0;
}
public int getSelectedPixel(BufferedImage image, int x, int y){
if(image != null){
int pixelNumber;
int IMAGEWIDTH = image.getWidth();
//if(y <= 1){
//needs work
//}
pixelNumber = (IMAGEWIDTH * (y - 1)) + x;
return pixelNumber;
}
else{
return 0;
}
}
public int getPixelLocation(BufferedImage image, int x, int y){
if(image != null){
return image.getRGB(x, y);
}
else{
return 0;
}
}
public int getPixelRGB(int pixel){
int rgb = pixel;
return rgb;
}
public int getPixelAlpha(int pixel){
int alpha = (pixel >> 24) & 0xff;
return alpha;
}
public int getPixelRed(int pixel){
int red = (pixel >> 16) & 0xff;
return red;
}
public int getPixelGreen(int pixel){
int green = (pixel >> 8) & 0xff;
return green;
}
public int getPixelBlue(int pixel){
int blue = (pixel) & 0xff;
return blue;
}
void blankSlate(){
clearImage();
graphics = null;
}
}
查看:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
class Testing{
private Testee testee;
private JFrame mainFrame;
private JPanel mainPane;
private Thread canvasThread;
private JPanel picturePane;
private JPanel canvas;
private JPanel imageControlPane;
private JPanel imageButtonHolder;
private JButton uploadButton;
private JButton clearButton;
private JPanel infoPane;
private JPanel pixelCountPane;
private JPanel pixelLocationPane;
private JPanel pixelSelectedPane;
private JPanel pixelRGBPane;
private JPanel pixelAlphaPane;
private JPanel pixelRedPane;
private JPanel pixelGreenPane;
private JPanel pixelBluePane;
private JPanel pixelColorPane;
final private JLabel pixelCountLabel = new JLabel("# of pixels");
private JTextField pixelCountText;
final private JLabel pixelSelectedLabel = new JLabel("pixel selected");
private JTextField pixelSelectedText;
final private JLabel pixelLocationLabel = new JLabel("pixel location");
private JTextField pixelLocationText;
final private JLabel pixelRGBLabel = new JLabel("pixel RGB");
private JTextField pixelRGBText;
final private JLabel pixelAlphaLabel = new JLabel("Alpha");
private JTextField pixelAlphaText;
final private JLabel pixelRedLabel = new JLabel("Red");
private JTextField pixelRedText;
final private JLabel pixelGreenLabel = new JLabel("Green");
private JTextField pixelGreenText;
final private JLabel pixelBlueLabel = new JLabel("Blue");
private JTextField pixelBlueText;
public Testing(Testee model){
testee = model;
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run(){
setupGUI();
}
});
} catch (InterruptedException | InvocationTargetException ex) {
System.exit(1);
}
}
private void setupGUI(){
System.out.println("Created GUI on EDT? "
+ SwingUtilities.isEventDispatchThread());
mainFrame = new JFrame();
mainPane = new JPanel(new BorderLayout());
mainFrame.setTitle(this.getClass().getSimpleName());
mainFrame.setSize(500,500);
mainFrame.setResizable(false);
mainFrame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
picturePane = new JPanel(new FlowLayout());
canvas = new DrawBoard();
generateCanvas();
picturePane.add(canvas);
mainPane.add(picturePane,BorderLayout.CENTER);
imageControlPane = new JPanel(new BorderLayout());
imageButtonHolder = new JPanel(new GridLayout(1,2,10,5));
uploadButton = new JButton("UPLOAD");
clearButton = new JButton("CLEAR");
imageButtonHolder.add(uploadButton);
imageButtonHolder.add(clearButton);
imageControlPane.add(imageButtonHolder, BorderLayout.WEST);
mainPane.add(imageControlPane, BorderLayout.SOUTH);
infoPane = new JPanel();
BoxLayout boxLayout = new BoxLayout(infoPane, BoxLayout.Y_AXIS);
infoPane.setLayout(boxLayout);
pixelCountPane = new JPanel(new BorderLayout(5,5));
pixelCountText = new JTextField(8);
pixelCountText.setEditable(false);
pixelCountPane.add(pixelCountLabel, BorderLayout.WEST);
pixelCountPane.add(pixelCountText, BorderLayout.EAST);
infoPane.add(pixelCountPane);
pixelSelectedPane = new JPanel(new BorderLayout(5,5));
pixelSelectedText = new JTextField(8);
pixelSelectedText.setEditable(false);
pixelSelectedPane.add(pixelSelectedLabel, BorderLayout.WEST);
pixelSelectedPane.add(pixelSelectedText, BorderLayout.EAST);
infoPane.add(pixelSelectedPane);
pixelLocationPane = new JPanel(new BorderLayout(5,5));
pixelLocationText = new JTextField(8);
pixelLocationText.setEditable(false);
pixelLocationPane.add(pixelLocationLabel, BorderLayout.WEST);
pixelLocationPane.add(pixelLocationText, BorderLayout.EAST);
infoPane.add(pixelLocationPane);
pixelRGBPane = new JPanel(new BorderLayout(5,5));
pixelRGBText = new JTextField(8);
pixelRGBText.setEditable(false);
pixelRGBPane.add(pixelRGBLabel, BorderLayout.WEST);
pixelRGBPane.add(pixelRGBText, BorderLayout.EAST);
infoPane.add(pixelRGBPane);
pixelAlphaText = new JTextField(8);
pixelAlphaText.setEditable(false);
pixelRedText = new JTextField(8);
pixelRedText.setEditable(false);
pixelGreenText = new JTextField(8);
pixelGreenText.setEditable(false);
pixelBlueText = new JTextField(8);
pixelBlueText.setEditable(false);
//pixelColorPane = new JPanel(new GridLayout(4,1));
pixelAlphaPane = new JPanel(new BorderLayout(5,5));
pixelAlphaPane.add(pixelAlphaLabel, BorderLayout.WEST);
pixelAlphaPane.add(pixelAlphaText, BorderLayout.EAST);
infoPane.add(pixelAlphaPane);
pixelRedPane = new JPanel(new BorderLayout(5,5));
pixelRedPane.add(pixelRedLabel, BorderLayout.WEST);
pixelRedPane.add(pixelRedText, BorderLayout.EAST);
infoPane.add(pixelRedPane);
pixelGreenPane = new JPanel(new BorderLayout(5,5));
pixelGreenPane.add(pixelGreenLabel, BorderLayout.WEST);
pixelGreenPane.add(pixelGreenText, BorderLayout.EAST);
infoPane.add(pixelGreenPane);
pixelBluePane = new JPanel(new BorderLayout(5,5));
pixelBluePane.add(pixelBlueLabel, BorderLayout.WEST);
pixelBluePane.add(pixelBlueText, BorderLayout.EAST);
infoPane.add(pixelBluePane);
//infoPane.add(pixelColorPane);
mainPane.add(infoPane,BorderLayout.EAST);
mainFrame.getContentPane().add(mainPane);
mainFrame.setVisible(true);
}
public class DrawBoard extends JPanel{
BufferedImage image;
public DrawBoard(){
image = testee.getImage();
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
private void generateCanvas(){
canvasReset();
canvasThread = new Thread(new Runnable(){
@Override
public void run() {
}
});
canvasThread.start();
}
public JPanel getCanvas(){
return canvas;
}
public void canvasReset(){
canvas.setPreferredSize(new Dimension(300,300));
canvas.setBackground(Color.white);
canvas.repaint();
}
void setPixelCount(String text){
pixelCountText.setText(text);
}
void setPixelSelected(String text){
pixelSelectedText.setText(text);
}
void setPixelLocation(int x, int y){
pixelLocationText.setText(x + ", " + y);
}
void setPixelRGB(String text){
pixelRGBText.setText(text);
}
void setPixelAlpha(String text){
pixelAlphaText.setText(text);
}
void setPixelRed(String text){
pixelRedText.setText(text);
}
void setPixelGreen(String text){
pixelGreenText.setText(text);
}
void setPixelBlue(String text){
pixelBlueText.setText(text);
}
void canvasAction(MouseListener mla){
canvas.addMouseListener(mla);
}
void uploadAction(ActionListener ual){
uploadButton.addActionListener(ual);
}
void clearAction(ActionListener al){
clearButton.addActionListener(al);
}
void blankGUI(){
canvasReset();
pixelCountText.setText(null);
pixelSelectedText.setText(null);
pixelLocationText.setText(null);
pixelRGBText.setText(null);
pixelAlphaText.setText(null);
pixelRedText.setText(null);
pixelBlueText.setText(null);
pixelGreenText.setText(null);
}
}
控制器:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
class Tester{
public Testing testing;
public Testee testee;
public Tester(Testing gui, Testee model){
testing = gui;
testee = model;
gui.uploadAction(new Upload());
gui.clearAction(new Clear());
gui.canvasAction(new Mouse());
}
public class Upload implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
testee.setImageBuffer(testee.desktopImage);
testing.setPixelCount(Integer.toString(testee.getPixelCount(testee.getImage())));
if(testee.getImage() != null){
}
}
}
public class Clear implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
testing.blankGUI();
testee.blankSlate();
}
}
public class Mouse extends MouseAdapter{
@Override
public void mouseClicked(MouseEvent me){
testee.setSelectedLocation(me.getX(), me.getY());
testing.setPixelLocation(testee.getSelectedX(), testee.getSelectedY());
testing.setPixelSelected(Integer.toString(testee.getSelectedPixel(testee.getImage(), testee.getSelectedX(), testee.getSelectedY())));
try{
int pixelColor = testee.getPixelLocation(testee.getImage(), testee.getSelectedX(), testee.getSelectedY());
testing.setPixelRGB(Integer.toString(testee.getPixelRGB(pixelColor)));
testing.setPixelAlpha(Integer.toString(testee.getPixelAlpha(pixelColor)));
testing.setPixelRed(Integer.toString(testee.getPixelRed(pixelColor)));
testing.setPixelGreen(Integer.toString(testee.getPixelGreen(pixelColor)));
testing.setPixelBlue(Integer.toString(testee.getPixelBlue(pixelColor)));
}
catch(NullPointerException ex){
}
}
}
}
我尝试了很多不同的解决方案但没有任何描述jpanel。我的桌面上的图像也是jpg,路径正确,可以从图像加载数据,只是不能让它出现在gui上。接下来要回答的问题......谢谢
编辑:对不起,如果重复,似乎无法找到正确的答案答案 0 :(得分:3)
所以,当我最终把所有代码放在一起时...我做了类似的事情......
Testee t = new Testee();
t.setImageBuffer("path/to/my/image");
new Testing(t);
它运作得很好......
Testee.bImage
没有任何理由static
,事实上,我可以看到很多理由不应该...... {/ li>
ImageIO.read
可能引发的异常,这可能实际上告诉你出了什么问题...... Testee
无法告诉感兴趣的各方其内容已发生变化,例如图片实际已被清除...... JFrame#setDefaultCloseOpertation
并将其设置为EXIT_ON_CLOSE
可以节省您必须使用WindowAdaptor
退出系统,建议不要使用SwingUtilities.invokeAndWait
,那只是我... pack
而不是setSize
,它会根据内容的需要计算窗口大小...... DrawBoard
应覆盖getPreferredSize
方法并返回更可靠的值在MVC框架中,控制器需要在模型和视图之间传递信息,协调功能。对于控制器应该不知道视图逻辑(存在什么控件或它们做什么),它应该只有一个契约,它可以通过它与状态通信并获得响应(模型也是如此)
所以,基本上,你有三个班级,有些可以影响另一个或两个班级,但是没有人知道发生了什么......