我正在尝试在java swing中添加滚动条到图像,还有从图像中读取RGB的功能。有两个问题:
代码: 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");
}
}
感谢您的帮助,对不起我的英语;)
答案 0 :(得分:0)
滚动条不起作用 - 我想将图像放到容器中...某种“框架”''''''''''''具有恒定的大小和缩放图像。缩放后 - 图片更大 - 还有滚动条......但它不起作用。
您需要通知容器组件状态发生了变化,这可能会影响组件的大小......
MySQLdb
public void zoomIn(int scale) {
zoom = zoom * scale;
revalidate();
repaint();
}
public void zoomOut(int scale) {
zoom = zoom / scale;
revalidate();
repaint();
}
调用将导致容器层次结构重新验证布局信息并更新布局
有从鼠标位置读取RGB颜色像素的功能,但是当我添加滚动条程序时认为图像在(0,0)位置,并且颜色是读取,而不是从帧中的真实图像。
将revalidate
添加到MouseListener
窗格。 MyImage
是从
您将需要一种方法将鼠标坐标缩放回"未缩放的"图像的版本,以便您可以获得由未缩放图像表示的像素