I haven’t made a Java Swing app for a while (2+ years), so excuse me for any silly mistake.
I want to upload some artwork , that will be displayed as full screen image.
The problem I’m having is that the JComponent, that I have extended to represent my Image object , does not call its paint () method & does not draw itself on the existing JPanel.
Below is the relevant snippet & event that uploads an Image & instantiates by “art” class which extends JComponent Class JFrameMainArt’s actionPerformed() method (relevant section only) , when the OPEN action fies , the image object should be drawn on the fullscreenJPanel
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getActionCommand().equalsIgnoreCase("Fullscreen Mode")) {
isScreenFullscreen = true;
// full screen mode settings
fullscreenFrame.invalidate();
fullscreenFrame = null;
// create new fullscreen frame
fullscreenFrame = new JFrame();
fullscreenFrame
.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
fullscreenFrame.setUndecorated(true);
fullscreenFrame.setResizable(false);
// add label to Panel
fullscreenPanel.add(new JLabel("ALT + F4 to exit .",
SwingConstants.CENTER), BorderLayout.CENTER);
fullscreenFrame.invalidate();
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.setFullScreenWindow(fullscreenFrame);
} else if (arg0.getActionCommand().equalsIgnoreCase("Upload")) {
System.out.println("Upload Pictures");
fileChooser = new JFileChooser();
fileChooser
.setDialogTitle("Choose Your Image File (PNG or JPEG only)");
// below codes for select the file
int returnval = fileChooser.showOpenDialog(null);
File file = fileChooser.getSelectedFile();
System.out.println("Upload Pictures, selected file path: "
+ file.getAbsolutePath());
// if OPEN
if (returnval == JFileChooser.APPROVE_OPTION) {
String name = file.getName();
if (name != null) {
int i = name.indexOf('.');
// get the extension such as jpeg, or png
String extension = name.substring(i + 1);
System.out.println("extension: " + extension);
if (extension.equalsIgnoreCase("jpeg")
|| extension.equalsIgnoreCase("jpg")
|| extension.equalsIgnoreCase("png")) {
Art newArt = new Art(file, fullscreenPanel.getSize());
//test dimension
System.out.println("Dimension of image "+name+"- Dimensions "+newArt.scaleImageToJPanelDimensions(newArt.getImageFromFile()));
//add Art Jcomponent to existing JPanel
fullscreenPanel.add(newArt);
//revalidate & paint
fullscreenPanel.revalidate();
fullscreenPanel.repaint();
//pack & revalidate the Jframe
fullscreenFrame.revalidate();
} else {
System.out
.println("Error file/image type is not supported: "
+ extension);
}
}// end name not null
else {
System.out.println("Error file name: " + name);
}
} else if (returnval == JFileChooser.ERROR_OPTION) {
System.out.println("Error selecting file");
} else {
System.out.println("returnval: " + returnval);
}
}// end else if upload
else if (arg0.getActionCommand().equalsIgnoreCase("Choose Effect")) {
} else {
}
}//end method actionPerformed()
Art.java extends JComponent and is responsible for loading the image, scaling image, drawing of the image object
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.beans.Transient;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
public class Art extends JComponent {
private File file = null;
private BufferedImage image = null;
private Dimension dimensionToScale = null;
public Art(File file, Dimension dimensionToScale) {
super();
this.file = file;
this.dimensionToScale = dimensionToScale;
this.setVisible(true);
}
@Override
@Transient
public Dimension getPreferredSize() {
// TODO Auto-generated method stub
return this.dimensionToScale;
}
public Art(File file) {
super();
this.file = file;
this.setVisible(true);
}
public BufferedImage getImageFromFile() {
BufferedImage image = null;
if (this.file != null) {
try {
image = ImageIO.read(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return image;
}
public Dimension scaleImageToJPanelDimensions(BufferedImage image) {
Dimension dimension = null;
// scaling factors
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
// scale based on largest side
int largestImageSide = Math.min(imageWidth, imageHeight);
double scaledHeight = this.dimensionToScale.getHeight()
/ largestImageSide;
double scaledWidth = this.dimensionToScale.getWidth()
/ largestImageSide;
// compute new image dimensions
imageWidth = (int) (imageWidth * scaledWidth);
imageHeight = (int) (imageHeight * scaledHeight);
dimension = new Dimension(imageWidth, imageHeight);
return dimension;
}
@Override
public void paintComponent(Graphics g) {
System.out.println("paintComponent(Graphics g) ");
super.paintComponent (g);
// repaint background
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(getForeground());
image = getImageFromFile();
if (image != null) {
// compute dimensions of image to draw
Dimension imageDimension = scaleImageToJPanelDimensions(image);
/*public abstract boolean drawImage(Image img,
int x,
int y,
int width,
int height,
ImageObserver observer)*/
g.drawImage(image, 0,0, (int) imageDimension.getWidth() ,(int)imageDimension.getHeight(),this);
} else {
System.out.println("paintComponent(Graphics g) image:"+image);
}
}
}/end Art class
Any help will be appreciated.
Thanks.
Actual working method ,image is now drawn , issue is solved.
public void loadArtToScreen(Art art)
{
//add Art JComponent to existing JPanel
JPanelGroupLayout.setHorizontalGroup(JPanelGroupLayout.createParallelGroup(
Alignment.LEADING).addGroup(
JPanelGroupLayout.createSequentialGroup().addGap(0)
.addComponent(art )
.addContainerGap((int)mainJFrameSize.getWidth(), Short.MAX_VALUE)));
JPanelGroupLayout.setVerticalGroup(JPanelGroupLayout.createParallelGroup(
Alignment.LEADING).addGroup(
JPanelGroupLayout.createSequentialGroup().addGap(0)
.addComponent(art )
.addContainerGap((int)mainJFrameSize.getHeight(), Short.MAX_VALUE)));
//revalidate & paint
fullscreenPanel.revalidate();
fullscreenPanel.repaint();
//pack & revalidate the Jframe
mainJFrame.revalidate();
}//end loadArtToScreen(Art art)