Hello all, i am writing up a project just as part of my experiments... (for everyone that has already help with my other questions, thank you for very much.)
Alright i have one file that already works.. both of my source files compiled correctly i have two jpanel's that are overridden so i can change their paintcomponents. One i use as a sort of background image for the application this is the one that works. the problem is with my second panel i am trying to add, for some reason when i start the application, it is either not showing above the first JPanel or it is not showing at all..
here is my code..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class COS extends JPanel implements ActionListener{
static JFrame f=new JFrame();
static Image bgImage=null;
static String message="";
JButton chbg=new JButton("change background");
public COS(){
chbg.setBounds(10,10,150,25);
chbg.addActionListener(this);
add(chbg);
}
public void paintComponent(Graphics g){
if(bgImage!=null){
g.drawImage(bgImage,0,0,this);
}
else{
g.drawString(message,40,40);
}
}
public static void loadbg(){
try{
String xmlpath="background.xml";
SAXBuilder builder=new SAXBuilder();
Document xdoc=builder.build(xmlpath);
String fimg="";
fimg=xdoc.getRootElement().getChild("bgimage").getText();
getFileImage(fimg);
} catch(Exception e){
message="File load failed: "+e.getMessage();
}
}
public static void getFileImage(String filein) throws IOException, InterruptedException{
FileInputStream in=new FileInputStream(filein);
byte[] b=new byte[in.available()];
in.read(b);
in.close();
bgImage=Toolkit.getDefaultToolkit().createImage(b);
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
JFileChooser jfc=new JFileChooser();
if(source==chbg){
int returnVal=jfc.showOpenDialog(null);
if(returnVal==JFileChooser.APPROVE_OPTION){
File file=jfc.getSelectedFile();
String fileone=file.getName();
changebg(fileone);
}
}
}
public void changebg(String filein){
try{
getFileImage(filein);
saveDefaultImage(filein);
repaint();
} catch(IOException e){
} catch(InterruptedException ie){
}
}
public void saveDefaultImage(String filein){
try{
String xmlpath="background.xml";
SAXBuilder builder=new SAXBuilder();
Document xdoc=builder.build(xmlpath);
xdoc.getRootElement().removeChild("bgimage");
xdoc.getRootElement().addContent(new Element("bgimage").setText(filein));
FileOutputStream fos=new FileOutputStream(xmlpath);
XMLOutputter out=new XMLOutputter();
out.output(xdoc, fos);
fos.flush();
fos.close();
} catch(Exception e){
}
}
public static void main(String[] args){
COS newcos=new COS();
COSmp cmp=new COSmp();
cmp.setBounds(720,0,25,600);
cmp.setLayout(null);
loadbg();
f.setSize(825,640);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);
f.setLayout(null);
newcos.setBounds(5,5,800,600);
newcos.setOpaque(false);
newcos.setLayout(null);
f.setLocation(10,5);
f.getContentPane().add(newcos);
f.add(cmp);
f.setVisible(true);
}
}
my second source file..
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class COSmp extends JPanel implements ActionListener{
public COSmp(){
JLabel whatisthis=new JLabel("I am going to be a start menu i think");
add(whatisthis);
}
public void actionPerformed(ActionEvent e){
}
}
the second one is very simple but all of it's methods are in the second the setbounds, add, etc..
i cannot seem to get it to display, even if i set the first one "newcos" is what i call the first one, to not display..
could someone help me? if i didn't explain well enough please tell me and i will try again.
also i just thought about this, the xml file, background.xml
background2.png