简体   繁体   中英

BufferedImage not displaying in gui

I've read read and read some more only to lead to more confusion. Here's my problem: I have three classes following the model view controller design. I am having trouble loading the image from the model and displaying onto the view. I have read to use icon, copy from bufferedimage to bufferedimage, display it on a jpanel but none have worked. heres the basic outline:

Model:

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;
    }

}

View:

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);
    }

}

Controller:

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){

            }
        }
    }

}

I have tried many different solutions but nothing paints the jpanel. also the image is a jpg on my desktop, path is correct, can load the data from the image, just cant make it appear on the gui. Further questions to follow... Thanks

EDIT: Sorry if duplicate, cant seem to find right answer

So, when I finally got all your code put together...I did something like...

Testee t = new Testee();
t.setImageBuffer("path/to/my/image");

new Testing(t);

And it worked just fine...

图片

  • I don't see any reason for Testee.bImage to be static and in fact, I can see a lot of reasons why it shouldn't be...
  • You're throwing away the exception that may be raised by ImageIO.read which may actually tell you what's going wrong...
  • There is no way for Testee to tell interested parties that it's contents has changed, like the graphics have actually been cleared...
  • Using JFrame#setDefaultCloseOpertation and setting it to EXIT_ON_CLOSE would save you having to use a WindowAdaptor to exit the system, it would also be advisable not to use SwingUtilities.invokeAndWait , but that's just me...
  • You should consider using pack over setSize , it calculates the window size based on the needs of it's content...
  • DrawBoard should override the getPreferredSize method and return a more reliable value

In a MVC framework, the controller needs to be passing information between the model and the view, coordinating functionality. For a controller should have no idea of the view logic (what controls exist or what they do), it should only have a contract by which it can communicate states to it and get response back (the same goes for the model)

So, basically, you have three class, some can effecting the other one or two, but nobody knows what's going on...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM