This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!

Paul Carter

Ranch Hand
+ Follow
since Sep 20, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Paul Carter

Not sure if this is the right forum but I'm wanting to create a small Java app to change voices in realtime i.e. take input from the mic and push it through the speaker.

Ultimately I'd like to create a Vocoder (Cylon) style effect, and later convert it to J2ME for mobile phone use, but ATM want to start crawling before I attempt to run.

I've used a little java before, but when it comes to anything sound/media based I'm clueless, so when I came across the following code it all seemed rather 'complicated' and wondered what do you guys thought of it - should I use it as a basis for what I'm attempting, or is there a simpler way?

EchoScript sample

Many thanks in advance

Paul.
13 years ago
Trying to build my first app in J2ME using the tutorial on Marik Bialoglowy's blog http://bialoglowy.blogspot.com/2006_01_01_archive.html

But in the main class TowerMidlet.java, on the line:
public class TowerMidlet extends MIDlet implements CommandListener {

I get an error:
TowerMidlet is not abstract and does not override abstract method commandAction

Any ideas?

Many thanks in advance

Paul.
14 years ago
Sorted!

I guess my classpath wasn't where I thought it would be.
My pictures/soounds where in subfolders of my main project folder.

This worked fine for setImageIcon?!?!?

Anyway, found a subfolder buried away in the project folders somewhere called classes - moving the image/sound folders there made all the difference.

Many thanks guys.
14 years ago
Okay, scratched together the code below which compiles, but picStream is returning null i.e. I get an exception on ImageIO that the stream is null

BTW there is definitley a folder DTImages in my class path i.e.
picLabel.setIcon(new ImageIcon("DTImages/beast.jpg"));
works fine.

I have also quadruple checked the case of resource and still no joy.

Any ideas?


InputStream picStream = getClass().getResourceAsStream("DTImages/beast.jpg"); //Also tried "DTImages/beast.jpg" but no joy
try {
final BufferedImage icon = ImageIO.read(picStream);
PlayPicViaThread(icon);
picStream.close();
}
catch (Exception e) {
e.printStackTrace();
};

P.S.
URL imgURL = getClass().getResource("DTImages/beast.jpg");
comes back null as well
[ March 02, 2007: Message edited by: Paul Carter ]
14 years ago
Basically still trying to learn Java and currently trying to load pics and sounds from my Jar file, and someone gave me a code snippet to try, but I'm unable to get it to work via Netbeans

Basically FileInputStream seems to be incompatible with getResourceAsStream (expecting fileInputstream not InputStream).

Plus how do I use the image - I've only used label.setIcon which doesn't like BufferedImage?!


// In this example, image.jpg must be in the same folder inside the JAR file as the class
FileInputStream in = getClass().getResourceAsStream("image.jpg");
// Use your favourite API (for example ImageIO) to load the image from the input stream
BufferedImage image = ImageIO.read(in);
// Close the stream
in.close();
picLabel.setIcon(image);
14 years ago

FileInputStream in = getClass().getResourceAsStream("image.jpg");
BufferedImage image = ImageIO.read(in);
in.close();



Cheers guys, but on the first line I get an error:
Incompatible Types
found : java.io.InputStream
required: java.io.FileInputStream
FileInputStream in = getClass().getResourceAsStream("image.jpg");

Do I use an InputStream instead?
14 years ago
I am trying to write a very basic game in Java and would like to store and fetch the images and sounds I play/show from the jar file itself instead of having to distribute the sounds/images in a separate folder.

What's the easiest/best way to acheive this?
14 years ago
Cheers guys - I'm up and running
14 years ago
Still very new to Java and want to create a layout with 3 rows and 1 column, but with the rows of different and fixed heights.

setSize seems to have no effect on the contents, so I must be going about it the wrong way.

Can anyone give me an example of what I need to do?

Many thanks in advance.

Paul.


public Tower(JFrame frame) {


super(new BorderLayout());
setLayout(new GridLayout(3, 1));

JLabel lCDLabel = new JLabel();

JFrame buttonsFrame = new JFrame("Buttons");
JPanel buttonsPanel = CreateTowerButtons();
buttonsPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

buttonsPanel.setSize(new Dimension(100, 300));
lCDLabel.setSize(new Dimension(50, 50));

setBackground(Color.black);
add(lCDLabel);
add(picLabel);
add(buttonsPanel);

tc.SetUpGame();
}
14 years ago
Am trying to write a little game to teach myself Java.

The game has an array of players and values, and restarts once it has completed.

I set up an array of Player objects at the start of the game (shown in the code snippet below), but am not sure how best to destroy the Player array so that I can load the players again.

Any advice?


public void loadPlayers(){
players = new Player[NUMPLAYERS];

for (int i=0; i<players.length; ++i) {
players[i] = new Player();
}
}
14 years ago
Currently have code below that plays a series of pictures and sounds and pauses for a set length of time for each sound/image, all from an array currently generated by various methods.

For various reasons this isn't going to work for me, so I want to be able to play a single picture+sound+pause via a thread as my code rolls along without overlap i.e. I want the pauses to complete before any more code is processed on the thread.

I'm guessing that I'll need to create a public thread e.g. 'playThread()' to feed instructions to, but how exactly this should look is currently beyond my very limited Java abilites

Can anyone give me a code snippet that I can spy on?

Many thanks in advance

Paul.


[fbr - added code tags]
[ January 29, 2007: Message edited by: Fred Rosenberger ]
14 years ago
Sorry - note to self "must not edit code in posts" :roll:

Your right the first line should be showing zero, but the result is the same

Regards

Paul.
14 years ago
As above, I'm trying to reference an array via a method in the following code, but it is not acting as I expect it to.

I am trying to update array element zero only, but I'm either updating both, or referencing both somehow, and for the life of me I can't see how

The following code results in the following output:

!!! BEFORE + GOLD
PlayerGold 0 = 30
PlayerGold 1 = 30
PlayerGold 2 = 30
!!! AFTER + GOLD
PlayerGold 0 = 40
PlayerGold 1 = 40
PlayerGold 2 = 30

i.e. both PlayerGold 1 & PlayerGold 2 seem to be getting updated



pD is an array of class Player which has been created and populated at startup e.g.
public Players pD = new Players();
pD.loadPlayers();



final boolean FoundGold() {
System.out.println("!!! BEFORE + GOLD");

System.out.println("PlayerGold 0 = " + pD.Invent(0)[gV.GOLD]);
System.out.println("PlayerGold " + pD.playerNum + " = " + pD.Invent(pD.playerNum)[gV.GOLD]);
System.out.println("PlayerGold " + (pD.playerNum+1) + " = " + pD.Invent(pD.playerNum+1)[gV.GOLD]);

pD.InventAdd(gV.GOLD, 10);
System.out.println("!!! AFTER + GOLD");

System.out.println("PlayerGold 0 = " + pD.Invent(0)[gV.GOLD]);
System.out.println("PlayerGold " + pD.playerNum + " = " + pD.Invent(pD.playerNum)[gV.GOLD]);
System.out.println("PlayerGold " + (pD.playerNum+1) + " = " + pD.Invent(pD.playerNum+1)[gV.GOLD]);


boolean fGold = true;
}








public class Players {

GlobalVariables gV = new GlobalVariables();
private static final int NUMPLAYERS = 6; //4PLAYERS+TEMP+DRAGON

public Player[] players;
int numPlayers = 1;
int playerNum;
int cursedPlayer = 1;

/** Creates a new instance of Players */
public Players() {
}

public void loadPlayers(){
players = new Player[NUMPLAYERS];

for (int i=0; i<players.length; ++i) {
players[i] = new Player();
}

players[5].invent[gV.GOLD] = 0; //Dragon!!!
players[5].invent[gV.WARRIOR] = 0; //Dragon!!!

}

public int[] Invent(int inventPlayerNum) {
return players[inventPlayerNum].invent;
}

public void InventAdd(int itemID, int incAmount) {
//players[0].invent[itemID] += incAmount;
//if (players[0].invent[itemID] > 99) players[0].invent[itemID] = 99;
//if (players[0].invent[itemID] < 0) players[0].invent[itemID] = 0;
//Doesn't work the above way either

Invent(0)[itemID] += incAmount;
if (Invent(0)[itemID] > 99) Invent(0)[itemID] = 99;
if (Invent(0)[itemID] < 0) Invent(0)[itemID] = 0;
}


public class Player {
public int score = 0;
public int[] foundItems = null;

//public int gold = 30;
//public int warriors = 10;
//public int food = 25;
//public boolean[] boughtItems = new boolean[3];
//public int[] invent = new int[6];

public int[] invent = {0,0,0,10,25,30,0,0,0,0,0};

public int kingdom = 0;
public int moves = 0;
public int lastLocation = 0;
public boolean cursed = false;

public void SetTheScore () {
int score = 140 - (moves + 1 + invent[gV.WARRIOR]);
//Add extra 1 to move as move @ end of turn

if (score > 99) score = 99;
if (score < 0) score = 0;
}
}
}
[ January 15, 2007: Message edited by: Paul Carter ]
14 years ago
Ah wait.... Have something that seems to show it working in principle.

I now need to play a little bit.

Many thanks

Paul.

private void MiniTest(int pauseVal, String picName) {
new Thread(){
public void run(){
try{Thread.sleep(1000);}catch(InterruptedException ie){ie.printStackTrace();}

SwingUtilities.invokeLater(new Runnable(){
public void run(){
picLabel.setIcon(new ImageIcon("C:/DT/images/BEAST.jpg"));
}
});
try{Thread.sleep(1000);}catch(InterruptedException ie){ie.printStackTrace();}

SwingUtilities.invokeLater(new Runnable(){
public void run(){
picLabel.setIcon(new ImageIcon("C:/DT/images/HEALER.jpg"));
}
});
try{Thread.sleep(1000);}catch(InterruptedException ie){ie.printStackTrace();}

SwingUtilities.invokeLater(new Runnable(){
public void run(){
picLabel.setIcon(new ImageIcon("C:/DT/images/SCOUT.jpg"));
}
});
}
}.start();
}
14 years ago
Thanks for that, but still can't get the syntax right for the second image i.e.

new Thread(){
public void run(){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
picLabel.setIcon(new ImageIcon("C:/DT/images/BEAST.jpg"));
}
});
try{Thread.sleep(1000);}catch(InterruptedException ie){ie.printStackTrace();}

}
NOW SHOW SECOND IMAGE AFTER THE ABOVE PAUSE
}.start();
14 years ago