Article View: borland.public.jbuilder.java.swing
Article #7607Re: Here's some more information
From: Reid
Date: Sun, 07 Jan 2001 18:56
Date: Sun, 07 Jan 2001 18:56
125 lines
4967 bytes
4967 bytes
Hello Jason,
I am new to Java and trying to do something very similar to what you are
doing.
I have an applet that contains a combobox and a JPanel below it. My
objective is to paint an image on the JPanel when the user selects a
title from the drop down list.
At this stage I would be happy just to get an image to draw on the
JPanel when the applet starts.
I have been unsuccessful so far.
I tried your code and got the same result.
I think I found the problem in your code. I actually kind of stumbled
on it by trying two things.
First I created a "176.JPG" image in my root drive and set a breakpoint
right on the "g.drawImage(image1,0,0,this);" line. I noticed that the
image1 variable seemed to have a height and width of "-1".
Second I noticed that if I tried to change the "C:\176.JPG" to a
"C:\ABC.JPG" the compiler complained about an "illegal escape character"
... Which seemed quite strange considering a filename of "C:\ABC.JPG"
is just as valid as any other.
I then changed your "C:\176.JPG" to "C:\\176.JPG" adding the double
backslash (addressing the escape character) and wholla! The image loaded.
I hope this solves your problem.
Unfortunately it doesn't solve mine because I am loading my images using
the getDocumentBase() function .....
imageArray[0] = getImage(getDocumentBase(), "itaxsystem.jpg");
tracker.addImage(imageArray[0], 0);
In my applet I can paint the image to the applet's entire canvas but not
just the JPanel's canvas so I am confident that the images are loading
correctly, just not painting..
I have a thread in the borland.public.jbuilder.applet-issues forum
entitled "Painting an Image on aJPanel" . If you can figure out in my
code why the ImageCanvas component's paint method is not getting called
let me know.
Reid
Jason Swett wrote:
> I tried writing the following application after reading a bit more. I
> created my own "ImagePanel" by slightly extending JPanel, but still can't
> get the image to display. Can you tell me what's wrong with the code?
> Thanks. (I added a re-paint button just for kicks, but it doesn't
> accomplish anything.)
>
> package testpaint1;
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
>
> public class Frame1 extends JFrame {
> JPanel contentPane;
> JButton jButton1 = new JButton();
> ImagePanel imagePanel1 = new ImagePanel();
>
> /**Construct the frame*/
> public Frame1() {
> enableEvents(AWTEvent.WINDOW_EVENT_MASK);
> try {
> jbInit();
> }
> catch(Exception e) {
> e.printStackTrace();
> }
> }
>
> /**Component initialization*/
> private void jbInit() throws Exception {
>
> jButton1.setText("Repaint");
> jButton1.setBounds(new Rectangle(28, 31, 79, 27));
> jButton1.addActionListener(new java.awt.event.ActionListener() {
> public void actionPerformed(ActionEvent e) {
> jButton1_actionPerformed(e);
> }
> });
> contentPane = (JPanel) this.getContentPane();
> contentPane.setLayout(null);
> this.setSize(new Dimension(400, 300));
> this.setTitle("Frame Title");
> imagePanel1.setBounds(new Rectangle(28, 79, 227, 132));
> contentPane.add(jButton1, null);
> contentPane.add(imagePanel1, null);
> }
>
> /**Overridden so we can exit when window is closed*/
> protected void processWindowEvent(WindowEvent e) {
> super.processWindowEvent(e);
> if (e.getID() == WindowEvent.WINDOW_CLOSING) {
> System.exit(0);
> }
> }
>
> void jButton1_actionPerformed(ActionEvent e) {
> repaint();
> }
>
> }
>
> // Define a custom Jpanel -- with images
>
> class ImagePanel extends JPanel {
> public void paintComponent(Graphics g) {
> super.paintComponent(g);
> Toolkit toolkit = Toolkit.getDefaultToolkit();
> Image image1 = toolkit.getImage("C:\176.JPG");
> g.drawImage(image1,0,0,this);
> }
> }
>
>
>
Technical Details
Message-ID:
<3A58BBD6.9090104@buzybee.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.mesh.ad.jp!newsfeed.rt.ru!news.algonet.se!algonet!neel.uni2.net!newssuck.uni2.dk!bornews.borland.com!dnews!209.86.217.6References:
<3a587c79_2@dnews> <3a588647_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,3d5e8a1122bfe253 X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2001-01-07 10:56:54 PST Message-ID: <3A58BBD6.9090104@buzybee.com> Date: Sun, 07 Jan 2001 10:56:22 -0800 From: Reid <reidr@buzybee.com> User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; m18) Gecko/20001108 Netscape6/6.0 X-Accept-Language: en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: Here's some more information References: <3a587c79_2@dnews> <3a588647_2@dnews> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 209.86.217.6 X-Trace: 7 Jan 2001 10:47:23 -0800, 209.86.217.6 Lines: 126 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!newsfeed.mesh.ad.jp!newsfeed.rt.ru!news.algonet.se!algonet!neel.uni2.net!newssuck.uni2.dk!bornews.borland.com!dnews!209.86.217.6