Hallo,
ich habe eine Dialog in dem ein Bild geladen werden soll. Dazu habe ich eine Klasse verwendet die das dann auch zeichnen soll. Diese Komponente wird einem Panel zugefügt. Das Bild bzw. die Datei scheint auch anzukommen. E wird aber offenbar nicht gezeichnet. Ich kann leider nicht nachvollziehen warum.
Danke für die Hinweise
Gruß niesel
ich habe eine Dialog in dem ein Bild geladen werden soll. Dazu habe ich eine Klasse verwendet die das dann auch zeichnen soll. Diese Komponente wird einem Panel zugefügt. Das Bild bzw. die Datei scheint auch anzukommen. E wird aber offenbar nicht gezeichnet. Ich kann leider nicht nachvollziehen warum.
Java:
package guientwurf;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
class ViewComponent extends JComponent {
private Image image;
public void setImage(File file) {
image = Toolkit.getDefaultToolkit().getImage(file.getAbsolutePath());
System.out.print("setImage="+file.getAbsolutePath());
if (image != null) {
System.out.println("Repaint");
repaint();
}
}
@Override
protected void paintComponent(Graphics g) {
if (image != null) {
System.out.println("Imgage");
g.drawImage(image, 0, 0, this);
}
}
}
public class JNewAnPicture extends javax.swing.JDialog {
private ViewComponent viewComponente;
/** Creates new form JNewAnPicture */
public JNewAnPicture(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
viewComponente = new ViewComponent();
jPanel1.add(viewComponente);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 423, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 357, Short.MAX_VALUE)
);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 281, Short.MAX_VALUE)
.addComponent(jButton2)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(13, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser jc = new JFileChooser();
//jc.showOpenDialog(jc);
jc.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".jpg")
|| f.getName().toLowerCase().endsWith(".gif")
|| f.getName().toLowerCase().endsWith(".png");
}
@Override
public String getDescription() {
return "*.jpg;*.gif";
}
});
jc.showOpenDialog(null);
File file = jc.getSelectedFile();
System.out.println(file.getAbsolutePath());
viewComponente.setImage(file);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
Danke für die Hinweise
Gruß niesel