Swing/AWT/SWT :: Snakes Stopped Moving When Tried Hitting Keys

Mar 28, 2014

I've visited this great site been following this java tutorial on making the snake game, and as I followed along at added the second player. Right up to the point I got into the resetting if I hit borders or myself or the other player, the snakes stopped moving when i tried hitting keys...problem started up to the point where I added snake2 in the GenerateDefaultSnake method

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;

[code]....

View Replies


ADVERTISEMENT

How To Make Moving Character By Alternating Arrow Keys

Aug 3, 2014

I've started creating a simple game in Java, 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.

Play Mini Sports Challenge game online - Y8.COM

View Replies View Related

Swing/AWT/SWT :: Printing JTable - Java (TM) Platform SE Binary Has Stopped Working

Nov 19, 2014

I am getting the following message when trying to print a JTable.

"Java(TM) platform SE binary has stopped working"

Here is the code:

try {
MessageFormat headerFormat = new MessageFormat("Page {0}");
MessageFormat footerFormat = new MessageFormat("- {0} -");
table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
} catch (java.awt.print.PrinterException pe) {
System.err.println("Error printing: " + pe.getMessage());
}

View Replies View Related

Swing/AWT/SWT :: Moving Image Left Or Right

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

JSP / JSTL :: Session Tracking Before Hitting Any Page

Nov 2, 2012

I want to validate the user session everyone when ever the request comes for any jsp page. I am able to validate the user in a filter for the first time. But i am confused what would happen when the request comes for other pages...how will i be able to get the same session from the server?

View Replies View Related

Swing/AWT/SWT :: Continuous Flash When Moving Mouse Next To Text Of Button

Jan 27, 2015

Just run the following code and move the mouse next to the text of the button and you will see exactly what I am talking about. Any way to prevent this continuous flash?

import java.awt.Dimension;
import javax.swing.JButton;
public class GUI {
public static void main (String[] args) {
javax.swing.JFrame frame = new javax.swing.JFrame();
javax.swing.JPanel panel = new javax.swing.JPanel();

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Code Prevent Cursor From Moving When Inside A Cell Of JTable

Jun 24, 2014

Tried creating a simple sample but all works as expected.

The following code prevents the cursor from moving when inside a cell of a JTable.

public void keyPressed(KeyEvent e) {
if ( (e.getKeyCode() == KeyEvent.VK_KP_LEFT) || (e.getKeyCode() == KeyEvent.VK_KP_RIGHT)
|| (e.getKeyCode() == KeyEvent.VK_RIGHT) || (e.getKeyCode() == KeyEvent.VK_LEFT) )
{
//Do nothing
e.consume();
}
}
});

When editing a cell, the existing code would use the right/left cursor keys to move from cell to cell as opposed to from character to character when editing a cell. I planned to override the functionality by tossing in the above code as a test to see if it stops the functionality before I override it.

After placing in the above code, the above functionality no longer occurs, but now the cursor moves within the cell as I wanted which is to move from character to character instead of cell to cell. Its great it works, but it really shouldn't. Essentially the default behavior has been restored when it should have really disabled the left/right keys.

I assume there is some underlying class someplace that is doing something to affect the behavior. Since a sample can't be provided I am wondering in what scenarios would the e.consume() restore default functionality?

View Replies View Related

Allowing User To Determine When A Program Is Stopped?

Nov 8, 2014

I was tasked with building a program that counts vowels in a phrase or word. I have this working properly, but the problem arises in the next situation. Here is my code:
 
