R
Random
Gast
Tag - ich bins wieder. Es geht um mein aktuelles Programm. Erstmal der Quelltext:
EditorForm.java
ForegroundPic.java
BackgroundPic.java
Das Problem ist die Methode [c]jMenuItemSetBackgroundActionPerformed[/c] (EditorForm.java). Hierbei kann man ein Hintergrundbild für das Frame (bzw. die ContentPane) bestimmen. Wenn ich dann ein Hintergrundbilde lade bleibt der Hintergrund zunächst unverändert, erst wenn ich die Größe des Fensters ändere wird das Hintergrundbild angezeigt. Wenn ich jedoch schon zuvor mit der Methode [c]jMenuItemLoadImgActionPerformed[/c] (auch EditorForm.java) Bilder geladen habe werden diese dannach nicht mehr angezeigt (nach dem Laden des Hintergrundbildes (also vor dem Größe-Ändern) sind sie noch sichtbar). Kann mir jemand sagen, wie ich das Hintergrundbild gleich anzeigen lassen kann und dannach die ForegroundPics zeichnen lassen kann?
Vielen Dank im Voraus!
EditorForm.java
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* EditorForm.java
*
* Created on 13.06.2011, 15:17:50
*/
package H_Game;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
/**
*
* @author Carl Hamann
*/
public class EditorForm extends javax.swing.JFrame
{
// ************************************
// ----- Liste der hinzugefügten Bilder -----
private ArrayList <ForegroundPic> PicList = new ArrayList <ForegroundPic>();
// ----- Das Hintergrundbild -----
private BackgroundPic BackGround = null;
// ************************************
/** Creates new form EditorForm */
public EditorForm()
{
initComponents();
InitHEditor();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
MenuBar = new javax.swing.JMenuBar();
jMenuGeneral = new javax.swing.JMenu();
jMenuItemSetBackground = new javax.swing.JMenuItem();
jMenuAdd = new javax.swing.JMenu();
jMenuItemLoadImg = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("H_Editor");
setAlwaysOnTop(true);
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
formComponentResized(evt);
}
});
jMenuGeneral.setText("Allgemein");
jMenuItemSetBackground.setText("Hintergrundbild laden");
jMenuItemSetBackground.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemSetBackgroundActionPerformed(evt);
}
});
jMenuGeneral.add(jMenuItemSetBackground);
MenuBar.add(jMenuGeneral);
jMenuAdd.setText("Hinzufügen");
jMenuItemLoadImg.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
jMenuItemLoadImg.setText("Bild");
jMenuItemLoadImg.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItemLoadImgActionPerformed(evt);
}
});
jMenuAdd.add(jMenuItemLoadImg);
MenuBar.add(jMenuAdd);
setJMenuBar(MenuBar);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-748)/2, (screenSize.height-396)/2, 748, 396);
}// </editor-fold>
private void jMenuItemLoadImgActionPerformed(java.awt.event.ActionEvent evt)
{
// Über Filechooser Datei suchen
JFileChooser PicChooser = new JFileChooser();
PicChooser.setFileFilter(new FileFilter()
{
@Override public boolean accept (File f)
{
return f.getName().endsWith(".jpg")
|| f.getName().endsWith(".jpeg")
|| f.getName().endsWith(".png")
|| f.getName().endsWith(".gif")
|| f.isDirectory();
}
@Override public String getDescription ()
{
return "Bilddateien";
}
});
int retVal = PicChooser.showOpenDialog(this);
if (retVal == JFileChooser.APPROVE_OPTION)
{
// Neues Bild eintragen
PicList.add(new ForegroundPic(0, 0, PicChooser.getSelectedFile().getPath(), this));
getContentPane().add(PicList.get(PicList.size()-1));
RepaintPics();
// In Order img kopieren
File SrcFile = new File(PicChooser.getSelectedFile().getPath());
File DestFile = new File("img/" + PicChooser.getSelectedFile().getName());
try
{
FileChannel iChannel = new FileInputStream(SrcFile).getChannel();
FileChannel oChannel = new FileOutputStream(DestFile).getChannel();
iChannel.transferTo(0, iChannel.size(), oChannel);
if (iChannel != null)
iChannel.close();
if (oChannel != null)
oChannel.close();
}
catch (IOException ex)
{
Logger.getLogger(EditorForm.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Datei nicht gefunden!");
}
}
}
private void jMenuItemSetBackgroundActionPerformed(java.awt.event.ActionEvent evt)
{
// Über Filechooser Datei suchen
JFileChooser PicChooser = new JFileChooser();
PicChooser.setFileFilter(new FileFilter()
{
@Override public boolean accept (File f)
{
return f.getName().endsWith(".jpg")
|| f.getName().endsWith(".jpeg")
|| f.getName().endsWith(".png")
|| f.getName().endsWith(".gif")
|| f.isDirectory();
}
@Override public String getDescription ()
{
return "Bilddateien";
}
});
int retVal = PicChooser.showOpenDialog(this);
if (retVal == JFileChooser.APPROVE_OPTION)
{
// Neues Bild anzeigen
BackGround = new BackgroundPic(PicChooser.getSelectedFile().getPath());
BackGround.setLayout(null);
setContentPane(BackGround);
RepaintPics();
// In Order img kopieren
File SrcFile = new File(PicChooser.getSelectedFile().getPath());
File DestFile = new File("img/" + PicChooser.getSelectedFile().getName());
try
{
FileChannel iChannel = new FileInputStream(SrcFile).getChannel();
FileChannel oChannel = new FileOutputStream(DestFile).getChannel();
iChannel.transferTo(0, iChannel.size(), oChannel);
if (iChannel != null)
iChannel.close();
if (oChannel != null)
oChannel.close();
}
catch (IOException ex)
{
Logger.getLogger(EditorForm.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Datei nicht gefunden!");
}
}
}
private void formComponentResized(java.awt.event.ComponentEvent evt)
{
//JOptionPane.showMessageDialog(null, "Resized!");
}
private void InitHEditor ()
{
// ggf. Ordner erstellen
File DirImg = new File("img");
if (!DirImg.exists())
DirImg.mkdir();
}
public void RepaintPics ()
{
for (ForegroundPic fp : PicList)
fp.repaint();
}
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EditorForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuBar MenuBar;
private javax.swing.JMenu jMenuAdd;
private javax.swing.JMenu jMenuGeneral;
private javax.swing.JMenuItem jMenuItemLoadImg;
private javax.swing.JMenuItem jMenuItemSetBackground;
// End of variables declaration
}
ForegroundPic.java
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package H_Game;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
/**
*
* @author Carl Hamann
*/
public class ForegroundPic extends JLabel
{
private ImageIcon imgico = null;
private Image img = null;
private EditorForm eForm = null;
private int x, y;
ForegroundPic (int x, int y, String path, final EditorForm eForm)
{
this.x = x;
this.y = y;
this.eForm = eForm;
Toolkit toolkit = Toolkit.getDefaultToolkit();
MediaTracker mediatracker = new MediaTracker(this);
img = toolkit.getImage(path);
mediatracker.addImage(img, 0);
try
{
mediatracker.waitForID(0);
}
catch (InterruptedException e)
{
img = null;
}
// Icon laden
imgico = new ImageIcon(img);
this.setIcon(imgico);
this.setSize(getPreferredSize());
this.setLocation(x, y);
addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
{
@Override public void mouseDragged(java.awt.event.MouseEvent evt)
{
PicMouseDragged(evt);
}
});
addMouseListener(new java.awt.event.MouseAdapter()
{
@Override public void mouseReleased(java.awt.event.MouseEvent evt)
{
PicMouseReleased(evt);
}
});
}
ForegroundPic (int x, int y, URL path, final EditorForm eForm)
{
this.x = x;
this.y = y;
Toolkit toolkit = Toolkit.getDefaultToolkit();
MediaTracker mediatracker = new MediaTracker(this);
img = toolkit.createImage(path);
mediatracker.addImage(img, 0);
try
{
mediatracker.waitForID(0);
}
catch (InterruptedException e)
{
img = null;
}
// Icon laden
imgico = new ImageIcon(img);
this.setIcon(imgico);
this.setSize(getPreferredSize());
this.setLocation(x, y);
addMouseMotionListener(new java.awt.event.MouseMotionAdapter()
{
@Override public void mouseDragged(java.awt.event.MouseEvent evt)
{
PicMouseDragged(evt);
}
});
addMouseListener(new java.awt.event.MouseAdapter()
{
@Override public void mouseReleased(java.awt.event.MouseEvent evt)
{
PicMouseReleased(evt);
}
});
}
public void SetX (int x)
{
this.x = x;
}
public void SetY (int y)
{
this.y = y;
}
public int GetX ()
{
return x;
}
public int GetY ()
{
return y;
}
public void PicMouseDragged (MouseEvent evt)
{
// Cursor ändern
eForm.setCursor(new Cursor(Cursor.MOVE_CURSOR));
// Position ändern
Rectangle rect = this.getBounds();
x = rect.x + evt.getX();
y = rect.y + evt.getY();
// Location ändern
setLocation(rect.x + evt.getX(),
rect.y + evt.getY());
}
public void PicMouseReleased(MouseEvent evt)
{
eForm.setCursor(Cursor.DEFAULT_CURSOR);
}
}
BackgroundPic.java
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package H_Game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JPanel;
/**
*
* @author Carl Hamann
*/
public class BackgroundPic extends JPanel
{
private Image img = null;
BackgroundPic (String path)
{
if (path != null)
{
MediaTracker mt = new MediaTracker(this);
img = Toolkit.getDefaultToolkit().getImage(path);
mt.addImage(img, 0);
try
{
mt.waitForAll();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
@Override protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
Das Problem ist die Methode [c]jMenuItemSetBackgroundActionPerformed[/c] (EditorForm.java). Hierbei kann man ein Hintergrundbild für das Frame (bzw. die ContentPane) bestimmen. Wenn ich dann ein Hintergrundbilde lade bleibt der Hintergrund zunächst unverändert, erst wenn ich die Größe des Fensters ändere wird das Hintergrundbild angezeigt. Wenn ich jedoch schon zuvor mit der Methode [c]jMenuItemLoadImgActionPerformed[/c] (auch EditorForm.java) Bilder geladen habe werden diese dannach nicht mehr angezeigt (nach dem Laden des Hintergrundbildes (also vor dem Größe-Ändern) sind sie noch sichtbar). Kann mir jemand sagen, wie ich das Hintergrundbild gleich anzeigen lassen kann und dannach die ForegroundPics zeichnen lassen kann?
Vielen Dank im Voraus!