Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  get image of panel  (Read 1941 times)
0 Members and 1 Guest are viewing this topic.
Offline civilwar

Senior Newbie





« Posted 2011-05-27 16:28:02 »

currently I have no way of getting the current image icon of a panel that is placed.

  Right now I am using getcomponent and figuring out its location to determine which image I used which isn't the best way any ideas?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
 for(lkm = 0; lkm < counter; lkm++)
                      {
                      ImagePanelList.get(lkm).addMouseListener(new MouseAdapter() {
                           @Override
                           public void mouseClicked(MouseEvent events) {
                              int letz;
                              char let;
                              String text;
                            text = events.getComponent().toString();
                            //gives me the panels location
                            System.out.println(text);
                            let = text.charAt(25);
                            //get first number of location
                            letz = Character.getNumericValue(let);
                            letz = letz - 1;
                            //minus one since my array list of images starts at 0
                              if(cardpicked[letz] == false && place[0] == -1)
                                {
                                   alPickedStringCardName.add(strCardName[letz]);
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-05-27 19:27:56 »

Did you know there is a Component.getBounds() method?

Offline civilwar

Senior Newbie





« Reply #2 - Posted 2011-05-28 18:37:48 »

ok I will use that.  another question I want to save this string how do I do that?

1  
2  
3  
4  
5  
6  
7  
 public ImagePanel(String img) {
       this(new ImageIcon(img).getImage());
}
 public String getImg() {
        return img.toString();
        }    
     }


doesnt work I want the imageicon string i suppose? how do I go about doing that
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-05-28 19:06:33 »

You want the String to become an image?
The String parameter in ImageIcon specifies the path to the image in your file system. The toString() method is available to all classes because Object is the superclass of all classes. There are no guarantees on what is returned in the String. Each class can have its own implementation of it. ImageIcon's toString() returns the path to the image. Image's toString() returns its name plus its hashcode.

Try actually reading the Javadocs.

Offline civilwar

Senior Newbie





« Reply #4 - Posted 2011-05-29 04:32:56 »

ya i understand the difference.  What I am trying to do is get the img path that was placed inside of a panel.  So I dont have to keep track of them.  I tried to do something with the path but I get null.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
class ImagePanel extends JPanel {

     private Image img;
     private String path2;
     
     public ImagePanel(String path) {
        Image image = new ImageIcon(path).getImage();
        path2 = path;
       //this(new ImageIcon(img).getImage());
   
     }
     public ImagePanel(Image img) {
       this.img = img;
     
       Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
       setPreferredSize(size);
       setMinimumSize(size);
       setMaximumSize(size);
       setSize(size);
       setLayout(null);
     }

     public void paintComponent(Graphics g) {
       g.drawImage(img, 0, 0, null);
     }
     
     public String getImg() {
        return path2;
        }

   }

Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-05-29 04:50:46 »

Haha you never actually assign img = image Wink
Try doing "this(image)" inside your first constructor.
Never mind, this(...) needs to be your first call.
do this:
1  
2  
3  
4  
public ImagePanel(String path) {
    this(new ImageIcon(path).getImage());
    path2 = path;
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!