我已经有一段时间(2 年多)没有制作 Java Swing 应用程序了,请原谅我犯了任何愚蠢的错误。
我想上传一些艺术品,将显示为全屏图像。
我遇到的问题是,我已经扩展以表示我的 Image 对象的 JComponent没有调用它的 paint () 方法并且没有在现有的 JPanel 上绘制自己。
下面是相关的片段和事件,它通过扩展 JComponent 类 JFrameMainArt 的 actionPerformed() 方法(仅相关部分)的“艺术”类上传图像和实例化,当 OPEN 动作执行时,图像对象应绘制在全屏JPanel
@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扩展了JComponent,负责加载图片、缩放图片、绘制图片对象
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
任何帮助将不胜感激。
谢谢。
实际工作方法,现在绘制图像,问题解决。
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)