我已经有一段时间没有制作Java Swing应用程序(2年以上),所以请原谅我任何愚蠢的错误。
我想上传一些艺术品,它们将显示为全屏图像。
我遇到的问题是JComponent,我已经扩展为代表我的Image对象,不会调用它的paint()方法&不会在现有的JPanel上绘制。
以下是相关代码段&上传图片&通过扩展JComponent的“art”类实例化 类JFrameMainArt的actionPerformed()方法(仅限相关部分),当OPEN动作结束时,应在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扩展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)
答案 0 :(得分:2)
image
,因此您的paint
方法会抛出NullPointerException
Graphics#drawImage(Image, int, int, ImageObserver)
指的是图像应该被绘制的位置,而不是它的大小(奇怪的是,你的缩放算法是返回0x0
,但我没有花太多时间研究它)this
传递给Graphics#drawImage
的最后一个参数paintComponent
方法的上下文中完成,而不是paint
方法。有关详细信息,请参阅Performing Custom Painting nb-我还尝试将窗格添加到全屏窗口而不会出现问题。我甚至在框架变得可见和添加窗格之间引入了一个小延迟,并且在我更正了NullPointerException
之后让它工作了。
尝试在测试时删除全屏要求...