0

I am making program which will show zoomed in picture in scrolled frame and allow user to change single pixels of buffered image. I am trying to add panel with buffered image to scrolledPane but scroll bars wont appear. Here is my panel:

package biometria1;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class noobPanel extends JPanel implements MouseListener{
     private BufferedImage background;
     int scale = 8;
     noobPanel  self;

   public noobPanel(File file){
       self = this;
         try {
             background = ImageIO.read(file);
         } catch (IOException ex) {
             Logger.getLogger(noobPanel.class.getName()).log(Level.SEVERE, null, ex);
         }

         addMouseListener(this);
   }

   public BufferedImage getImage(){
       return this.background;
   }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, background.getWidth()*8, background.getHeight()*8, 0, 0, background.getWidth(), background.getHeight(), this);  
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX()/scale;
        int y = e.getY()/scale;
        System.out.println("before:"+background.getRGB(x, y));
        background.setRGB(x, y, 100);
        System.out.println("after:"+background.getRGB(x, y));

        self.repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseExited(MouseEvent e) {
      //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

and thats how i add this panel in frame:

                File image = chooser.getSelectedFile();
                obraz = new noobPanel(image);

                scrollPanel= new JScrollPane(obraz);
                // 8 because i scale its 8 times bigger
                obraz.setPreferredSize(new Dimension(obraz.getWidth()*8, obraz.getHeight()*8));

                self.setSize(860, 640);
                self.add(scrollPanel);
                self.pack();
                self.revalidate();
                self.repaint();

Could u tell me what I am doing wrong? Thanks for any advice

Max
  • 59
  • 1
  • 12

1 Answers1

1

You never set the size of your panel (preferred or otherwise). So the new Dimension(width*8, height*8) ist still 0,0.

So you can either do what Andrew Thompson suggested and override getPreferredSize() or just set your preferred size in your panels constructor, probably after loading the image since that's what actually matters?

Oh and why do you define the scale and then not use it? I'm talking about the *8 instead of *scale.

Also: This is a little off topic but why do you keep an extra reference "self" around? Seems like unnecessary overhead... You can call all those methods without the "self." in front of them and even if you couldn't there's already the "this" keyword for that. I mean yeah it's only around 4 bytes or something but it looks odd.

Mark
  • 1,498
  • 1
  • 9
  • 13
  • 1
    [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). The problem with calling `setPreferredSize` is, some else can also – MadProgrammer Mar 03 '16 at 20:53