Display image selected from JFileChooser

J

jimmy

Hi All,

I am having big problems setting out my code and need help organising
it. I want to write a GUI that displays jpg images that are selected
by the user from a file chooser. I have written two classes: MyImage
does the painting to the screen and MyImageApp for drawing the button,
and then calls MyImage when it has returned an image selected by the
user. The problem is it doesn't work.

I am almost there, but I think I am causing problems with MyImageApp.
I need to be able to access the BufferedImage returned by the
JFileChooser and pass it as an argument to MyImage for painting. I
just can't figure it out though. Can someone show me the errors of my
ways?

Cheers

Jimmy

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;

@SuppressWarnings("serial")
class MyImage extends JPanel{

private BufferedImage bi;

public MyImage(BufferedImage imgSrc){

int h = imgSrc.getHeight();
int w = imgSrc.getWidth();
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
}


public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bi, 0, 0, null);
}
}

import javax.swing.*;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

public class MyImageApp extends Component {

public MyImageApp(){
}
BufferedImage image = null;

public void buildUI(){

JButton button1 = new JButton("Open file");
button1.setSize(150, 100);
button1.addActionListener((ActionListener) new myevent());
final MyImage panny = new MyImage(image);
}

public static void main(String[] args){

JFrame myFrame = new JFrame();

myFrame.setTitle("Here's my Frame");
myFrame.setSize(1000, 800);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyImageApp core = new MyImageApp();
core.buildUI();
}
}
 
J

jimmy

Oops! I forgot the third class I wrote to handle the getting of the
file.


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;

