Вывод изображения на JPanel - Java (241563)
Формулировка задачи:
Добрый вечер!
Делаю приложение для вывода выбранной картинки на панель. Но картинка к сожалению не отрисовывается на панели. Помогите пожалуйста разобраться в том, что я делаю не так. Спасибо!
package saprtest;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SaprTest {
JFrame jf = new JFrame("Тестовое окно");
JPanel jp1 = new JPanel(new BorderLayout());
WorkPanel wp1 = new WorkPanel();
JButton jb1 = new JButton("Открыть");
JButton jb2 = new JButton("Выход");
JOptionPane jop1 = new JOptionPane();
public static void main(String[] args) {
SaprTest sp1 = new SaprTest();
sp1.start();
}
public void start (){
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(jp1, BorderLayout.WEST);
jp1.setSize(100, 100);
jp1.add(jb1, BorderLayout.NORTH);
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JFileChooser openFile = new JFileChooser();
openFile.setDialogTitle("Выбор графической подложки");
openFile.setFileSelectionMode(JFileChooser.FILES_ONLY);
//openFile.setFileFilter(new MyFileFilter(".jpg", ""));
int ret = openFile.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION){
try {
File file = openFile.getSelectedFile();
Image image = ImageIO.read(file);
Graphics gr = image.getGraphics();
wp1.paintComponent(gr, image);
}
catch (FileNotFoundException ex){
JOptionPane.showMessageDialog(jop1, "Такого файла не существует");
}
catch (IOException ex) {
JOptionPane.showMessageDialog(jop1, "Исключение ввода-вывода");
}
}
}
});
jp1.add(jb2, BorderLayout.SOUTH);
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
jf.add(wp1, BorderLayout.CENTER);
wp1.setVisible(true);
//wp1.setBackground(Color.black);
wp1.setPreferredSize(new Dimension(100, 100));
}
}package saprtest;
import java.awt.Graphics;
import java.awt.Image;
public class WorkPanel extends javax.swing.JPanel {
@Override
protected void paintComponent (Graphics g) {
super.paintComponent(g);
g.drawImage(null, 0, 0, this);
}
protected void paintComponent(Graphics g, Image img){
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
}Решение задачи: «Вывод изображения на JPanel»
textual
Листинг программы
package javafxpixel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
/**
* @web [url]http://java-buddy.blogspot.com/[/url]
*/
public class JavaFXPixel extends Application {
ImageView myImageView;
@Override
public void start(Stage primaryStage) {
Button btnLoad = new Button("Load");
btnLoad.setOnAction(btnLoadEventListener);
myImageView = new ImageView();
VBox rootBox = new VBox();
rootBox.getChildren().addAll(btnLoad, myImageView);
Scene scene = new Scene(rootBox, 300, 300);
primaryStage.setTitle("java-buddy.blogspot.com");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
EventHandler<ActionEvent> btnLoadEventListener
= new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
myImageView.setImage(image);
} catch (IOException ex) {
Logger.getLogger(JavaFXPixel.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
}