滚动条Java Swing图像
我试图在JavaSwing中将滚动条添加到图像中,还有一个从图像中读取RGB的函数。有两个问题:滚动条Java Swing图像,java,image,swing,scrollbar,rgb,Java,Image,Swing,Scrollbar,Rgb,我试图在JavaSwing中将滚动条添加到图像中,还有一个从图像中读取RGB的函数。有两个问题: 滚动条不工作-我想把图像放到容器中…某种具有恒定大小和缩放图像的“框架”。变焦后-图片变大-也有滚动条…但它不工作 有从鼠标位置读取RGB颜色像素的功能,但当我添加滚动条程序时,认为图像处于(0,0)位置,并且有颜色在读取,而不是从帧中的真实图像读取 代码: Window.class: import java.awt.Button; import java.awt.Color; import jav
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");
}
}
谢谢你的帮助,对不起我的英语;)
滚动条不工作-我想把图像放到容器中…某种具有恒定大小和缩放图像的“框架”。变焦后-图片变大-也有滚动条…但它不工作
您需要通知容器组件的状态发生了更改,这可能会影响组件的大小
public void zoomIn(int scale) {
zoom = zoom * scale;
revalidate();
repaint();
}
public void zoomOut(int scale) {
zoom = zoom / scale;
revalidate();
repaint();
}
调用revalidate
将导致容器层次结构重新验证布局信息并更新布局
有从鼠标位置读取RGB颜色像素的功能,但当我添加滚动条程序时,认为图像处于(0,0)位置,并且有颜色在读取,而不是从帧中的真实图像读取
将MouseListener
添加到MyImage
窗格中MouseEvent
s与生成它们的组件相关
您需要一种方法将鼠标坐标缩放回图像的“未缩放”版本,以便获得未缩放图像所代表的像素请阅读,不要将
鼠标侦听器
添加到内容窗格中,将其添加到MyImage
窗格中,MouseEvent
s与生成它们的组件相关