Scanner userInput = new Scanner(System.in);
int userDecision;
String Choice;
String Phrase;
int vowelCount = 0;
System.out.println(" This program will count the number of vowels in it.

[Code] ....

As you can see, I used a 'for' statement to declare how long the program should run. However, I need the user to be able to say when it ends. For example, at the end of the program it must say "There are ______ number of vowels in your word. Please press '1' to run the program again, or any other number to exit."

I understand a do-while loop is needed here, but every time I try and create an adequate loop with no logic errors, I either break my existing code, or the program does not work correctly.

Basically, my question is, what is the best way to perform this task? Is a do-while loop necessary, or is there a better way?

View Replies View Related

Insert Break Line To Text Message On Hitting Enter By Taking Its ASCII Value

Mar 2, 2014

I need to insert a break line to a text message on hitting enter by taking its ascii value i.e 10.I have used node.insert commands.I have tried using node.insertAttribute and node.insertChars but is not working ....

View Replies View Related

Java Program Which Would Display A Message Dialog Box Stopped Working

Mar 16, 2014

I have a java program which would display a message dialog box. The problem is it would stopped working before even displaying the message dialog box. This is my code.

import javax.swing.JOptionPane;
public class HelloDialog {
public static void main(String [] args) {
JOptionPane.showMessageDialog(null, "Hello World!");
}
}

There is a pop-up window which would say "Java(TM) Platform SE binary has stopped working." I am using textpad. What should I do with this?

View Replies View Related

HashMap / How To Get Size Of Value And NOT The Keys

Apr 5, 2014

I am asked in my assignment to make a program that accepts a text file as an example a novel and i have to sort each word as a PERSON or ORGANIZATION or LOCATION or O as in Other , example :

Microsoft/ORGANIZATION ,/O Nelly/PERSON !/O '/O

Now we notice that microsoft is and organitzation and "," is Other and Nelly is a person's name and so on ..

Now I am asked to return the numbers of tags in the text which is 4 in our case because we have (ORGANIZATION,PERSON,LOCATION,OTHER)

My question here is my logic true ? And since i made a map of String,String ; Is there any way that i can get the size of the values in our case the values are the organization etc.. ?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;

[code].....

View Replies View Related

Getting Keys From Values In Hashmap

Feb 9, 2014

I have one doubt.In HashMap if keys contains 1,2,3,4 and values are a,b,c,d we can get values using get(key) method like 1 will A,2 will return B and so on. Can we get the keys from values like A will get 1 and also if in key if there is a String like 1,2,3,Z and value is A,B,C,7 Z should get me 7. Here I am not using any generics.

View Replies View Related

Going Through All The Keys On Keyboard One At A Time

Jul 14, 2014

I am writing a code that tries to figure out the users password by going through every possible key (brute force). Although I think its going to work, it looks EXTREMELY inefficient to me, since its just a huge switch statement for each character -- 94 total, including the shift values. Is there a built in method in the JAVA API that goes through every key or something?

Here is my code for that segment:

public char[] HackingPassword(){
char[] passChars = this.password.toCharArray();//convert the password to char[]
char[] hacking = new char[passChars.length];//make the hacking variable same size as users password
int nextCharValue = 0;//this is used to cycle through the keyboard
//check each letter of hacking to match with password
for(int i = 0; i < passChars.length; i++){

[Code] .....

View Replies View Related

JPA Primary Key From Two Foreign Keys

Oct 22, 2010

I am new with java, eclipse, jpa(eclipselink), postgresql, and trying to make a web application. I have two tables:

bids: id, quantity, price

trades: bidid, askid, quantity, price

bidid and askid columns are foreign keys from bids table (id), and they are the primary key for the trades table.

I created the Entities from the Tables (Bid and Trade class) with eclipse and it generated a TradePK class for the primary key.

Trade.java:

@Entity
@Table(name="trades")
public class Trade implements Serializable {
@EmbeddedId
private TradePK id;

[Code] ....

I understand that this is necessary because the primary key is from two column, but as soon I want to persist a Trade back to the database Eclipselink call the column names twice:

INSERT INTO trades (price, quantity, datetime, BIDID, ASKID, bidid, askid) VALUES (?, ?, ?, ?, ?, ?, ?)

So postgres give me back an exception: ERROR: column "bidid" specified more than once

And I don't know why. According to the JPA docs I didn't found any mistake:

Introduction to EclipseLink JPA (ELUG) - Eclipsepedia

Or I just can't see it... or is there a better doc which explains it better?

View Replies View Related

How To Print Out All Keys Currently Stored In A HashMap

Jul 4, 2014

I want to write a method to print the all the names of a phone book. phoneBook is a HashMap<String, String> , that has names as keys and phone numbers as values.

Reading the documentation for both HashMap and Set, I have more or less an idea of how it could be done but I cant put it on code..
 
public String getName()
{
  String[] keys = phoneBook.keySet().toArray();
return keys[0];
  }

This is wrong. It doesnt compile saying incompatible types. I was thinking first to manage to succeed to get a key from my phone book and then I change it to print all the keys.

View Replies View Related

JSF :: SQL Database - CRUD With Foreign Keys

Aug 12, 2014

I am trying to work with a SQL database and some JSF pages that were created with/for CRUD. The data comes up just fine but all my foreign keys show the primary key for the key not the data. So instead of having the actual Zone I get the number (my primary key) that matches the Zone.

example:
4717A Cool ReceptionTitle: Gatherer of Cool Companycom.vancowboy.lotor_db.LotroZones[ id=11 ]

The last item should read "All" not "com.vancowboy.lotor_db.LotroZones[ id=11 ]"

This was straight from a tutorial so I can learn this but the FK has got me baffled...

View Replies View Related

HashMap Keys To String Array

Dec 21, 2014

I have the following hashMap declared:

private HashMap<String, Car> cars = new HashMap<String, Car>();

How can i get an array of String filled with hashMap keys?

View Replies View Related

Hashmaps - Selecting Keys / Values

Apr 22, 2014

I'm learning Java using BlueJ, I have made a class that has a HashMap of (String, String) that contains an the make of a car and the model.

I want a method to return a collection of all the keys that satisfy a condition, like if someone wants to find what make a certain model is. I know it requires a loop, just not too sure how to write the loop to satisfy the condition.

View Replies View Related

How To Identify Up Down Left And Right Keys With Keylistener

Apr 9, 2014

I Want to identify up, down, left and right keys with the keylistener() WHATE NAME DO I IDENTIFY IT WITH

View Replies View Related

HashMap - Return Multiple Keys -OR- Values

Dec 13, 2014

Here is my HashMap and a method for listing all the keys in it

HashMap<String, String> exampleOne = new HashMap<String, String>();
public void allKeys() {
int i;
i =0;
for (String name: exampleOne.keySet())

[Code]....

Now I want to return all values that associated with one key. How do I do this? Or is it possible to other way round? I mean return All keys associated with a value?

View Replies View Related

To Decrypt Data - File Containing Keys Is Not Being Read

Aug 27, 2014

I am developing a java code using netbeans for encryption decryption by RSA algorithm. Swings and file handling play a major role in these code. My encryption code is working nicely but the code for decryption is not since keys file is not being read. That is why variable mod and pri are getting null values and the following error stack is coming. I know where the problem is somewhere in void readkeys() function but cannot solve it.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.math.BigInteger.modPow(BigInteger.java:1579)
at rsakeydecryption1.RsaKeyDecryption.decryption(RsaKeyDecryption1.java:167)
at rsakeydecryption1.RsaKeyDecryption.read_output(RsaKeyDecryption1.java:294)
at rsakeydecryption1.RsaKeyDecryption.actionPerformed(RsaKeyDecryption1.java:330)

[Code] .....

My code for decryption is

package rsakeydecryption1;
import java.math.BigInteger;
import java.util.Random;
import java.io.*;
import javax.swing.*;

[Code] ....

View Replies View Related

Binary Search Trees Theory - Sequence Of Keys?

Mar 15, 2014

Suppose that a certain BST has keys that are integers between 1 and 10, and we search for 5. Which sequence below cannot be the sequence of keys examined?

(a) 10,9,8,7,6,5
(b) 4,10, 8, 6, 5
(c)1,10,2,9,3,8,4,7,6,5
(d) 2,7,3,8,4,5
(e)1,2,10,4,8,5

I've noticed many differences, such as there is only one with an odd number of keys, and one has all integers from 1-10 inside of it, but I can't find any real reason that you wouldn't be able to search it?

View Replies View Related

Deep Clone Of Entities - Set Primary Keys To Null

Nov 21, 2014

I need to get a clone of a fairly deep object tree. All objects are entities and I need to set the primary keys to null. The root object:

public class Grundentscheidung implements Cloneable {
@OneToMany(cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name = "gesamtentscheidung_dstNr"),
@JoinColumn(name = "gesamtentscheidung_id")

[Code] .....

When I call

Grundentscheidung grundentscheidungClone = grundentscheidung.clone();

grundentscheidungClone contains the whole object tree with all dependencies, but the Tatbestand objects have their primary keys. When I use the debugger I see that Tatbestand.clone() is never called.

Is my code faulty? I would like to avoid to write a large method which sets all primary keys on the object tree to null.

View Replies View Related

Compiler Warnings When Using SortedSet To Process HashMap Keys In Order

Feb 16, 2015

My objective here is to process a HashMap's key's in order. I found SortedSet as a way to do it.

The HashMap is like this:

nobelPrizeWinners = new HashMap<String, PrizeWinner[]>();
// 2009:
nobelPrizeWinners.put(new String ("2009 Physics"), new PrizeWinner[] {new PrizeWinner("Charles K.", "Kao"), new PrizeWinner("Willard S.", "Boyle"), new PrizeWinner("George S.", "Smith")});

[Code] ....

This is the method I am trying to write

public void displayAllYearsAndWinners_2()
{
// Creation of the SortedSet
SortedSet sortedSet = new TreeSet();

[Code] ....

However, the compiler gives me a warning of NobelPrizeWinners.java uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details.

As I said, my objective here is to process them in order. If this compiler warning cannot be resolved, I am open to other methods of accomplishing my objective.

View Replies View Related

Java 2D Game - Overcoming Max Of Three Keys At A Time Limitation Of Keyboard

Jan 7, 2014

I'm working on a Java 2D game which requires a max of six keys be held down at the same time.

The game is for two players on the same keyboard, playing simultaneously.

However, all three computers I ran the program on only allow a max of three keys held at a time. They all have trouble with reacting to more than three keys being held.

It seems that pressing a new key after three are already held, either cancels some other key-holding or is ignored.

I've been told that this is a hardware issue. Most keyboards can't handle more than three keys held at a time. But a lot of games do require this, and they do not require special gaming-keyboards to run on my computer without problems.

So there has to be a solution that will make the game playable on any standard keyboard.

(I use Key Bindings).

The game's controls:

Player 1

- Rotate sprite and set angle of movement: LEFT arrow

- Rotate sprite and set angle of movement: RIGHT arrow

- Move forward: UP arrow

- Shoot missile: ENTER key

Player 2

- Rotate sprite and set angle of movement: 'A' key

- Rotate sprite and set angle of movement: 'D' key

- Move forward: 'W' key

- Shoot missile: 'T' key

Relevant code:

The Key Bindings part:

Java Code:

// An action for every key-press.
// Each action sets a flag indicating the key is pressed.
leftAction = new AbstractAction(){
public void actionPerformed(ActionEvent e){
keysPressed1[0] = true;

[Code] .....

This is mostly how reacting to key-presses and key-releases works in my program. When a key is pressed or released, a flag is set. The Board class reades the flags every game-loop cycle and reacts accordingly.

As I said, the program doesn't react correctly to more than 3 keys held at a time, probably because of the keyboard. Is there a way to code a solution?

View Replies View Related

Moving A Map In A Game

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







Copyrights 2005-15 www.BigResource.com, All rights reserved