Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: I tried to do something with this code but I can't get it

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I tried to do something with this code but I can't get it

    I try that a image to move with a cursors and i would to get to pass something like a focus that another image to move with a cursors next to move with a cursors the first image. Now I only have got to move simultaneus the two images but i would like to move independently each image. What i can do? Thanks This is the code that I get with the two images.

     import java.awt.Frame;
     import java.awt.Graphics;
     import java.awt.event.KeyEvent;
     import java.awt.event.KeyListener;
     import java.awt.event.WindowAdapter;
     import java.awt.event.WindowEvent;
     import java.net.MalformedURLException;
     import java.net.URL;
     import javax.swing.ImageIcon;
     
     public class Ejemplo extends Frame
      implements KeyListener
     {
       private static final long serialVersionUID = 1L;
       private String imageURL = "http://dreamers.com/byrne/46.jpg";
      private ImageIcon imagen;
      private ImageIcon imagen1;
      private int x;
       private int y;
       public int x1;
       public int y1;
     
      public static void main(String[] paramArrayOfString)
       {
        Ejemplo localEjemplo = new Ejemplo();
         localEjemplo.setVisible(true);
       }
     
       Ejemplo()
       {
         super("Ejemplo");
         setSize(500, 500);
     
         this.x = 200;
        this.y = 200;
         this.x1 = (this.x + 300);
         this.y1 = this.y;
        try
         {
           this.imagen = new ImageIcon(new URL(this.imageURL));
         } catch (MalformedURLException localMalformedURLException1) {
           localMalformedURLException1.printStackTrace();
        }
        try
         {
          this.imagen1 = new ImageIcon(new URL(this.imageURL));
         } catch (MalformedURLException localMalformedURLException2) {
           localMalformedURLException2.printStackTrace();
         }
     
         addKeyListener(this);
        addWindowListener(new WindowAdapter()
        {
           public void windowClosing(WindowEvent paramWindowEvent)
           {
             System.exit(0);
           }
         });
       }
     
       public void keyPressed(KeyEvent paramKeyEvent)
      {
         int i = 1;
         int j = 1;
         switch (paramKeyEvent.getKeyCode())
         {
         case 38:
           this.y -= 1;
     
           break;
        case 40:
           this.y += 1;
     
           break;
         case 37:
           this.x -= 1;
           break;
        case 39:
           this.x += 1;
           break;
         default:
          i = 0;
         }
         if (i != 0) {
           repaint();
         }
    public void paint(Graphics paramGraphics)
      {
        if (this.imagen != null) {
          paramGraphics.drawImage(this.imagen.getImage(), this.x, this.y, this.imagen.getImageObserver());
          paramGraphics.drawImage(this.imagen1.getImage(), this.x1, this.y1, this.imagen1.getImageObserver());
        }
        else {
          paramGraphics.drawString("Can't load image " + this.imageURL, this.x, this.y);
          paramGraphics.drawString("Can't load image " + this.imageURL, this.x1, this.y1);
        }
      }
     
      public void keyReleased(KeyEvent paramKeyEvent)
      {
      }
     
      public void keyTyped(KeyEvent paramKeyEvent)
      {
      }
    }
    Last edited by byrne; September 11th, 2011 at 03:18 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    Please edit your code and wrap it in code tags. Use the Go Advanced button below, select your code and press the #icon button above the input box.
    Or see this: BB Code List - Java Programming Forums


    Also please get rid of the empty comments at the beginning of each line.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    I just put the java code between tags anybody can help me to resolve the problem with my program in java? Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    The code you posted does not compile. It has many errors in it.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    This code works now

    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.swing.ImageIcon;
     
    public class Ejemplo extends Frame
      implements KeyListener
    {
      private static final long serialVersionUID = 1L;
      private String imageURL = "http://dreamers.com/byrne/46.jpg";
      private ImageIcon imagen;
      private ImageIcon imagen1;
      private int x;
      private int y;
      public int x1;
      public int y1;
     
      public static void main(String[] paramArrayOfString)
      {
        Ejemplo localEjemplo = new Ejemplo();
        localEjemplo.setVisible(true);
      }
     
      Ejemplo()
      {
        super("Ejemplo");
        setSize(500, 500);
     
        this.x = 200;
        this.y = 200;
        this.x1 = (this.x + 300);
        this.y1 = this.y;
        try
        {
          this.imagen = new ImageIcon(new URL(this.imageURL));
        } catch (MalformedURLException localMalformedURLException1) {
          localMalformedURLException1.printStackTrace();
        }
        try
        {
          this.imagen1 = new ImageIcon(new URL(this.imageURL));
        } catch (MalformedURLException localMalformedURLException2) {
          localMalformedURLException2.printStackTrace();
        }
     
        addKeyListener(this);
        addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent paramWindowEvent)
          {
            System.exit(0);
          }
        });
      }
     
      public void keyPressed(KeyEvent paramKeyEvent)
      {
        int i = 1;
        int j = 1;
        switch (paramKeyEvent.getKeyCode())
        {
        case 38:
          this.y -= 1;
     
          break;
        case 40:
          this.y += 1;
     
          break;
        case 37:
          this.x -= 1;
          break;
        case 39:
          this.x += 1;
          break;
        default:
          i = 0;
        }
        if (i != 0) {
          repaint();
        }
     
        switch (paramKeyEvent.getKeyCode())
        {
        case 38:
          this.y1 -= 1;
          break;
        case 40:
          this.y1 += 1;
          break;
        case 37:
          this.x1 -= 1;
          break;
        case 39:
          this.x1 += 1;
          break;
        default:
          j = 0;
        }
        if (j != 0)
          repaint();
      }
     
      public void paint(Graphics paramGraphics)
      {
        if (this.imagen != null) {
          paramGraphics.drawImage(this.imagen.getImage(), this.x, this.y, this.imagen.getImageObserver());
          paramGraphics.drawImage(this.imagen1.getImage(), this.x1, this.y1, this.imagen1.getImageObserver());
        }
        else {
          paramGraphics.drawString("Can't load image " + this.imageURL, this.x, this.y);
          paramGraphics.drawString("Can't load image " + this.imageURL, this.x1, this.y1);
        }
      }
     
      public void keyReleased(KeyEvent paramKeyEvent)
      {
      }
     
      public void keyTyped(KeyEvent paramKeyEvent)
      {
      }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    i would like to move independently each image.
    Look at how the one image is being moved.
    How do you want to make the other image move? What keys or cursor or other controls do you want to use to make it move?

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    I want to use the same controls the cursor first move first image with the cursor and next to use the same keys, the cursors to move the second image

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    Do you want to change what controls the image movement to be the mouse click or motion instead of the pressing of a key?
    Or do you want to use the same keys but have only one image move?
    How will the program know which image to move when you press on one of the keys?
    You need a way to say: move this image when a key is pressed.

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    Do you want to change what controls the image movement to be the mouse click or motion instead of the pressing of a key? I only want to use the cursors.
    Or do you want to use the same keys but have only one image move?I want to use the cursors two move two loads of the same images indepently they are the same images but in two loads how to see in the code.
    How will the program know which image to move when you press on one of the keys?I want something like to "pass the focus" from a image to another load for move the two loads indepently like Visual Basic with the controls.
    You need a way to say: move this image when a key is pressed. they are two images not only one
    I expect you understand me I want to move each image although the load are the same image in the frame Maybe using differents frames? when you load the code you must press the cursors and you will see the two loads of images moving.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    I only want to use the cursors.
    Not sure what you mean by cursors. I only have one cursor on my system.
    To cause movement with the cursor would require you to use mouse listeners.

    How would you "set the focus" on an image?
    One way would be to click on it. That would select the one to move when the cursor moves.

    Do you want the selected image to move for all mouse movements anywhere
    or only for those that are near the selected image?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    Look at this site. It has code that uses the cursor to move labels around.
    How to move or drag a Graphics Class Object [Like Select in Paint-Editor] - Java

  12. #12
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    I want to use "cursors keys" it helps to resolve my problem?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    The code you posted uses the Arrow keys.
    Have you tried changing it to use the "cursors keys"?
    For example keyCode of 40 is the KeyEvent.VK_DOWN keyCode.

    What are the VK_???? names for the "cursors keys"?

  14. #14
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    I want to use cursors keys not cursor it helps to understand what i want to say?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    What/where are "cursors keys"? I don't understand what you mean.

  16. #16
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    I want to say "cursor keys" not cursor, I want to use "cursor keys" to move the image are we near to resolve the problem?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    The current code uses the Arrow keys to move the images.
    Have you tried changing that code to use the "cursor keys"?

  18. #18
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    I want to use "the arrow keys" to move first a image and next, the second image , not "the cursors keys".Sorry my english is not very good. I use "the arrow keys" to move the two images simultaneously.

    I saw the page you suggested to see before and I deduced that I could use two Objects Jlabel to move the two images independently but I think to say in Internet that you can't move with arrow keys a Object Jlabel........is it possible to use a Image in a object Jlabel to move it with arrows keys?

    Thanks a lot

  19. #19
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    Could I use "internal frames" to show the two images?

    Thanks,

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    I want to use "the arrow keys" to move first a image and next, the second image
    What you need is a way to determine which image is to be moved when you press the arrow keys.
    Then when you have selected which image, only move that image when the arrow keys are pressed.
    Then if you change the selected image, the arrow keys will move the image that is now selected.

    One way would be to define a rectangle that surrounds the image and have a mouse listener that detects when the mouse has clicked within that rectangle and use that to select the image to move with the arrow keys.

  21. #21
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    I tried to do you said me but I couldn't get anything important.....
    My main problem is I tried to introduce a image in rectangule but I couldnīt get it because de method DrawRect() doesn't have a parameter "image" and then, it's the only way I know to introduce a image in a rectangle. Maybe I could try to create something like a class rectangle and I don't know what can I introduce in it to introduce a image inside this class rectangle?..... and do I need to create it like a extend a applet class? and I don't use the class applet in my program.

    I'm sorry because I think my knowledge of Java is minimal and Object programming, as well...I think there are things I don't understand. I try to learn something of java only to see things about this subject in internet and I think there are things to escape me, sorry
    Thanks a lot

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    a way to determine which image is to be moved
    The image is being drawn at a certain x,y location on the screen. Define a rectangle with its upper left corner at the same location that the image's x,y location. Have a class that associates that rectangle and its position with the image drawn at that location.
    When there is a mouse click on the screen, check if the mouse click is inside of the rectangle you have associated with an image. If it is, then "SELECT" that image.
    When you move an image, you need to also move its associated rectangle's location.

  23. #23
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    "Have a class that associates that rectangle and its position with the image drawn at that location" you said, Norm


    Why must I create a new class? I only use the class "Ejemplo.class" (in this class is in the code that I put up in this question in the forum) because I think it is the only way to execute the program I don't know how could I use two class and relationating these for execute the same program. Maybe what you want to say it's to use the constructor rectangle() inside the class "Ejemplo.class" to relationate the image and the rectangle but I tried to relationate them but I don't know if I could put inside the event Keypress and function paint inside the constructor rectangle () because its the only way I Know to relationate the image and rectangle() but I think these things donīt work in Object programming.
    Last edited by byrne; October 10th, 2011 at 09:29 AM.

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I tried to do something with this code but I can't get it

    The new class would contain a reference to the Rectangle that contains/surrounds the image as shown on the screen and a reference to the image.
    It would have methods that you could call to see if a mouse click is within its rectangle.
    When you move the image on the screen, you would also update the rectangle's x,y location so that it contains the image.

  25. #25
    Junior Member
    Join Date
    Aug 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I tried to do something with this code but I can't get it

    Hello,

    I Change and I define what I want with my program:

    Its very long to explain and I will say you smalls details.

    I have done in the program rectangles appear with random in columns of three and I leave a hole free and I get it if you want I can show you the program.

    Now I want put a image or a rectangle drawn inside a label, more later I want to move the image clicking with the mouse

    I dont know why I can't get the first part of the sentence before I think my code is correct what's is wrong?


    JLabel imagen = new JLabel();
    imagen.setIcon(new ImageIcon(getClass().getResource("cosa.jpg")));
    add(imagen);
    imagen.setText("tu texto");


    I put it in the constructer of my program I execute it and it appear the window in white and doesn't load the image

    I want to get several things and I can't get it firstly.

    Maybe after I will say you more things I want to get If you want but firstly I need it.

    Thanks!

Page 1 of 2 12 LastLast