I am trying to make a GUI application using Swing that views images and reads the RGB values of the image using the mouseMoved event. The GUI allows the image to be rotated using a JSlider. This is where the problem lyes. The image is being rendered in a different location to where it actually is. For example the image will render somewhere in the middle of the panel but will give no RGB reading or if the image is rotated it doesn't actually rotate the buffered image itself just the render. I am rather new to dealing with images in swing and would like some advice on how to approach this so that the actual buffered image is rotating too and is in the same location as the render. I have posted a screen shot of the application below. The red circle is where the mouse is located in that instance.
Screen Shot Of GUI (red circle is where mouse is hovered)
I can provide the image view class below:
// Image View
public static class ImageView extends JPanel implements ChangeListener{
protected BufferedImage image;
protected JSlider imageSlider;
public BufferedImage getBufImage(){
return this.image;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g.create();
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
int x = (getWidth() - image.getWidth()) / 2;
int y = (getHeight() - image.getHeight()) / 2;
AffineTransform imageAT = new AffineTransform();
imageAT.setToRotation(this.getAngle(), x + (imageWidth / 2.0), y + (imageHeight / 2.0));
imageAT.translate(x, y);
g2.setTransform(imageAT);
g2.drawImage(image, 261, 152, this);
g2.dispose();
}
public void getImage(File imageFile) {
try {
image = ImageIO.read(imageFile);
}
catch (IOException e) {
e.printStackTrace();
}
}
public ImageView(){
this.setLayout(new BorderLayout());
// Init Slider
imageSlider = new JSlider();
imageSlider.setMinimum(0);
imageSlider.setMaximum(360);
imageSlider.setMinorTickSpacing(5);
imageSlider.setMajorTickSpacing(20);
imageSlider.setPaintTicks(true);
//imageSlider.setPaintLabels(true);
imageSlider.setValue(0);
this.add(imageSlider, BorderLayout.SOUTH);
imageSlider.addChangeListener(this);
}
@Override
public void stateChanged(ChangeEvent event){
JComponent source = (JComponent) event.getSource();
if(source == imageSlider){
this.revalidate();
this.repaint(); // Repaint JPanel
}
}
public double getAngle() {
// Angle needs to be radians
return Math.toRadians(imageSlider.getValue());
}
}
Thanks for your time.