2017-05-20 14 views
0

간단한 그래픽 편집기를 만들고 있습니다.어떻게 창을 새로 고침 할 수 있습니까?

나는 이중 창 프로그램을하고 있는데, 한 창에서 단추를 클릭하고 이미지의 경로를 선택하고 그 이미지가 두 번째 창에 나타나는 후에. 거의 모든 일을했지만 그 것처럼 보입니다. 나는 프로그램을 시작하지 않고 두 번째 창에 아무 것도 나타나지 않고 이미지를 읽었고 두 번째 창에 그다지 나타나지 않습니다. 이미지를 읽은 후 두 번째 창을 새로 고칠 필요가있는 것처럼 보입니다. 나는 그 일을하는 법을 모릅니다. 제발 도와주세요.

나는) (칠 방법이 있다는 것을 알고,하지만 난 하나 개의 창에서 다시 칠 사용하고 두 번째

먼저 파일을 다시 칠하도록 강제하는 방법을 잘 모릅니다 :

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); 
    } 
} 

두 번째 파일 :

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     
} 
+1

할 수 있습니다하시기 바랍니다 당신이 시도한 코드를 게시하십시오 – poisonedYouth

답변

0

방금 ​​내가 변경하고 추가 한 장소를 보여줍니다. 편집 :

private static boolean isWindowActive = false; //for window active. 
public przybornik() { 
    initComponents(); 
    if(isWindowActive == false){ //window active check here. 
    setVisible(true); 
    isWindowActive = true; 
    } 
} 

// --------과 :

public class Edytor extends JFrame{ 
     CObrazek obraz = new CObrazek();//here (new place) 
     public static boolean pom, pom1; 
     public static Image image; 
     public static String sciezka; 
     public void EdytorShow(String image_path) {//changed 
      setBounds(420,50,800,600); 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      Container con = this.getContentPane(); 
      //CObrazek obraz = new CObrazek();//disabled 
      przybornik przyb = new przybornik();//here (new place) 
      /** 
      * We do not want the window to appear without selecting an image. 
      * For this reason we will use "if" 
      */ 
      if(!image_path.equals("null")){ 
       con.add(obraz); 
      // przybornik przyb = new przybornik();//Moved out of if ;) 
      pom = false; 
      image = new ImageIcon(image_path).getImage(); 
      setVisible(true); 
      }//if 
     } 

     public static void main(String args[]) { 
      Edytor et = new Edytor(); 
      et.EdytorShow("null"); 
     } 


} 

그리고 "przybornik.java"파일에

 Edytor edytor = new Edytor();//here (new place) 
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(); 
     //new Edytor().EdytorShow(selectedFile.getAbsolutePath());//disabled(Moved out of function ;) 
     edytor.EdytorShow(selectedFile.getAbsolutePath());//newly added 
    } 

    System.out.print(Edytor.sciezka); 
} 
+0

대단히 감사합니다, 당신은 최고입니다;) – lifififi

+0

하나의 질문, 왜 두 창은 내가 이미지를로드 한 후에 그려지고 있습니까? 어떻게 든 그것을 바꿀 수 있습니까, 나는 더 많은 윈도우를 갖고 싶지 않습니다. 현재 이미지를로드하고 싶습니다. – lifififi

+0

항상 같은 창 이미지를로드 하시겠습니까? –