Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with components/ widgets using JPanel

 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pramod,
While posting code, please UseCodeTags
You can modify your existing posts to incorporate them by clicking on the Edit button
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Manish,

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

how to do it ?

Thanks
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manish.

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

 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);".
 
pramod talekar
Ranch Hand
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 345
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic