简体   繁体   中英

ScrollBar Java Swing Image

I'm trying to add scrollbar to image in java swing, there is also function to read RGB from image. There are 2 problems:

  1. Scrollbar doesn't work - i want to put image to container...some kind of ''frame'' with constant size and zoom image. After zoom - picture is larger - there are also scrollbars...but it's not working .
  2. There is function to read RGB color pixel in from mouse position , but when i add scrollbar program think that image is in (0,0) position , and there colors are reading , not from real image in frame.

Code: Window.class:

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;


public class Window extends JFrame implements ActionListener{
    JButton Bzoom ;
    ButtonGroup BGzoom,  BGMzoom;
    JRadioButton RB1,RB2,RB4,RB8,RB16;
    JRadioButton RBM1, RBM2, RBM4, RBM8, RBM16;
    MyImage myImage;
    public Window()
    {
        super("Image_Processing");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800 , 600);
        setLayout(new FlowLayout());
        setVisible(true);
        setLocation(50,50);
        setResizable(true);


        // RadioButtons for X 1,2,4,8,16
        BGzoom = new ButtonGroup();
        RB1 = new JRadioButton("1x",true);
        RB2 = new JRadioButton("2X",false);
        RB4 = new JRadioButton("4X",false);
        RB8 = new JRadioButton("8X",false);
        RB16 = new JRadioButton("16X",false);


        BGzoom.add(RB1);
        BGzoom.add(RB2);
        BGzoom.add(RB4);
        BGzoom.add(RB8);
        BGzoom.add(RB16);

        add(RB1);
        add(RB2);
        add(RB4);
        add(RB8);
        add(RB16);

        RB1.addActionListener(this);
        RB2.addActionListener(this);
        RB4.addActionListener(this);
        RB8.addActionListener(this);
        RB16.addActionListener(this);

        // RadioButtons for MINUS X 1,2,4,8,16
        BGMzoom = new ButtonGroup();
        RBM1 = new JRadioButton("-1x",true);
        RBM2 = new JRadioButton("-2X",false);
        RBM4 = new JRadioButton("-4X",false);
        RBM8 = new JRadioButton("-8X",false);
        RBM16 = new JRadioButton("-16X",false);

        BGzoom.add(RBM1);
        BGzoom.add(RBM2);
        BGzoom.add(RBM4);
        BGzoom.add(RBM8);
        BGzoom.add(RBM16);

        add(RBM1);
        add(RBM2);
        add(RBM4);
        add(RBM8);
        add(RBM16);

        RBM1.addActionListener(this);
        RBM2.addActionListener(this);
        RBM4.addActionListener(this);
        RBM8.addActionListener(this);
        RBM16.addActionListener(this);

        Bzoom = new JButton("WYKONAJ");
        add(Bzoom);
        Bzoom.addActionListener(this);

        // RGB for mouse click

        getContentPane().addMouseMotionListener(new MouseMotionListener()
        {

            public void mouseMoved(MouseEvent e)
            {
                int x = (int) ((getContentPane().getMousePosition().x - (myImage.getBounds().x + 1))/myImage.getZoom());
                int y = (int) ((getContentPane().getMousePosition().y - (myImage.getBounds().y + 1))/myImage.getZoom());
                int color = myImage.getColorAt(x, y);
                Color thisColor = new Color(color);
                System.out.println("R: " + thisColor.getRed() + " G: " + thisColor.getGreen() + " B: " + thisColor.getBlue());

            }

            public void mouseDragged(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });

    }

    public void LoadImage(String filename)
    {
        myImage = new MyImage(filename);
        JScrollPane scroll = new JScrollPane(myImage,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        add(scroll);
        pack();
    }
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == Bzoom)
        {
            if(RB1.isSelected())
            {
                myImage.zoomIn(1);
                repaint();
            }
            else if(RB2.isSelected())
            {
                myImage.zoomIn(2);
                repaint();
            }
            else if(RB4.isSelected())
            {
                myImage.zoomIn(4);
                repaint();
            }
            else if(RB8.isSelected())
            {
                myImage.zoomIn(8);
                repaint();

            }
            else if(RB16.isSelected())
            {
                myImage.zoomIn(16);
                repaint();

            }
            else if(RBM1.isSelected())
            {
                myImage.zoomOut(1);
                repaint();
            }
            else if(RBM2.isSelected())
            {
                myImage.zoomOut(2);
                repaint();
            }
            else if(RBM4.isSelected())
            {
                myImage.zoomOut(4);
                repaint();
            }
            else if(RBM8.isSelected())
            {
                myImage.zoomOut(8);
                repaint();
            }
            else if(RBM16.isSelected())
            {
                myImage.zoomOut(16);
                repaint();
            }
        }


    }

}

MyImage.class:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;



public class MyImage extends JPanel{
    protected BufferedImage image;
    private float zoom = (float) 1.0;


    public MyImage() {}
    public MyImage(String filename)
    {
        super();

        File imageFile = new File(filename);
        try
        {
            image = ImageIO.read(imageFile);

        }
        catch (IOException e)
        {
            System.err.println("Blad odczytu obrazka");
            e.printStackTrace();
        }

    Dimension dimension = new Dimension(image.getWidth(), image.getHeight()); //wymiar
    setPreferredSize(dimension);

    }
    public Dimension getPreferredSize()
    {
        int w = (int)(zoom * (image.getWidth()+20));
        int h = (int)(zoom * (image.getHeight()+20));
        return new Dimension(w, h);
    }
    @Override
    public void paintComponent(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.scale(zoom, zoom);
        g2d.drawImage(image, 0 , 0, this);
    }
    public float getZoom()
    {
        return zoom;
    }
    public int getColorAt(int x, int y)
    {
        return image.getRGB(x, y); 
    }
    public void zoomIn(int scale)
    {
        zoom = zoom*scale;
    }

    public void zoomOut(int scale)
    {
        zoom = zoom/scale;
    }
}

Main.class:

import java.awt.BorderLayout;
import java.awt.Container;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;

//import org.opencv.core.Core;
//import org.opencv.core.CvType;
//import org.opencv.core.Mat;


public class Main {


     public static void main( String[] args ) throws IOException
       {
         Window m = new Window();
         m.LoadImage("file.jpg");


       }
}

Thanks for all help , and sorry for my English ;)

Scrollbar doesn't work - i want to put image to container...some kind of ''frame'' with constant size and zoom image. After zoom - picture is larger - there are also scrollbars...but it's not working .

You need to inform the container that there has been change to the state of the component which might affect it's size...

public void zoomIn(int scale) {
    zoom = zoom * scale;
    revalidate();
    repaint();
}

public void zoomOut(int scale) {
    zoom = zoom / scale;
    revalidate();
    repaint();
}

The revalidate call will cause the container hierarchy to revalidate the layout information and update the layout

There is function to read RGB color pixel in from mouse position , but when i add scrollbar program think that image is in (0,0) position , and there colors are reading , not from real image in frame.

Add the MouseListener to the MyImage pane. MouseEvent s are contextual to the component to which they are generated from

You will need a way to scale the mouse coordinates back to the "unscaled" version of the image so you can get the pixel represented by the unscaled image

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