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();
}
}
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();
}
}