public class myevent implements ActionListener{

BufferedImage image;

public void actionPerformed (ActionEvent e){

JFileChooser fileChooser = new JFileChooser();

int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println("Image selected: " + file.getPath());
System.out.println("Image about to be loaded to buffer");
try {
System.out.println("Image loaded to buffer");

image = ImageIO.read(fileChooser.getSelectedFile());
System.out.println("Image stored as a buffered image");
}
catch (IOException ex) {
System.out.println("problem accessing file
"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}
 
L

Lew

I am having big problems setting out my code and need help organising
it. I want to write a GUI that displays jpg images that are selected
by the user from a file chooser. I have written two classes: MyImage

Actually, three ...
does the painting to the screen and MyImageApp for drawing the button,
and then calls MyImage when it has returned an image selected by the
user. The problem is it doesn't work.

What does "doesn't work" mean? It hangs? It displays some different image?
It does nothing apparent? It gives errors?

Your request is like asking a doctor to prescribe medicine when all you'll
admit is, "I don't feel well". We need the specific symptoms, accurately
reported.

On the face of it, 'MyImage' refuses to use the image data passed to it.
I am almost there, but I think I am causing problems with MyImageApp.
I need to be able to access the BufferedImage returned by the
JFileChooser and pass it as an argument to MyImage for painting. I
just can't figure it out though. Can someone show me the errors of my
ways?

Stop using TAB to indent code for Usenet. Use up to four space characters per
indent level.
import ... [omitted for brevity]
@SuppressWarnings("serial")
class MyImage extends JPanel{

private BufferedImage bi;

public MyImage(BufferedImage imgSrc){

int h = imgSrc.getHeight();

'NullPointerException' ("NPE") thrown here.
int w = imgSrc.getWidth();

NPE here, but that the code doesn't get this far.
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
}


public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bi, 0, 0, null);

'bi' will never have data.
public class MyImageApp extends Component {
BufferedImage image = null;

Here you set 'image' to 'null', twice.
public void buildUI(){

JButton button1 = new JButton("Open file");
button1.setSize(150, 100);
button1.addActionListener((ActionListener) new myevent());

By convention, class names should begin with an upper-case character and be in
camel case.
final MyImage panny = new MyImage(image);

Here you build a 'MyImage' around a 'null' image, which in turn is ignored
except to throw 'NullPointerException'.
}

public static void main(String[] args){

GUI code should ONLY run on the Event Dispatch Thread (EDT).
JFrame myFrame = new JFrame();

myFrame.setTitle("Here's my Frame");
myFrame.setSize(1000, 800);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

MyImageApp core = new MyImageApp();
core.buildUI();
}
}
public class myevent implements ActionListener{

By convention, class names should begin with an upper-case character and be in
camel case.
BufferedImage image;

This 'image' is disconnected from all proposed use of it. You never give it
to a component for display. It's also peculiarly scoped as an instance
variable of the event-handler class.
public void actionPerformed (ActionEvent e){

JFileChooser fileChooser = new JFileChooser();

int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println("Image selected: " + file.getPath());
System.out.println("Image about to be loaded to buffer");

Don't use 'System.out.println()' for debugging. Don't even use
'System.err.println()'. This is especially bad for GUI code.

Use a logger.
try {
System.out.println("Image loaded to buffer");

image = ImageIO.read(fileChooser.getSelectedFile());

Now what? You have the image, but you do nothing with it.
 
J

John B. Matthews

jimmy said:
I am having big problems setting out my code and need help organising
it. I want to write a GUI that displays jpg images that are selected
by the user from a file chooser. I have written two classes: MyImage
does the painting to the screen and MyImageApp for drawing the
button, and then calls MyImage when it has returned an image selected
by the user. The problem is it doesn't work.

I am almost there, but I think I am causing problems with MyImageApp.
I need to be able to access the BufferedImage returned by the
JFileChooser and pass it as an argument to MyImage for painting. I
just can't figure it out though. Can someone show me the errors of my
ways?

In addition to Lew's compelling observations, each of which deserves
attention, I would like to address a few design problems.

First, your class MyImageApp extends Component, but it doesn't really
alter the behavior of Component; it just contains a JFrame holding two
JComponents (JPanel and JButton).

Second, your button handler needs to work in concert with your image
panel, but the button itself may be elsewhere. The Action interface is
intended "to separate functionality and state from a component." This
makes it east to add the "Open" functionality to a button or menu item.

<http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html>

Compare this example with your own in light of Lew's comments. As a
convenience, ImageOpenAction uses pack() to adjust the parent Window to
the image's size:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** @author John B. Matthews */
public class ImageApp {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setTitle("Title");
ImagePanel imagePanel = new ImagePanel(f);
f.add(imagePanel, BorderLayout.CENTER);
JButton open = new JButton(imagePanel.getAction());
f.add(open, BorderLayout.SOUTH);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
});
}
}

class ImagePanel extends JPanel {

private final Window parent;
private BufferedImage image;
private Action action = new ImageOpenAction();

public ImagePanel(JFrame parent) {
this.parent = parent;
this.setPreferredSize(new Dimension(320, 240));
}

@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}

public Action getAction() {
return action;
}

private class ImageOpenAction extends AbstractAction {

public ImageOpenAction() {
super("Open");
}

@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser(
new File(System.getProperty("user.dir")));
int returnVal = chooser.showOpenDialog(chooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
image = ImageIO.read(chooser.getSelectedFile());
ImagePanel.this.setPreferredSize(new Dimension(
image.getWidth(), image.getHeight()));
parent.pack();
} catch (IOException ex) {
Logger.getLogger(ImagePanel.class.getName())
.log(Level.SEVERE, null, ex);
}
}
}
}
}
 
J

jimmy

Reply to Lew:

Many thanks for your comments on my code. Yes my statement "it doesn't
work" was vague and not informative, apologies. As you can tell I am a
Java novice and could not diagnose the faults myself. From your
comments it is clear that my code contained many problems. I will read
more about EDT and logger (I have left my println statements in for
the time being as I am more concerned with getting the images to
display at the moment).

reply to John B. Matthews:

Many thanks for your comments also. I have read and digested your
code, most of which I can follow, but some is still too advanced for
me. I have read the Action page you posted the link to and have tried
to implement an action in my code.



Heavily influenced by John B. Matthews' code, I have rewritten much of
my code, however it still does not display an image. I cannot identify
the source of the problem, but based on the println statements (which
I will remove in place of a logger) I think that myAction (the action
associated with the button) is not being returned.

I have attached my new code. I would be most grateful if someone could
identify the problem and indicate how I could resolve it.

Many thanks,

Jimmy

import java.awt.event.ActionEvent;


class MyAction extends AbstractAction{

BufferedImage image;

public MyAction(){
super("Open");
}

public void actionPerformed(ActionEvent e) {

JFileChooser fileChooser = new JFileChooser();

int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println("Image selected: " + file.getPath());
System.out.println("Image about to be loaded to buffer");
try {
System.out.println("Image loaded to buffer");
image = ImageIO.read(fileChooser.getSelectedFile());
System.out.println("Image painted");
}
catch (IOException ex) {
System.out.println("problem accessing
file"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}

import javax.imageio.ImageIO;

class MyImage extends JPanel{

private final Window parent;
BufferedImage img;
MyAction myAction = new MyAction();

public MyImage(JFrame parent){
this.parent = parent;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}

public Action MyAction(){
return myAction;
}
}

import java.awt.event.ActionEvent;


class MyAction extends AbstractAction{

BufferedImage image;

public MyAction(){
super("Open");
}

public void actionPerformed(ActionEvent e) {

JFileChooser fileChooser = new JFileChooser();

int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println("Image selected: " + file.getPath());
System.out.println("Image about to be loaded to buffer");
try {
System.out.println("Image loaded to buffer");
image = ImageIO.read(fileChooser.getSelectedFile());
System.out.println("Image painted");
}
catch (IOException ex) {
System.out.println("problem accessing
file"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}
 
J

jimmy

Oops! I'm really not doing well pasting code - I posted two copies of
the same class previously. The missing class is:



import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionListener;

public class MyImageApp extends Component{

public static void main(String[] args){

JFrame myFrame = new JFrame();

myFrame.setTitle("Here's my Frame");
myFrame.setSize(200, 200);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();

Action myAction = new MyAction();

JButton myButton = new JButton(myAction);

myFrame.add(myButton, BorderLayout.SOUTH);

MyImage core = new MyImage(myFrame);
myFrame.add(core, BorderLayout.CENTER);
core.MyAction();
}
}
 
L

Lew

Reply to Lew:

Many thanks for your comments on my code. Yes my statement "it doesn't
work" was vague and not informative, apologies. As you can tell I am a
Java novice and could not diagnose the faults myself. From your
comments it is clear that my code contained many problems. I will read
more about EDT and logger (I have left my println statements in for
the time being as I am more concerned with getting the images to
display at the moment).

reply to John B. Matthews:

Many thanks for your comments also. I have read and digested your
code, most of which I can follow, but some is still too advanced for
me. I have read the Action page you posted the link to and have tried
to implement an action in my code.



Heavily influenced by John B. Matthews' code, I have rewritten much of
my code, however it still does not display an image. I cannot identify
the source of the problem, but based on the println statements (which
I will remove in place of a logger) I think that myAction (the action
associated with the button) is not being returned.

I have attached my new code. I would be most grateful if someone could
identify the problem and indicate how I could resolve it. ....
import javax.imageio.ImageIO;

This is an unused import. Didn't your IDE warn you? Does your IDE support an
operation to clean up imports (Ctrl-Shift-O in Eclipse, Ctrl-Shift-I in NetBeans)?
class MyImage extends JPanel{

private final Window parent;
BufferedImage img;
MyAction myAction = new MyAction();

public MyImage(JFrame parent){
this.parent = parent;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);

'img' will always be 'null', since you never assign it a value.
}

public Action MyAction(){
return myAction;
}
}

import java.awt.event.ActionEvent;


class MyAction extends AbstractAction{

BufferedImage image;

public MyAction(){
super("Open");
}

public void actionPerformed(ActionEvent e) {

JFileChooser fileChooser = new JFileChooser();

int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println("Image selected: " + file.getPath());
System.out.println("Image about to be loaded to buffer");
try {
System.out.println("Image loaded to buffer");
image = ImageIO.read(fileChooser.getSelectedFile());

You create an image then throw it away. That is, if this line were to
compile, which it won't unless you import 'ImageIO'.
System.out.println("Image painted");

What in the world makes you think the image was painted here? All you did was
assign a variable. You never passed it to anything to paint anything.
 
R

Roedy Green

I am having big problems setting out my code and need help organising
it. I want to write a GUI that displays jpg images that are selected
by the user from a file chooser

You might use one of classes I have written for that purpose.

ImageViewer is part of the common11 bundle. See
http://mindprod.com/products1.html#COMMON11
--
Roedy Green Canadian Mind Products
http://mindprod.com

There is no harm in being sometimes wrong especially if one is promptly found out.
~ John Maynard Keynes (born: 1883-06-05 died: 1946-04-21 at age: 62)
 
K

Knute Johnson

Oops! I'm really not doing well pasting code - I posted two copies of
the same class previously. The missing class is:



import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionListener;

public class MyImageApp extends Component{

public static void main(String[] args){

JFrame myFrame = new JFrame();

myFrame.setTitle("Here's my Frame");
myFrame.setSize(200, 200);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();

Action myAction = new MyAction();

JButton myButton = new JButton(myAction);

myFrame.add(myButton, BorderLayout.SOUTH);

MyImage core = new MyImage(myFrame);
myFrame.add(core, BorderLayout.CENTER);
core.MyAction();
}
}

This is too painful to watch anymore.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test extends JPanel {
private BufferedImage image;

public test() {
setPreferredSize(new Dimension(400,300));
}

public void setImage(BufferedImage bi) {
image = bi;
setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));
revalidate(); // tells the JScrollPane the size has changed
repaint(); // redraws the new image
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,null);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final test t = new test();
final JFileChooser chooser = new JFileChooser();

JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);
JMenu file = new JMenu("File");
mb.add(file);
JMenuItem mi = new JMenuItem("Open");
file.add(mi);
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (chooser.showOpenDialog(f) ==
JFileChooser.APPROVE_OPTION) {
try {
BufferedImage bi = ImageIO.read(
chooser.getSelectedFile());
if (bi != null)
t.setImage(bi);
else
JOptionPane.showMessageDialog(f,
"File is not an image!");
} catch (IOException ioe) {
JOptionPane.showMessageDialog(f,
"Error Reading File!");
}
}
}
});

f.add(new JScrollPane(t),BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}
 
J

John B. Matthews

jimmy said:
I have attached my new code. I would be most grateful if someone could
identify the problem and indicate how I could resolve it.

Again, Lew is more assiduous than I, and ilAn's advice will serve you
better in the long run; but I will make a few observations:

0) Don't invoke pack() until you've added the components.

1) Again, extend Component? I don't see a reason.

2) Again, Swing components should be constructed in the EDT; I've
implemented Runnable as another example of doing so.

3) Invoking super.paintComponent(g) is unnecessary when you paint the
entire content.

4) If you make MyAction a separate class, its constructor needs to know
who gets the selected image. Similarly, MyImage needs a method,
setImgage(), to set the new image for later painting.

5) As an example, I've added the same action to a menu bar.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyImageApp implements Runnable {

