redho home | products | services

Programming Forums


Community for Java, PHP, Perl C, ASP and Python programmers
Friday 03 September 2010 04:50

Ask your IT question here

Java GUI problem - paintComponent problem?



 
Post new topic   Reply to topic    Programming Forums -> JAVA programming forum
View previous topic :: View next topic  
Author Message
Richard
Guest Programmer




Hi everyone. I can't seem to get my java code to work. I suspect it's a problem with the way I've coded my paintComponent methods, but for the life of me I can't figure out where the problem is.

Instead of drawing a pretty picture on screen, all I get is a vast expanse of nothing. Any/all input would be greatly appreciated Smile Oh, and yes - the image file is definitely in the correct directory.

Code:

import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrontEnd extends JFrame {

   public FrontEnd() {
      Image miniIcon = new ImageIcon("icon.gif").getImage();
      this.setIconImage(miniIcon);
      MainScreen mainScr = new MainScreen();
      add(mainScr);
      mainScr.repaint();
   }

   public static void main(String args[]) {
      FrontEnd window = new FrontEnd();
      window.setTitle("Blah");
      window.setSize(400, 400);
      window.setLocationRelativeTo(null);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setVisible(true);

   }

}

//#####################################################################################################

class MainScreen extends JPanel {

   public MainScreen() {

      JPanel p = new JPanel(new GridLayout(1, 1, 0, 0));
      System.out.println("Container panel created");

      Image image = new ImageIcon("icon.gif").getImage();
      ImageViewer viewPanel = new ImageViewer(image);
      add(viewPanel);
      viewPanel.repaint();      

   }
   
   protected void paintComponent(Graphics g) {
      ImageViewer.repaint();
      System.out.println("The main screen is redrawn");
   }
   
   
}

//#####################################################################################################

class ImageViewer extends JPanel {

   private java.awt.Image image;

   public ImageViewer(Image image) {
      this.image = image;   
      System.out.println("ImageViewer constructor called");
   }

   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawImage(image, 0, 0, this);
      System.out.println("ImageViewer paintComponent called");
   }

}

Reply with quote
 
devunion
New Programmer


Joined: 04 Feb 2009
Posts: 8
Try to use
g.drawImage(image, image.getWidth(), image.getHeight(), this);

Use Toolkit.getDefaultToolkit().getImage(filename) to load your image.
_________________
Welcome to Freelancer's world-wide portal

Reply with quote
 
Page 1 of 1
Post new topic   Reply to topic    Programming Forums -> JAVA programming forum


Dubai Forums - Expat Help | Vegan Forum | Java Programming | 3d Design Resources | 3d Forum | 3D Jobs | 3D Textures | Paris Forum | Europe Forum
Dubai Classifieds | Dubai Property | Jobs in Dubai | Free London Classifieds | Jobs in London UK

High Quality, Custom 3d animation and Web Design solutions Royal Quality Web Hosting Services Vegetarian and Animal Rights news

Based on phpBB © 2001-2010 RedHo