Moving Object In Java?
Feb 21, 2014
Basically I have a program that paints a simple object image. There are 5 of these objects altogether and different classes and methods have been set up to do this and this works correctly. The program currently also asks the user if they would like to change the colour of this object shape, and which shape they would like to change (user inputs a number 1-5) then the program will change the colour of this shape.
But the new task is for the user to input a number (1-5) to which shape they would like to MOVE position of. Hhow this would be done? Would there need to be different methods for changePositionX, changePositionY? The user needs to input the X and Y co ordinates and then this will move the selected shape to its NEW position in the current JFRAME.
View Replies
ADVERTISEMENT
Jul 10, 2014
I just have one problem with a "car" program in java. The objective is to move the car in the direction it is facing. Moving forward in the normal position (when the angle is zero) means moving in the x-axis. Choosing to turn the car function (for example: 90 ) and then using the forward function should move up or down the car in the y-axis. However the y-axis is not working.
Car2 Demo.rar
JPGMoveforwardfunction.jpg
View Replies
View Related
May 7, 2014
I have made a code that should allowed me to move an object in a frame, using the arrow keys. But I don't know why, is not working.
package joc;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
[Code] ....
View Replies
View Related
Jan 13, 2015
I have a class that works only when in the default package. If I move the class to a package I receive UnsatisfiedLinkError.
This is the class in the default package(this works fine):
[code]
public class CsharpConsumer {
private native int reigsterAssemblyHandler(String str);
public CsharpConsumer() {
String dir = System.getProperty("user.dir");
System.load(dir+"dllscardJNICsharpBridge.dll");
int reigsterAssemblyHandler = reigsterAssemblyHandler(dir+"dllscard");
}
}
[/code]
If at the top of this class I add "package DLLUtils;"
the error is "Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError:
DLLUtils.CsharpConsumer.reigsterAssemblyHandler(Ljava/lang/String;)I"
I tried a lot of things but none solved the problem. I must move the class to o package because I cant "import CsharpConsumer;" from default package.
View Replies
View Related
Jan 18, 2014
I have been following a 2d side scroller tutorial in java and have got the basics working, I changed the code a bit to make it so that the tiles and background are always moving, this worked as i intended.Within the character class:
if (speedX < 0) {
centerX += speedX;
}
if (speedX == 0 || speedX < 0) {
bg1.setSpeedX(0);
bg2.setSpeedX(0);
[code]....
I wanted a way to make it so that if the character moves left, the tiles + background move slower. I am having issues with variable scope and setters/getters. Within the main game class, I have the code:
case
KeyEvent.VK_LEFT:
character.moveLeft();
character.setMovingLeft(true);
break;
How can i include an if statement within the tile class that reads whether the character is moving left (determined from a keyPressed event in the main game class) and adjusts the speed of the tiles accordingly?I want to write something to the effect of:
if (setMovingLeft = true){
speedX = bg.getSpeedX() * 3;
tileX = tileX + speedX - 1; << changed from - 4
View Replies
View Related
Apr 24, 2015
try to make a game in this code i am trying to make the basics i have made a mouse it should move the game code is
import java.applet.Applet;
import java.awt.*;
public class Game extends gameLoop {
public void init()
{
setSize( 854 , 480);
Thread th = new Thread (this);
[code]...
View Replies
View Related
Aug 4, 2014
I'm learning about the printf command, and when I have it, it is not letting me ad an input. Here is my quick little program:
import java.util.Scanner;
public class TestingPrintF {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
[Code] ....
The first section works good, but when i move down to the second part it just automatically finishes without letting me enter a phrase.
View Replies
View Related
Mar 19, 2015
Okay, gotta be able to move the image when a key is pressed. When I test it, the image moves, but leaves the old image behind.
package Game;
import java.awt.Component;
import javax.swing.JOptionPane;
public class Game
{
private Grid grid; //holds the grid used to store and display images
private int userRow; //keep track of which row the user-controlled image appears in
private int msElapsed; //keep track of the total amount of milliseconds that have elapsed since the start of the game
[Code] ....
Here is what I was given on instructions:
Complete the handleKeyPress method. Write the following line of code in the handleKeyPress method to check for a key press:
int key = grid.checkLastKeyPressed();
If the user pressed the up arrow (key == 38), move the user's image up one row (unless the user is already in the top row). Likewise, if the user pressed the down arrow (key == 40), move the user's image down one row (unless the user is already in the bottom row). Either way, make sure to update userRow.
Movement of the image can be accomplished by changing the value of userRow. Study how the initial image is displayed in the game constructor, then
Set the current image location to null
Update userRow to the new location and,
Set the image in the new value of userRow.
You should be able to move the user image up and down, and you should be prevented from moving the image off the screen.
View Replies
View Related
Apr 1, 2014
I am writing code for a simple java game. The problem I am running into is that I have a moving icon (J button) that is controlled using the keyboard (arrows) and I also have stationary icons (J button). Is there a way to keep the moving icon from passing over the stationary icon? I would like the stationary icon to act like a wall.
View Replies
View Related
May 24, 2015
I am trying to make a simple platformer game in Java. When I try to move the character I made it only moves over then immediately back
import java.awt.event.KeyEvent;
public class UCC
{
private int xLoc;
private int yLoc;
private int xSpeed;
private int ySpeed;
private double gravity;
private int size;
[code]....
View Replies
View Related
Jul 22, 2014
I've been at this for a while and I would like to make the Camera in my game follow the player and I know the way to do this would be to move the map and not the player. How would I do this if my map is rendered from a text file using for loops. Here is my code for the map.
package Game;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Map {
[Code] .....
View Replies
View Related
Feb 3, 2014
I'm trying to understand and learn Java for GUI but I'm stacked around a project. I had done some other "test app" but on this, I can't figure out what I had done. I can't move my circle around the panel.... Where I had made some errors?
Main class
package main;
// imports
import javax.swing.JFrame;
import java.awt.Dimension;
public class Main {
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;
[Code] .....
View Replies
View Related
Sep 7, 2014
Why does my program write that i have problem in Timer?
public class Ball extends JPanel implements ActionListener {
Timer timer = new Timer (3, this);
int x = 0, y = 0, aX = 2, aY = 2;
public void PaintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
[Code] ....
View Replies
View Related
Jan 17, 2014
I was following a tutorial on how to make a game when i suddenly got an error concerning moving tiles. i get this error when i tried to use a and d to move:
Exception in thread "Display" java.lang.ArrayIndexOutOfBoundsException: 48600
at com.cherno.graphics.Screen.render(Screen.java:44)
at com.cherno.Game.render(Game.java:124)
at com.cherno.Game.run(Game.java:87)
at java.lang.Thread.run(Unknown Source)
I do not get the error while using w or s but the tiles are still not moving when pressing them.
I have three classes at work here.
The main, Game.java;
package com.cherno;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
[Code] .....
View Replies
View Related
Mar 11, 2014
I can't find any resource on the net about a simple Java code just to move a gif image left or right. I've already accomplished the up, down, and center and they're working fine, thus, I'm still struggling with moving the image left or right. Here's the code.
public class MoveIt extends Applet implements ActionListener
{
private Image cup;
private Panel keypad;
public int top = 10;
public int left = 10;
[Code] ....
I remember in Visual BASIC it's easily achieved by NameOfImage.left = NameOfImage.left - 10 to move left and NameOfImage.left = NameOfImage.left + 10.
View Replies
View Related
Dec 8, 2014
I'm currently taking a computer science class but the class is moving extremely slow and I'm not learning much, so I've started to program on my own. The most recent project I've done is a tic tac toe game, and I'm looking for something else to do now. I can do with an increased difficulty from this project, but not extremely difficult.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
[code]...
View Replies
View Related
Jun 6, 2014
So I've been working on a music player in android studio and I've created two java classes. One for Main Start Screen(StartScreen.Java) and one for the main player (player.Java). I have an onClicl event associated with a button as shown below.
public void onclick(View view) {
Intent detailIntent = new Intent(this, main_player.class);
startActivity(detailIntent);
}
The code for the second Java Class is as below. There is nothing in the java class as for now.
package com.example.musicplayer.app;
import android.app.Activity;
import android.os.Bundle;
public class main_player extends Activity {
protected void onCreate(Bundle savedInstanceState) {
[Code] .....
Where is the actual mistake because the app works fine until i click the button that is suppose to take me to the main player screen?
View Replies
View Related
Jul 24, 2009
I am just trying to move a rectangle in a diagonal line. When the applet pops up, the rectangle is there but doesn't move. If I drag the sides of the applet to make it bigger or smaller the rectangle will redraw itself and move, but I want it to move without touching the window.
Java Code:
package javaapplication3;
import java.applet.Applet;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
[Code] .....
Why does it only repaint when I resize the window?
View Replies
View Related
May 14, 2014
I am trying to only allow the user to input numbers. But I need to enter a number twice before it moves to the next line statement and also skips a line when i enter th number a second time.
How can I go around fixing this.
My code for this is
case 1:
do{
Event event = new Event();
out.println("Please Enter the name.");
event.setEvent(input.next());
input.nextLine();
[Code]...
View Replies
View Related
Aug 8, 2014
I've started creating a simple game in Java, but I'm still a beginner, so now I'm stuck. I wanted to make moving character by fast alternating arrow keys, but I don't know how. I can make a boolean variable, which disables moving by pressing the same key again, but the player can just hold the key and character is still moving. I thought about changing position of character when the key is pressed, instead of increasing speed, but then the movement wouldn't be smooth. I think this may sound incomprehensible, so I will add a link to game like an example of what I mean.
[URL] ....
View Replies
View Related
Apr 9, 2014
I am try to move circle.Below is my code...
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;
[Code] .....
And I have error
run:
Exception in thread "main" java.lang.NullPointerException
at swingdesing.Game.buffer(Game.java:32)
at swingdesing.Game.moveBall(Game.java:27)
at swingdesing.Game.main(Game.java:51)
BUILD SUCCESSFUL (total time: 9 seconds)
how to overcome this?
View Replies
View Related
Apr 28, 2014
Let's say we have a matrics of coordinates:
(0,0) (0,10) (0,20)
(10,0) (10,10) (10,20)
(20,0) (20,10) (20,20)
And we have a class that draws a rectangle of 10 pixel height and 10 pixel width at 0,0 coordinates.
And we want to move with the arrows the rectangle but not to go off the frame.
The question is:
How can I do that if I draw another rectangle, it knows where is the other object and they not collapse. And so on, they move, but not collapse, just touch and stop.
View Replies
View Related
Apr 8, 2014
How can I find out the simple moving average by just using the numbers stored in an array?
View Replies
View Related
Mar 22, 2014
I have a text file that has contents as follows:
testing1,true,0,0.0,0,0.0,4.0,0.0,0.0,0.0,0.0
test2,true,0,0.0,0,0.0,4.0,0.0,0.0,0.0,0.0
I am trying to read only the first item in each list using a comma as a delimiter using the following code.
public String[] pList() {
ArrayList<String> names = new ArrayList<String>();
String word;
try {
String path = P_FOLDER + P_FILE;
[Code]....
View Replies
View Related
Apr 1, 2014
I've been trying to deploy my desktop application as an executable jar file but I'm facing the following problem: Whenever I move the jar file from its original build location, my application becomes unable to write to its external files. I initially thought the problem was path related; however, when moved the app can still load data from these files.
The solution is probably simple but for some reasons I've yet to find it .
The app consists of a jar file and a folder named "files" containing text files (such as library.data) to which serialized objects are written.
I wouldn't be surprise if the problem has to do with the xml build file, but all modification I've tried so far have failed. If it is likely that it is the problem, I'll be happy to post a copy of it upon request.
public static void saveLibraryToFile() {
File file = new File(System.getProperty("user.dir")+"fileslibrary.data");
if (file != null && file.exists()) {
library = new Library(sortedAccounts, accountCategoriesMap, accountCategoriesList);
[Code] ....
A mostly complete set of the relevant codes I'm using, including the full Ant xml file, can be found at : [URL] .....
View Replies
View Related
Mar 28, 2014
Create an equals method that takes an object reference and returns true if the given object equals this object.
Hint: You'll need 'instanceof' and cast to a (Geocache)
So far I have:
public boolean equals(Object O){
if(O instanceof Geocache){
Geocache j=(Geocache) O;
if (this.equals(j)) //I know this is wrong... but I can't figure it out
return true;
}
else return false;
}
I think I have it correct up to the casting but I don't understand what I'm suppose to do with the this.equals(). Also I'm getting an error that I'm not returning a boolean... I get this all the time in other problems. I don't get why since I have to instances of returning booleans in this. "returns true if the given object equals this object" makes no sense to me. I assume the given object, in my case, is 'O'. What is 'this' object referring to?
View Replies
View Related