public static void main(String[] args) {
EventQueue.invokeLater(new MyImageApp());
}

@Override
public void run() {
JFrame myFrame = new JFrame();
myFrame.setTitle("Here's my Frame");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyImage core = new MyImage(myFrame);
Action myAction = new MyAction(core);
JButton myButton = new JButton(myAction);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.add(new JMenuItem(myAction));
menuBar.add(menu);
myFrame.setJMenuBar(menuBar);
myFrame.add(core, BorderLayout.CENTER);
myFrame.add(myButton, BorderLayout.SOUTH);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}

class MyAction extends AbstractAction {

MyImage panel;

public MyAction(MyImage panel) {
super("Open");
this.panel = panel;
}

@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println("Image selected: " + file.getPath());
try {
BufferedImage image =
ImageIO.read(fileChooser.getSelectedFile());
panel.setImgage(image);
System.out.println("Image loaded to buffer.");
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
System.out.println("File access cancelled by user.");
}
}
}

class MyImage extends JPanel {

private final Window parent;
BufferedImage image;

public MyImage(JFrame parent) {
this.parent = parent;
this.setPreferredSize(new Dimension(200, 200));
}

@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}

public void setImgage(BufferedImage image) {
this.image = image;
setPreferredSize(new Dimension(
image.getWidth(), image.getHeight()));
parent.pack();
parent.setLocationRelativeTo(null);
}
}
 
