getContentPane (). getUIPanel () does not display the contents

0

Problems in this line: frmLupe.getContentPane().add(getUIPanel(), "South"); open the form but doesn't display the contents.

when I run my program it gives me read error:

  

read: Can not read input file!

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    import javax.swing.event.*; 

    public class Magnifying  {

        JFrame frmLupe = new JFrame();
        MagnifierPanel magnifierPanel; 
        MagnifierControl control; 
        private final LineBorder border = new LineBorder(Color.RED,1);
        private Point initialClick;
        JMenuBar JMenuMagnifying = new JMenuBar();
        JMenu JMenuMagnifying1 = new JMenu();
        JMenu JMenuMagnifying2 = new JMenu(); 
        JMenu JMenuMagnifying3 = new JMenu();
        JLabel JlImagen = new JLabel();
        JPanel PImagen = new JPanel();

        public void Magnifying(BufferedImage pic){

           (pic.getScaledInstance(pic.getWidth(), pic.getHeight(), Image.SCALE_DEFAULT)); 
            JlImagen.setIcon(icon);
            JlImagen.repaint();
            PImagen.add(JlImagen);
            frmLupe.add(PImagen);


            frmLupe.setUndecorated(true);
            frmLupe.getRootPane().setBorder(border);
            frmLupe.setFocusable(true);
            //frmLupe.getContentPane().add(getUIPanel(), "South"); <--ERROR AQUI
            frmLupe.getContentPane().add(JMenuMagnifying, BorderLayout.NORTH );
            frmLupe.getContentPane().add(new JScrollPane(magnifierPanel));
            frmLupe.setSize(620,650);
            frmLupe.setLocation(370,100);
            frmLupe.setVisible(true);


            JMenuMagnifying.setPreferredSize(new Dimension(35,35));   
            JMenuMagnifying.add(JMenuMagnifying1);
            JMenuMagnifying.add(JMenuMagnifying2);
            JMenuMagnifying.add(JMenuMagnifying3);  

javax.swing.ImageIcon(getClass().getResource("/iconos/verde_c.png")));
            JMenuMagnifying1.setToolTipText("Minimize window");
            JMenuMagnifying1.addItemListener(new ItemListener(){
                public void itemStateChanged(ItemEvent e){ 
                    frmLupe.setExtendedState(JFrame.NORMAL);
                }
            });


            JMenuMagnifying2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/amarillo_c.png"))); 
            JMenuMagnifying2.setToolTipText("Maximize window");
            JMenuMagnifying2.addItemListener(new ItemListener(){
                public void itemStateChanged(ItemEvent e){ 
                    frmLupe.setExtendedState(frmLupe.getExtendedState()| FramePrincipal.MAXIMIZED_BOTH);
                }
            });


            JMenuMagnifying3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/rojo_c.png")));
            JMenuMagnifying3.setToolTipText("Close window"); 
            JMenuMagnifying3.addItemListener(new ItemListener(){
                public void itemStateChanged(ItemEvent e){ 
                    frmLupe.setVisible(false); 
                }
            });


            frmLupe.addMouseListener(new MouseAdapter() { 
                public void mousePressed(MouseEvent e) {
                     initialClick = e.getPoint();
                     frmLupe.getComponentAt(initialClick); 
                }
            });

            frmLupe.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {

                //se localiza la ventana
                int thisX = frmLupe.getLocation().x;
                int thisY = frmLupe.getLocation().y;

                //se determina el movimiento del mouse
                int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
                int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);

                //se mueve la ventana
                int X = thisX + xMoved;
                int Y = thisY + yMoved;
                frmLupe.setLocation(X, Y); 
            }
          });
             magnifierPanel = new MagnifierPanel(pic); 
            control = new MagnifierControl(magnifierPanel);
            magnifierPanel.addMouseListener(control);
            magnifierPanel.addMouseMotionListener(control);
        }

        private JPanel getUIPanel(){

            final JLabel label = new JLabel("scale = 2.0");
            int value = (int)(magnifierPanel.scale * 2);
            final JSlider slider = new JSlider(JSlider.HORIZONTAL, 1, 8, value);
            slider.addChangeListener(new ChangeListener(){
                @Override
                public void stateChanged(ChangeEvent e){
                    float value = slider.getValue()/2f;
                    label.setText("scale = " + value);
                    magnifierPanel.setScale(value);
                }
            });

            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2,2,2,2);
            gbc.weightx = 1.0;
            panel.add(label, gbc);
            panel.add(slider, gbc);
            return panel;
        }
    }

    final class MagnifierPanel extends JPanel{ 
        BufferedImage image;
        Rectangle viewer, imageBounds; 
        float scale;
        Dimension targetSize;

        public MagnifierPanel(BufferedImage pic){ 
            loadImage(pic);
            viewer = new Rectangle(0, 0, 160, 160);
            imageBounds = new Rectangle(0, 0, image.getWidth(), image.getHeight());
            setScale(2.0f);
            setLayout(null);
            addComponentListener(new ComponentAdapter(){
                public void componentResized(ComponentEvent e){
                    positionViewerAndImage();
                }
            });
        }


        @Override
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            int w = getWidth();
            int h = getHeight();
            //se lee el ancho de la imagen
            int imageWidth = image.getWidth(); 
            //se lee el alto de la imagen
            int imageHeight = image.getHeight();
            //tamaño de x (recuadro de la lupa)
            int x = (w - imageWidth)/2; 
            //tamaño de y (recuadro de la lupa)
            int y = (h - imageHeight)/2;
            //se pinta la lupa sobre la imagen con los tamaños de x/y
            g2.drawImage(image, x, y, this); 
            BufferedImage viewImage = getMagnifiedImage();
            if(viewImage != null)
                g2.drawImage(viewImage, viewer.x, viewer.y, this);
                //se le da el color rojo al borde de la lupa
                g2.setPaint(Color.red); 
                g2.setStroke(new BasicStroke(2f));
                g2.draw(viewer);
            }

        private BufferedImage getMagnifiedImage(){
            Point p = getSubimageLocation();
            int w = targetSize.width;
            int h = targetSize.height;
            BufferedImage target = null;
            try{
                target = image.getSubimage(p.x, p.y, w, h);
            }catch(RasterFormatException rfe){
                System.out.println("viewer out of bounds: " + rfe.getMessage() + "\n" +
                                   "\tx = " + p.x + "\t(x + w) = " + (p.x + w) + "\n" +
                                   "\ty = " + p.y + "\t(y + h) = " + (p.y + h));
                return target;
            }

            w = viewer.width;
            h = viewer.height;
            BufferedImage view = new BufferedImage(w, h, image.getType());
            Graphics2D g2 = view.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
            g2.drawRenderedImage(target, at);
            g2.dispose();
            return view;
        }

        private Point getSubimageLocation(){
            int w = targetSize.width;
            int h = targetSize.height;
            int x = viewer.x - imageBounds.x + (viewer.width - w)/2;
            int y = viewer.y - imageBounds.y + (viewer.height - h)/2;
            if(x + w > image.getWidth())
                x = image.getWidth() - w;
            if(y + h > image.getHeight())
                y = image.getHeight() - h;
            return new Point(x, y);
        }

        public Dimension getPreferredSize(){
            int w = image.getWidth() +
                        (scale > 1.0f ? viewer.width  - targetSize.width  : 0);
            int h = image.getHeight() +
                        (scale > 1.0f ? viewer.height - targetSize.height : 0);
            return new Dimension(w, h);
        }

        public void setScale(float f){
            scale = f;
            int width  = (int)(viewer.width / scale);
            int height = (int)(viewer.height / scale);
            targetSize = new Dimension(width, height);
            positionViewerAndImage();
            revalidate();
            repaint();
        }

        private void positionViewerAndImage(){
            int w = getWidth();
            int h = getHeight();
            int x = (w - viewer.width)/2;
            int y = (h - viewer.height)/2;
            viewer.setLocation(x, y);
            x = (w - image.getWidth())/2;
            y = (h - image.getHeight())/2;
            imageBounds.setLocation(x, y);
            repaint();
        }

        public void setViewer(int x, int y){
            Point p = getLegalLocation(x, y);
            viewer.setLocation(p.x, p.y);
            repaint();
        }

        private Point getLegalLocation(int x, int y){
            int deltaW = (viewer.width - targetSize.width)/2;
            int deltaH = (viewer.height - targetSize.height)/2;
            if(x + deltaW < imageBounds.x)
                x = imageBounds.x - deltaW;
            if(x + viewer.width - deltaW > imageBounds.x + imageBounds.width)
                x = imageBounds.x + imageBounds.width - viewer.width + deltaW;
            if(y + deltaH < imageBounds.y)
                y = imageBounds.y - deltaH;
            if(y + viewer.height - deltaH > imageBounds.y + imageBounds.height)
                y = imageBounds.y + imageBounds.height - viewer.height + deltaH;
            return new Point(x, y);
        }


        public void loadImage(BufferedImage pic){ 

            String fileName ="Historial_Salidas/Bordes/" + pic;
            try{
              File archivo_imagen = new File (fileName);
              image = ImageIO.read(archivo_imagen);    
            }catch(MalformedURLException mue){  
              System.err.println("url: " + mue.getMessage());
            }catch(IOException ioe){
              System.err.println("read: " + ioe.getMessage());               
            }
         }
       }

    class MagnifierControl extends MouseInputAdapter{
        MagnifierPanel magnifierPanel;
        Point offset;
        boolean dragging;

        public MagnifierControl(MagnifierPanel mp){
            magnifierPanel = mp;
            offset = new Point();
            dragging = false;
        }

        public void mousePressed(MouseEvent e){
            Point p = e.getPoint();
            if(magnifierPanel.viewer.contains(p)){
                offset.x = p.x - magnifierPanel.viewer.x;
                offset.y = p.y - magnifierPanel.viewer.y;
                dragging = true;
            }
        }

        public void mouseReleased(MouseEvent e){
            dragging = false;
        }

        public void mouseDragged(MouseEvent e){
            if(dragging){
                int x = e.getX() - offset.x;
                int y = e.getY() - offset.y;
                magnifierPanel.setViewer(x, y);
            }
        }

    }
    
