keď tam mám try catch tak to funguje tak ako chcem
Kód: Vybrať všetko
public class ImageExporter {
private List<GraphicsObject> objects;
private Component frame;
PaintSurface ps = new PaintSurface();
public ImageExporter(List<GraphicsObject> objects) {
this.objects = objects;
objects.add(new FillRectangle(0, 0, ps.getWidth(), ps.getHeight(), Color.yellow));
}
public void saveAsPng(String name, int width, int height) {
if (name == null) {
}
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D ig2 = bi.createGraphics();
Font font = new Font("TimesRoman", Font.BOLD, 20);
ig2.setFont(font);
try {
for (GraphicsObject obj : objects) {
obj.draw(ig2);
}
} catch (NullPointerException e) {
}
try {
String s = (String) JOptionPane.showInputDialog(
null,
"Insert image name\n",
"Image name",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"");
try {
if (s.length() == 0 && s != null) {
JOptionPane.showMessageDialog(frame,
"The name was set to >image<");
s = "image";
}
} catch (NullPointerException nullPointerException) {
}
Object[] possibilities = {"png", "jpg", "bmp"};
String img = (String) JOptionPane.showInputDialog(
frame,
"save as type\n",
"Save type",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"");
try {
if (img.equals("png")) {
ImageIO.write(bi, "PNG", new File("output\\ " + s + ".PNG"));
}
if (img.equals("jpg")) {
ImageIO.write(bi, "JPEG", new File("output\\ " + s + ".JPG"));
}
if (img.equals("bmp")) {
ImageIO.write(bi, "BMP", new File("output\\ " + s + ".BMP"));
}
} catch (NullPointerException nullPointerException) {
}
if (img != null) {
JOptionPane.showMessageDialog(frame,
"The image was saved as output\\" + s + "." + img);
}
} catch (IOException ex) {
Logger.getLogger(ImageExporter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}