Hi i am trying to make a simple frame displaying an image a textfield and a button, but for some reason the textfield is invisible, since i am quite nooby at java and even more at these graphical things can you help :) THE CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package solverapplet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import javax.swing.JTextField;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
public class AwtImage extends Frame{
Image img;
String base="R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==";
/*public static void main(String[] args){
AwtImage ai = new AwtImage();
}*/
public void setbase(String a){
this.base=a;
}
public void refreshimage(){
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] imgBytes = decoder.decodeBuffer(this.base);
BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(imgBytes));
//File imgOutFile = new File("newLabel.png");
//ImageIO.write(bufImg, "png", imgOutFile);
img = bufImg;
} catch (IOException ex) {
Logger.getLogger(AwtImage.class.getName()).log(Level.SEVERE, null, ex);
}
}
public AwtImage(){
super("Solve");
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] imgBytes = decoder.decodeBuffer(this.base);
BufferedImage bufImg = ImageIO.read(new ByteArrayInputStream(imgBytes));
MediaTracker mt = new MediaTracker(this);
img=bufImg;
mt.addImage(img,0);
JTextField textfield= new JTextField("Text field 2", 8);
add(textfield,"South");
setSize(400,400);
//pack();
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
dispose();
}
});
} catch (IOException ex) {
Logger.getLogger(AwtImage.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(img != null)
g.drawImage(img, 100, 100, this);
else
g.clearRect(0, 0, getSize().width, getSize().height);
}
}
It gets instantiated by an other class.
The problem is, you've overridden the paint method without making a call to it's super, meaning that it never gets a chance to paint.
public void paint(Graphics g){
if(img != null)
g.drawImage(img, 100, 100, this);
else
g.clearRect(0, 0, getSize().width, getSize().height);
}
When using java.awt.Frame
you will find it difficult to achieve what you. java.swingx.JFrame
allows you to override the paintComponent
method, which allows you to paint the background of the component, but the java.awt.Frame
does not.
You should also avoid mixing light and heavy weight components (I know, it's apparently fixed), but if you can, just avoid it.