asked by César Landaeta 31.07.2017 в 21:43
source

1 answer

0

As I said in the comment, everything seems to indicate that it is an instantiation problem, since you call it magnifierPanel before its instantiation.

What you can do is instantiate it before getUIPanel ().

staying like this:

magnifierPanel = new MagnifierPanel(pic); <-- hasta aca se instancia
frmLupe.getContentPane().add(getUIPanel(), "South");

Try and tell me

public void Magnifying(BufferedImage pic){

       (pic.getScaledInstance(pic.getWidth(), pic.getHeight(), Image.SCALE_DEFAULT)); 
        JlImagen.setIcon(icon);
        JlImagen.repaint();
        PImagen.add(JlImagen);
        frmLupe.add(PImagen);


        frmLupe.setUndecorated(true);
        frmLupe.getRootPane().setBorder(border);
        frmLupe.setFocusable(true);
        //frmLupe.getContentPane().add(getUIPanel(), "South"); <-- Lo llamas aquí.
        frmLupe.getContentPane().add(JMenuMagnifying, BorderLayout.NORTH );
        frmLupe.getContentPane().add(new JScrollPane(magnifierPanel));
        frmLupe.setSize(620,650);
        frmLupe.setLocation(370,100);
        frmLupe.setVisible(true);


        JMenuMagnifying.setPreferredSize(new Dimension(35,35));   
        JMenuMagnifying.add(JMenuMagnifying1);
        JMenuMagnifying.add(JMenuMagnifying2);
        JMenuMagnifying.add(JMenuMagnifying3);  

javax.swing.ImageIcon(getClass().getResource("/iconos/verde_c.png")));
        JMenuMagnifying1.setToolTipText("Minimize window");
        JMenuMagnifying1.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e){ 
                frmLupe.setExtendedState(JFrame.NORMAL);
            }
        });


        JMenuMagnifying2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/amarillo_c.png"))); 
        JMenuMagnifying2.setToolTipText("Maximize window");
        JMenuMagnifying2.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e){ 
                frmLupe.setExtendedState(frmLupe.getExtendedState()| FramePrincipal.MAXIMIZED_BOTH);
            }
        });


        JMenuMagnifying3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/rojo_c.png")));
        JMenuMagnifying3.setToolTipText("Close window"); 
        JMenuMagnifying3.addItemListener(new ItemListener(){
            public void itemStateChanged(ItemEvent e){ 
                frmLupe.setVisible(false); 
            }
        });


        frmLupe.addMouseListener(new MouseAdapter() { 
            public void mousePressed(MouseEvent e) {
                 initialClick = e.getPoint();
                 frmLupe.getComponentAt(initialClick); 
            }
        });

        frmLupe.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {

            //se localiza la ventana
            int thisX = frmLupe.getLocation().x;
            int thisY = frmLupe.getLocation().y;

            //se determina el movimiento del mouse
            int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
            int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);

            //se mueve la ventana
            int X = thisX + xMoved;
            int Y = thisY + yMoved;
            frmLupe.setLocation(X, Y); 
        }
      });
         magnifierPanel = new MagnifierPanel(pic); <-- hasta aca se instancia
        control = new MagnifierControl(magnifierPanel);
        magnifierPanel.addMouseListener(control);
        magnifierPanel.addMouseMotionListener(control);
    }
    
answered by 31.07.2017 в 23:07