J

jimmy

I never dreamed my post would provoke such a response! Thanks all for
helping me in my image display attempts.

ilAn - thanks for the tip about the error stack - its something I'll
look into.

Lew - thanks for pointing out two serious problems. In my println
statement I wrote that the image had been printed, but didn't really
believe that - it was more just a marker to see where execution had
reached, but I agree again very misleading.

Roedy Green - thanks for the link to your classes, I will investigate
these. I have been reading some other helpful pages on your website
that I found useful also, thanks. http://mindprod.com/jgloss/image.html

Knute Johnson - LOL. I know it must be disparaging to watch someone
fumble around when the answer is obvious to you, but this is the best
way for me to learn. Thanks for your solution, I will read and digest
your techniques.

John B. Matthews - thanks once again for your help. I find it
particularly useful that you have bullet pointed major failings and
modified my code (which is largely based on the code you wrote!), as I
am happy with it's style and flow. I will read and digest your
solution in detail.


Whilst there was all of the activity on this post I was busily
fumbling around with code in NetBeans. My ultimate intention is to
create a GUI which displays images for viewing. There will be many
widgets on the GUI so I decided to use NetBeans, for ease of layout.
Using a button push event and a class which extends JPanel, I have
been able to achieve my desired effect of displaying an image in a
JPanel in the GUI. However, not having seen this style of code before
(calling the paintComponent() method) in any of the many examples on
the net, I was wondering if there is a particular reason why it is not
used? Below is the relevant snippet from the code ((button push event
and DrawImage class).

Thanks once again, I'm going to read in detail the solutions that have
been posted in detail now.

Cheers,

Jimmy



BufferedImage image;
String filename;
DrawImage pic = null;

private void
openFileButtonActionPerformed(java.awt.event.ActionEvent evt)
{
JFileChooser chooser = new JFileChooser(
new File(System.getProperty("user.dir")));
int returnVal = chooser.showOpenDialog(chooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
image = ImageIO.read(chooser.getSelectedFile());
System.out.println("The file selected is: " +
file.getPath());

//mainImagePanel is the target JPanel for the
image
Graphics g=mainImagePanel.getGraphics();

pic = new DrawImage();
pic.setImage(image);
System.out.println("got to here");

pic.paintComponent(g);

filename = file.getPath();
jLabel1.setText(filename);

} catch (IOException ex) {
System.out.println("There was a problem opening
the selected file: " + ex);
}
}
}

class DrawImage extends JPanel{

public DrawImage() {
}

public void setImage(BufferedImage image){
this.img = image;
}

BufferedImage img;
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
System.out.println("Now I'm down here");
if(img != null)
{
g2d.scale(0.2, 0.2);
g2d.drawImage(image, 0, 0, null);
}
}
}
 
J

John B. Matthews

[..]
John B. Matthews - thanks once again for your help. I find it
particularly useful that you have bullet pointed major failings and
modified my code (which is largely based on the code you wrote!), as I
am happy with it's style and flow. I will read and digest your
solution in detail.

Thank you for an excellent summary. Knute Johnson's use of a scroll pane
is more flexible than my window resizing approach. I found it
instructive to alter my example accordingly.

[...]
g2d.scale(0.2, 0.2);
g2d.drawImage(image, 0, 0, null);

As an aside, overloads of dawImage() that specify a width and height
will scale, too:

<http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top