🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #1239

Re: how do I draw a JPG to a panel/container

#1239
From: PeterH
Date: Wed, 17 Apr 2002 02:29
103 lines
4364 bytes
I'm sorry to bug you again, but I've run into another problem.  You touched
on this one before, so I'm hoping it's a simple thing I'm just not seeing
this late at night.  (I should try more coffee, I know :>).

I've typed in the following code (easy on the code review, I'm playing with
things :>).  And I was able to get things to load up without the code for
the scaledImage.  I was trying to scale the image to the current frame size.
I saw the Image.getScaledInstance() and thought that would do it, but now I
can't see anything on the screen (no picture I mean).  If I take a look a
the width method, it says -1, so that would lead me to believe the image
hasn't loaded or has an error.  What do you think?

Thanks.



----------------------------------------------------------------------------
-------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Image;

public class TestThis{

   public static void main( String[] args ){
      System.err.println( "BEG" );

      JFrame frame = new TestThisFrame();
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      frame.show();

      System.err.println( "END" );
   }//main
}//TestThis

class TestThisFrame extends JFrame{
   private Image image;
   private Image scaledImage;
   private int frameWidth = 0;
   private int frameHeight = 0;

   public TestThisFrame(){
      super( "This is the Title for my TestThisFrame" );
      System.err.println( "TestThisFrame constructor");
      image = Toolkit.getDefaultToolkit().getImage(
"c:/temp/temp/IMG_1738.JPG" );
      image.getSource();
      repaint();
      this.setSize( 400, 400 );

   }

   public void paint( Graphics g ){
      System.err.println("paint");
      frameWidth = getWidth();
      frameHeight = getHeight();

      //scaledImage = image.getScaledInstance( frameWidth, frameHeight,
Image.SCALE_DEFAULT );
      scaledImage = image.getScaledInstance( 300, 300,
Image.SCALE_DEFAULT );
      scaledImage.getSource();
      g.drawImage( scaledImage, 1, 1, this );
   }

//   public void update( Graphics g ){
//      System.err.println("update");
//      paint( g );
//   }
}//TestThisFrame

----------------------------------------------------------------------------
-------------------




"Gillmer J. Derge [TeamB]" <spam@gillmerderge.com> wrote in message
news:3cbc9afb_2@dnews...
> "PeterH" <!REMOVEBeforeSending!peter@pchenry.com> wrote in message
> news:3cbc80fd$1_2@dnews...
> > In the JFrame object, there is a getGraphics() method.  How come I can't
> use
> > that to get a Graphics object and use the drawImage() from there?
>
> That probably works except your work is destroyed before you even see it.
> The problem is that the paint method is called automatically in response
to
> various user interface events.  For example, when the window first appears
> on the screen or when the window is uncovered by moving another window,
the
> paint method will be called to draw the window's contents.  Your approach
> drew the image, but then at some point paint was called, and it redrew the
> window right on top of your work.  That's why overriding paint or
> paintComponent will work.
>
> --
> Gillmer J. Derge [TeamB]
>
>
>
Technical Details
Message-ID: <3cbcdf8a_1@dnews>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.online.be!news.algonet.se!algonet!news000.worldonline.se!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!dnews
References: <3cb9ef73$1_1@dnews> <MPG.172579c5bd0dc2ea98b534@newsgroups.borland.com> <3cbc80fd$1_2@dnews> <3cbc9afb_2@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,422c9cef40be2392
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2002-04-16 19:39:30 PST
From: "PeterH" <!Sorry-TooMuchSpam!nope@nope.com>
Newsgroups: borland.public.jbuilder.java.api
References: <3cb9ef73$1_1@dnews> <MPG.172579c5bd0dc2ea98b534@newsgroups.borland.com> <3cbc80fd$1_2@dnews> <3cbc9afb_2@dnews>
Subject: Re: how do I draw a JPG to a panel/container
Date: Tue, 16 Apr 2002 22:29:00 -0400
Lines: 105
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
NNTP-Posting-Host: 24.112.10.56
Message-ID: <3cbcdf8a_1@dnews>
X-Trace: dnews 1019010954 24.112.10.56 (16 Apr 2002 19:35:54 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.online.be!news.algonet.se!algonet!news000.worldonline.se!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!dnews