12 Replies Latest reply: Jul 2, 2008 11:40 AM by 807589 RSS

    need help drawing image on a background using coordinates from a props file

    807589
      What I need to do is to draw a small image on a black background using coordinates from a properties file...this is what I came up with but I get errors with compiling...im new to this kinda thing.
      public class Drawstar extends Component {
        public static void main(String args[]) {
          Drawstar props = new Drawstar();
          props.draw();
          JFrame f = new JFrame("Stars Map");
                  
              f.addWindowListener(new WindowAdapter(){
                      public void windowClosing(WindowEvent e) {
                          System.exit(0);
                      }
                  });
      
              f.add(new Drawstar());
              f.pack();
              f.setVisible(true);
          }
      Image img;
      
        public void draw() {
          try{
            Graphics g;
            Properties p = new Properties();
            p.load(new FileInputStream("userpos.props"));
            g.drawImage(img, p.getproperty("x1"), p.getproperty("y1"), null);
            g.drawImage(img, p.getproperty("x2"), p.getproperty("y2"), null);
            g.drawImage(img, p.getproperty("x3"), p.getproperty("y3"), null); 
            g.drawImage(img, p.getproperty("x4"), p.getproperty("y4"), null);
            g.drawImage(img, p.getproperty("x5"), p.getproperty("y5"), null);
            g.drawImage(img, p.getproperty("x6"), p.getproperty("y6"), null);
            g.drawImage(img, p.getproperty("x7"), p.getproperty("y7"), null);
            g.drawImage(img, p.getproperty("x8"), p.getproperty("y8"), null);
            g.drawImage(img, p.getproperty("x9"), p.getproperty("y9"), null);
            g.drawImage(img, p.getproperty("x10"), p.getproperty("y10"), null);
            } catch (IOException e) {
            }
          }
        
        public Drawstar() {
             try {
                 img = ImageIO.read(new File("star.jpg"));
             } catch (IOException e) {
             }
          }   
      }  
        • 1. Re: need help drawing image on a background using coordinates from a props file
          807589
          Welcome to Suns forums! When you need help with a compiler error, post the exact error message and indicate on which line it occurs.
          • 2. Re: need help drawing image on a background using coordinates from a props
            807589
            oh ok...well it gives me an error saying it cannot find the symbol "getproperty" method...on all the "getproperty" methods in the g.drawimage function
            • 3. Re: need help drawing image on a background using coordinates from a props
              807589
              wolvz38 wrote:
              oh ok...well it gives me an error saying it cannot find the symbol "getproperty" method...on all the "getproperty" methods in the g.drawimage function
              Java is case sensitive. Are you sure there is a method whose exact name is "getproperty"?

              You will want to bookmark this: [http://java.sun.com/javase/6/docs/api/]
              • 4. Re: need help drawing image on a background using coordinates from a props
                807589
                its for properties files or ".props" which is sort of like windows "ini" files...and properties function is from the java.util.*
                • 5. Re: need help drawing image on a background using coordinates from a props
                  807589
                  wolvz38 wrote:
                  its for properties files or ".props" which is sort of like windows "ini" files...and properties function is from the java.util.*
                  Have you checked the API for Properties? As the error message indicates, it has no method named exactly "getproperty":

                  [http://java.sun.com/javase/6/docs/api/java/util/Properties.html]
                  • 6. Re: need help drawing image on a background using coordinates from a props
                    807589
                    there is a method called "getproperty("string")"...look at the method summary table for that link u gave...its the first one...and ill give u the link where i found the method originally...

                    [http://www.rgagnon.com/javadetails/java-0024.html]
                    • 7. Re: need help drawing image on a background using coordinates from a props
                      DrClap
                      No, the method's name is "getProperty", not "getproperty". And the page you linked to also uses "getProperty" and not "getproperty". There's a difference. And it's not an insignificant difference.
                      • 8. Re: need help drawing image on a background using coordinates from a props
                        807589
                        oh i was not sure if it was case sensitive or not...im very sorry ill change my code and see what happens...thanks
                        • 9. Re: need help drawing image on a background using coordinates from a props
                          807589
                          wolvz38 wrote:
                          there is a method called "getproperty("string")"...look at the method summary table for that link u gave...its the first one...and ill give u the link where i found the method originally...
                          Please read and listen to what is posted above. Again: Java is cAsE sEnSitive. Do you understand now where your error is?

                          edit: too late

                          Edited by: Encephalopathic on Jul 2, 2008 9:40 AM
                          • 10. Re: need help drawing image on a background using coordinates from a props
                            807589
                            wolvz38 wrote:
                            oh i was not sure if it was case sensitive or not...im very sorry ill change my code and see what happens...thanks
                            Note that I wrote way back in reply 3 that Java was case sensitive.

                            Suggestion: don't try to run before you can walk. There are more than a few errors in your code. I suggest you learn the basics of the language before attempting to write Swing code.
                            • 11. Re: need help drawing image on a background using coordinates from a props
                              807589
                              okay...no need to be rude....its not like its my first time programming in java...its just been awhile since i last did some java coding....but thanks anyways i forgot about the case sensitive part...just kinda went over my head
                              • 12. Re: need help drawing image on a background using coordinates from a props
                                807589
                                wolvz38 wrote:
                                okay...no need to be rude....its not like its my first time programming in java...its just been awhile since i last did some java coding....but thanks anyways i forgot about the case sensitive part...just kinda went over my head
                                If you have programmed in Java before then you should know that you should separate these two things:

                                * reading from a properties file
                                * a component that draws a background

                                Here's a demo. Hint: note there are five dukes:
                                import java.awt.*;
                                import java.awt.image.*;
                                import java.io.*;
                                import java.net.*;
                                import javax.imageio.*;
                                import javax.swing.*;
                                
                                public class Dukes extends JComponent {
                                    private static final long serialVersionUID = 0;
                                    private BufferedImage image;
                                    private Point[] locations;
                                
                                    public Dukes(BufferedImage image, Point[] locations) {
                                        this.image = image;
                                        this.locations = locations;
                                    }
                                
                                    @Override protected void paintComponent(Graphics g) {
                                        super.paintComponent(g);
                                        for(Point p : locations) {
                                            g.drawImage(image, p.x, p.y, null);
                                        }
                                    }
                                
                                    public static void main(String[] args) throws IOException {
                                        URL url = new URL("http://forum.java.sun.com/im/duke.gif");
                                        BufferedImage image = ImageIO.read(url);
                                        Point[] locations = {
                                            new Point(10, 20),
                                            new Point(40, 30),
                                            new Point(70, 10),
                                            new Point(30, 60),
                                            new Point(80, 80)
                                        };
                                        launch(image, locations);
                                    }
                                
                                    static void launch(final BufferedImage image, final Point[] locations) {
                                        EventQueue.invokeLater(new Runnable(){
                                            public void run() {
                                                Dukes component = new Dukes(image, locations);
                                                component.setPreferredSize(new Dimension(100,100));
                                
                                                JFrame f = new JFrame();
                                                f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                                                f.getContentPane().add(component);
                                                f.pack();
                                                f.setLocationRelativeTo(null);
                                                f.setVisible(true);
                                            }
                                        });
                                    }
                                }
                                Reading coordinates from a property file is left as an exercise.