• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

image display problems

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic