0
我想添加滾動條到java swing中的圖像,也有函數從圖像讀取RGB。有兩個問題:ScrollBar爪哇Swing圖像
- 滾動條不起作用 - 我想把圖像放到容器中......某種「常規尺寸和縮放圖像」的「框架」。在縮放圖片變大後 - 還有滾動條...但它不起作用。
- 有從鼠標位置讀取RGB彩色像素的功能,但是當我添加滾動條程序時,認爲圖像位於(0,0)位置,並且有顏色正在讀取,而不是來自實際圖像。
代碼: 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");
}
}
感謝所有幫助和抱歉的我的英語;)
請閱讀[幫助]和[如何詢問](http://stackoverflow.com/help/asking) –
不要將「MouseListener」添加到內容窗格,將其添加到「MyImage」窗格中, 'MouseEvent'是它們生成的組件的上下文 – MadProgrammer