This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Problem with components/ widgets using JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with components/ widgets using JPanel" Watch "Problem with components/ widgets using JPanel" New topic
Author

Problem with components/ widgets using JPanel

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hello,
I'm new to Java and have written the below code while going through the Swing chapter:

package GUI;

import java.awt.*;

import javax.swing.*;

public class MyDrawPanel extends JPanel
{
public void paintComponenets(Graphics g)
{
Image image = new ImageIcon("C:/hyd.jpg","meaningless picture").getImage();
g.drawImage(image, 300,400, this);

g.drawRect(50, 50, 250, 50);
g.setColor(Color.red);
g.fillRect(0, 0, 250, 750);
}
}


But nothing is happening when I use an instance of MyDrawPanel in the main method which also contains a button.
I've checked ActionPerformed(ActionEvent event) and it's working fine.

what is the correct way to metion path of a picture ? (I didn't get it even in api ).

Thanks a lot...
Pramod

Thanks,
Pramod
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You'll want to scrap this code and start over. For one, paintComponents is not the same as paintComponent, in fact there's a big difference. Next, you'll not want to read in images or do other file IO in a paintComponent method. Re-read the tutorials as they'll show the proper syntax. Best of luck and welcome to Java and the Ranch!
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Thanks Pete,

I made it back to paintComponent .

I've written the below code this time :-

package GUI;


import java.awt.*;

import javax.swing.*;

class ImagePanel extends JPanel {

private Image image;

public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}

public ImagePanel(Image img) {
this.image= image;
Dimension size = new Dimension(image.getWidth(null), image.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}

public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
}


But in main() method, while creating an instance of ImagePanel(path), I'm writing path as "C:/Users/Pramod/Desktop/Beckham.jpg" , but its giving me nullpointer error.
I can't use '\' sign,
Please tell me how to write the correct path.

Thanks a lot.
Pramod
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

I have corrected this error :

public ImagePanel(Image img) {
this.image= image;

I made it this.image = img;

The code is getting compiled now, but still no pic...
what if I need to display the pic available on my desktop...

Thanks,
Pramod.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

Pramod,
While posting code, please UseCodeTags
You can modify your existing posts to incorporate them by clicking on the Edit button


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi Manish,

I clicked on Edit,
but didn't get any option to modify codetag.

how to do it ?

Thanks
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

Select the post.
Click edit. A text area will show up.
Select the code
Click the "Code" button on top of the text area.
Click preview to confirm proper code tags.
Click submit to save
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Thanks Manish.

Can you please help me on the code written...

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

What I immediately saw is that your paintComponent method is public where protected is recommended, and you forgot to start that method with "super.paintComponent(g);".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi Rob.

I had tried out super statement but nothing changed.
no effect of making the method private from public.

Thanks for your reply.

Pramod.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
pramod talekar wrote:Hi Rob.

I had tried out super statement but nothing changed.

You need to show the code of your attempt. Otherwise we'll have no idea what you're doing wrong.




What the heck is this?
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hi Pete,

This is the code I was talking about :

protected void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawImage(image, 0, 0, null);
}
}

Inclusion/Exclusion of the super statement doesn't affect the output.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with components/ widgets using JPanel
 
Similar Threads
learning GUI
Head First Java problem
How to put image on JPanel
Aspect ratio-preserving image scaling, drawing and infinite loops
Not able to compile this piece of code