i am making simple graphics editor, and on the beggining ive got a problem.
I am doing double windows program, i want to click button in one window, choose path of the image, and after that image appear on the second window. I ve done almost everything, but it looks like that: i start the program nothing appears in second window, i read image and it doesnt appear on the second window, it looks like i need something to refresh second window after i read image, but i dont know how to do that, please help.
I know that there is repaint() method, but i dont know how to use repaint in one window and force it to repaint the second one
First file:
package edytor;
import java.awt.*;
import javax.swing.*;
public class Edytor extends JFrame {
public static boolean pom, pom1;
public static Image obraz;
public static String sciezka;
public Edytor() {
super("Edytor 2D");
setBounds(420,50,800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = this.getContentPane();
CObrazek obraz = new CObrazek();
con.add(obraz);
przybornik przyb = new przybornik();
pom = false;
pom1=false;
Edytor.obraz = new ImageIcon(Edytor.sciezka).getImage();
setVisible(true);
}
public static void main(String args[]) {
new Edytor();
}
}
class CObrazek extends Canvas {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(Edytor.obraz, 0,0,null);
g2d.drawString("Wsp x: " , 300, 400);
}
}
Second file:
package edytor;
import java.io.*;
import javax.swing.*;
public class przybornik extends javax.swing.JFrame {
public przybornik() {
initComponents();
setVisible(true);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
read_but = new javax.swing.JButton();
save_but = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
read_but.setText("Wczytaj");
read_but.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
read_butActionPerformed(evt);
}
});
save_but.setText("Zapisz");
save_but.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
save_butActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(read_but, javax.swing.GroupLayout.PREFERRED_SIZE,
184, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(save_but, javax.swing.GroupLayout.DEFAULT_SIZE,
178, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(read_but)
.addComponent(save_but))
.addContainerGap(266, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void read_butActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new
File(System.getProperty("user.home")));
int path = fileChooser.showOpenDialog(this);
if (path == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("Selected file: " +
selectedFile.getAbsolutePath());
Edytor.sciezka = selectedFile.getAbsolutePath();
}
System.out.print(Edytor.sciezka);
}
private void save_butActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(przybornik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(przybornik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(przybornik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(przybornik.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new przybornik().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton read_but;
private javax.swing.JButton save_but;
// End of variables declaration
}