• Post Reply Bookmark Topic Watch Topic
  • New Topic

image display problems

 
simcel jones
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Thanks for your time
I am experiencing problems with displaying an image
In the code below I have a JFrame with a JPanel holding three other JPanels
the final panel displays a simple image
when this is displayed however in the background is the drawing from the first panel in error
I have played around with what I display in the other two Panels and it seems that this problem only occurs when drawLine is used not drawString
can anyone help
thanks
simon
import java.awt.*;
import javax.swing.*;
public class immage extends JFrame {
public immage() {
setSize(500,550);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel holder=new JPanel(new GridLayout(3,1));
graphPanel g=new graphPanel();
helloPanel h=new helloPanel();
imagePanel i=new imagePanel();
holder.add(g);
holder.add(h);
holder.add(i);
setContentPane(holder);
show();
}

public static void main(String args[]) {
immage f = new immage();
}
}

//support class
class graphPanel extends JPanel {
int width;
int height;
public void paintComponent(Graphics g) {
width=getWidth();
height=getHeight();
g.setColor(Color.black);
g.drawLine(0,height/2,width,height/2);
g.drawLine(width/2,3,width/2,height);
}
}

//support class
class helloPanel extends JPanel {
public void paintComponent(Graphics g) {
g.drawString("hello",50,10);
}
}

//support class
class imagePanel extends JPanel {
Image hi;
imagePanel() {
Toolkit kit=Toolkit.getDefaultToolkit();
hi=kit.getImage("hello.gif");
}
public void paintComponent(Graphics g) {
g.drawImage(hi,50,10,this);
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!