• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

image display problems

 
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);
}
}
 
He's giving us the slip! Quick! Grab this tiny ad!
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic