• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with components/ widgets using JPanel

 
Ranch Hand
Posts: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Manish.

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

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 367
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic