Painting Problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Painting Problem

  1. #1
    Join Date
    Aug 2003
    Posts
    313

    Painting Problem

    I'm having a problem painting a background on a JPanel. I have the following written:
    Code:
    public class ImageBackgroundPainter implements IBackgroundPainter {
      private Image _image;
      public ImageBackgroundPainter(Image img) {
        _image = img;
      }
      public void init(JComponent comp) {
        comp.setSize(_image.getWidth(comp), _image.getHeight(comp));
      }
      public void paint(JComponent comp, Graphics g) {
        System.out.println("Painting..."+_image);
        g.drawImage(_image, 0, 0, comp);
      }
    }
    
    public class HotSpotPanel extends JPanel {
      private IBackgroundPainter _background;
    
      public HotSpotPanel(IBackgroundPainter background) {
        super(true);
        _background = background;
        initComponents();
      }
    
      public void paint(Graphics g) {
        super.paint(g);
        _background.paint(this,g);
        paintComponents(g);
      }
    
      protected void initComponents() {
    
      }
    }
    I'm working in an Applet and I'm calling getImage() to get the image. I get the message saying "Painting..." but the JPanel is still all gray. Does anyone have any ideas why this wouldn't work? Thanks a lot.
    ~evlich

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    I tracked my problem back to getting the image. Apparently it wasn't loaded properly. Anyone know what is wrong with this code:
    Code:
    public class WebApplet extends JApplet {
      private MediaTracker _tracker;
    
      private IResourceAdapter buildResourceAdapter() {
          return new IResourceAdapter() {
            public void changeGUI(JComponent display) {
              Container pane = getContentPane();
              pane.removeAll();
              pane.add(display);
              pane.validate();
            }
    
            public Image loadImage(String resourceName) {
              try {
                System.out.println("Trying to get: "+resourceName);
                Image img = getImage(new URL("file://"+resourceName));
                _tracker.addImage(img,0);
                _tracker.waitForAll();
                System.out.println("Checkall returned "+ _tracker.checkAll());
    
                return img;
              } catch (MalformedURLException e) {
                e.printStackTrace();
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              return null;
            }
          };
        }
    
        public void init() {
          _tracker = new MediaTracker(this);
          new SimController(getParameter("classpath"), getParameter("startroom"), this.buildResourceAdapter());
        }
    }
    I am using "file://" since the image is on my hardrive, for the time being. Any help would be greatly appreciated.
    ~evlich

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center