-
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.
Code :
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)
{
}
}
-
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.
-
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
-
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.
-
Re: I tried to do something with this code but I can't get it
This code works now
Code :
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)
{
}
}
-
Re: I tried to do something with this code but I can't get it
Quote:
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?
-
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
-
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.
-
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.
-
Re: I tried to do something with this code but I can't get it
Quote:
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?
-
Re: I tried to do something with this code but I can't get it
-
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?
-
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"?
-
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?
-
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.
-
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?
-
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"?
-
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
-
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,
-
Re: I tried to do something with this code but I can't get it
Quote:
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.
-
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
-
Re: I tried to do something with this code but I can't get it
Quote:
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.
-
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.
-
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.
-
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!