This is my code at the moment:
class loadImage extends JPanel {
private int choice;
double scale;
double currScale = 1.0;
BufferedImage img;
Timer timer;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println(choice);
g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
if (choice == 1)
{
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
int w = getWidth();
int h = getHeight();
int imageWidth = img.getWidth();
int imageHeight = img.getHeight();
double x = (w - currScale * imageWidth) / 2;
double y = (h - currScale * imageHeight) / 2;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.scale(currScale, currScale);
g2.drawRenderedImage(img, at);
}
else if (choice == 2)
{
Graphics2D g2 = (Graphics2D)g;
int x = 5;
int y = getHeight() - img.getHeight();
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.setToTranslation(x, y);
g2.drawRenderedImage(img, at);
}
}
public loadImage() {
choice = 0;
try {
img = ImageIO.read(new File("pic.png"));
} catch (IOException ex) {
}
}
public void setChoice(int choice) {
this.choice = choice;
}
public void setCurrScale(double currScale) {
this.currScale = currScale;
repaint();
}
public double getCurrScale() {
return currScale;
}
}
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
setLocationRelativeTo(null);
}
public void ZoomIn(double scale) {
if(scale > ((loadImage)imageArea).getCurrScale())
{
double scaleAddOn = 0.1;
Timer timer;
timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
double currScale;
currScale = ((loadImage)imageArea).getCurrScale();
if(currScale < scale)
{
((loadImage)imageArea).setCurrScale(currScale + scaleAddOn);
}
}
});
if(((loadImage)imageArea).getCurrScale() >= scale)
{
timer.stop();
}
else
{
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
}
else JOptionPane.showMessageDialog(rootPane, "Fail Zoom In\nScale: " + scale + "\nCurrScale: " + ((loadImage)imageArea).getCurrScale());
}
public void ZoomOut(double scale) {
if(scale < ((loadImage)imageArea).getCurrScale())
{
double scaleDecrement = 0.1;
Timer timer;
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
double currScale;
currScale = ((loadImage)imageArea).getCurrScale();
if(scale < currScale)
{
((loadImage)imageArea).setCurrScale(currScale - scaleDecrement);
}
}
});
if(scale > ((loadImage)imageArea).getCurrScale())
{
timer.stop();
}
else
{
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
}
}
else JOptionPane.showMessageDialog(rootPane, "Fail Zoom Out\nScale: " + scale + "\nCurrScale: " + ((loadImage)imageArea).getCurrScale());
}
private void resetImageActionPerformed(java.awt.event.ActionEvent evt) {
((loadImage)imageArea).setChoice(0);
this.repaint();
}
private void zoomInActionPerformed(java.awt.event.ActionEvent evt) {
((loadImage)imageArea).setChoice(1);
this.repaint();
ZoomIn(3.0);
}
private void printScaleActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println(((loadImage)imageArea).getCurrScale());
}
private void zoomOutActionPerformed(java.awt.event.ActionEvent evt) {
((loadImage)imageArea).setImage(1);
this.repaint();
ZoomOut(1.0);
}
The goal is to display an image and then add the zoom in, out, and move it about.
Details about the image = its dimensions are 1280 x 1024 the GUI jpanel's dimensions are 563 x 377. When the program runs, the image fits in the JPanel exactly 563 by 377.
The zoom in does its job, but when I perform it, and slow down the timer, I notice that it doesn't seem to be starting following the JPanel's dimensions, but the original dimensions instead. When the zoom in code runs, it reverts back to its original dimensions (it's now cropped in the jpanel) and then does the zooming.
The zoom out code does the same as far as the image size changes go. It doesn't actually zoom out. What I end up seeing on the screen is the image scaling up by a bit and then back down by the same amount that it scaled up. And it's just in that constant loop. I press the printScale button and it returns the exact same numbers even regardless of what part of that loop is being showed at the moment.
The translation (if choice == 2) is the same deal as well. It isn't following the JPanel's image size but the actual image size.
Why aren't the images in the choices following the size of the image even when g.drawImage(img, 0, 0, getWidth(), getHeight(), null); has been called? Why is my zoom out method acting the way that is? Sorry for the unpleasant read. Any help is appreciated.
MY FIX: I ended up ditching the timers for their thread counterparts and there is no more issue with the zooming in and out. I'm still trying to figure out the g.drawImage thing and how to get them consistent. I'll update this again when I do figure it out.