简体   繁体   English

ScrollBar Java摇摆图像

[英]ScrollBar Java Swing Image

I'm trying to add scrollbar to image in java swing, there is also function to read RGB from image. 我试图在Java swing中向图像添加滚动条,还有从图像读取RGB的功能。 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. 有功能可以从鼠标位置读取RGB彩色像素,但是当我添加滚动条程序时,认为图像位于(0,0)位置,并且有颜色在读取,而不是从帧中的真实图像读取。

Code: Window.class: 代码: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: 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 revalidate调用将导致容器层次结构重新验证布局信息并更新布局

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. 有功能可以从鼠标位置读取RGB彩色像素,但是当我添加滚动条程序时,认为图像位于(0,0)位置,并且有颜色在读取,而不是从帧中的真实图像读取。

Add the MouseListener to the MyImage pane. MouseListener添加到MyImage窗格中。 MouseEvent s are contextual to the component to which they are generated from MouseEvent与生成它们的组件相关

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 您将需要一种将鼠标坐标缩放回图像的“未缩放”版本的方法,以便获得由未缩放图像表示的像素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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