Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projektjpwmiijedralskik1;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.io.File;
- import javax.swing.ImageIcon;
- import javax.swing.JFileChooser;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import static java.awt.Image.SCALE_SMOOTH;
- /**
- *
- * @author Konrad
- */
- public class JFrame extends javax.swing.JFrame {
- private Image image;
- public JFrame() {
- initComponents();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jLabel1 = new javax.swing.JLabel();
- jButton1 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("Konrad Jędralski");
- jLabel1.setText("Obraz...");
- jLabel1.addComponentListener(new java.awt.event.ComponentAdapter() {
- public void componentResized(java.awt.event.ComponentEvent evt) {
- jLabel1ComponentResized(evt);
- }
- });
- jButton1.setText("Wybierz obraz");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap(338, Short.MAX_VALUE)
- .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
- .addContainerGap(320, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jButton1)
- .addGap(17, 17, 17))
- );
- pack();
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- JFrame frame = new JFrame();
- frame.setLocation(200, 200);
- JFileChooser fileChooser = new JFileChooser();
- FileNameExtensionFilter filter = new FileNameExtensionFilter("Grafika JPG", "jpg");
- fileChooser.setFileFilter(filter);
- fileChooser.showOpenDialog(frame);
- File file = fileChooser.getSelectedFile();
- Toolkit toolkit = Toolkit.getDefaultToolkit();
- image = toolkit.getImage(file.toString());
- jLabel1.setText("");
- int labelWidth = jLabel1.getWidth();
- int labelHeight = jLabel1.getHeight();
- Image imageTemp = image.getScaledInstance(labelWidth, labelHeight, Image.SCALE_SMOOTH);
- ImageIcon imageIcon = new ImageIcon(imageTemp);
- jLabel1.setIcon(imageIcon);
- }
- private void jLabel1ComponentResized(java.awt.event.ComponentEvent evt) {
- if (image != null) {
- jLabel1.setSize(this.getWidth(), this.getHeight());
- Image imageTemp = image.getScaledInstance(jLabel1.getWidth(), jLabel1.getHeight(), Image.SCALE_SMOOTH);
- ImageIcon imageIcon = new ImageIcon(imageTemp);
- jLabel1.setIcon(imageIcon);
- }
- }
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new JFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JLabel jLabel1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement