From C# to Java with some troubles

O

OziRus

Hi,

I'm new at Java programming language. I know a bit of C-sharp
language. I wrote a game in C-sharp for fun. It is a memory/find-match
game (like Pairs in old Nokia mobile-phones). I wrote it in C# in 4-5
hours with great helps of Visual Studio 2005. But now, I have to write
it in Java for my supererogatory homework.

I've some-little problems with Java that I can solve in C-sharp easily
but not in Java. First of all; In C# , I define a picturebox array to
keep my images in like; "PictureBox[] boxes = new PictureBox[17]" but
I don't know how to do that in Java. I have no PictureBox control -and
also an array- (by the way, I'm using NetBeans IDE).

Second of all; I bind all of my boxes to my event handler picBox_Click
with writing Box.Click -= new EventHandler(picBox_Click) in a
loop ; but again, I don't know how to do that in Java... I know this
languages have different paradigms in these topics but I couldn't get
it yet.

Thanks for your help...
 
S

senior

for Images use ImageIcon class

for handlers there is many ways
but i prefer anynoumous one

like :

JButton button = new JButton("Press Here ");

button.addActionListener

wrote only the prefix of addActionListener and the NetBeans IDE will
wrote all the rest .

for more info. visit Java tutorial
 
R

RedGrittyBrick

OziRus said:
Hi,

I'm new at Java programming language. I know a bit of C-sharp
language. I wrote a game in C-sharp for fun. It is a memory/find-match
game (like Pairs in old Nokia mobile-phones). I wrote it in C# in 4-5
hours with great helps of Visual Studio 2005. But now, I have to write
it in Java for my supererogatory homework.

I've some-little problems with Java that I can solve in C-sharp easily
but not in Java. First of all; In C# , I define a picturebox array to
keep my images in like; "PictureBox[] boxes = new PictureBox[17]" but
I don't know how to do that in Java. I have no PictureBox control -and
also an array- (by the way, I'm using NetBeans IDE).

Maybe something like
Image[] boxes = new Image[17];
or
List said:
Second of all; I bind all of my boxes to my event handler picBox_Click
with writing Box.Click -= new EventHandler(picBox_Click) in a
loop ; but again, I don't know how to do that in Java... I know this
languages have different paradigms in these topics but I couldn't get
it yet.


There are many ways to do this, one way might be ...

public class ImageArrayTest {

private List<ImageIcon> boxes = new ArrayList<ImageIcon>();

ImageArrayTest() {
// TODO populate boxes then ...
for (ImageIcon box: boxes) {
JButton imageButton = new JButton(box);
imageButton.addActionListener(new ActionListener() {
// TODO something appropriate for button click
});
}
}
}

I tend to have a class implement ActionListener and then use
buttonname.addActionListener(this) but the above is probably more
appropriate in this case.
 
E

Efi Merdler

OziRus said:
Hi,

Second of all; I bind all of my boxes to my event handler picBox_Click
with writing Box.Click -= new EventHandler(picBox_Click) in a
loop ; but again, I don't know how to do that in Java... I know this
languages have different paradigms in these topics but I couldn't get
it yet.

Thanks for your help...


Hi,
Regarding event handling I would recommend reading sun's tutorial on
the subject.
http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html

Efi
 
M

Mark Space

OziRus said:
I've some-little problems with Java that I can solve in C-sharp easily
but not in Java. First of all; In C# , I define a picturebox array to
keep my images in like; "PictureBox[] boxes = new PictureBox[17]" but


I appear to be late to the party here, but if you are using NetBeans
check out the Matisse tutorial. Matisse allows you to build apps
visually, which is darn nicer than having to code them up if you ask me.

After going through the Matisse for NetBeans tutorial (it's on the
NetBeans website), try this.

Make a new project with the New button. That'll give you a single main
class with the standard "static public main (String [] x)" template for
an entry point.

Then click on New->JFrame. You'll get a designer window with a palette
of user controls like menus and text boxes and buttons to play with.
Add about 10 buttons to the JFrame where ever you wish. As the other
poster mentions images can be added to JButtons. I've never tried to do
this so you'll have to play around with the properties window if you
need to pre-set the imaage to something. Lastly, go to the inspector
window on the left, right-click on one of the buttons in the tree
representation of your JFrame, and choose Add->Event->Action Performed.
NetBeans will add an action listener for you and pop you over to the
source code section where you can add some code for when you button gets
clicked.

This is easy and should take you 5 minutes once you get the hang of
using Matisse. Should be a lot shorter time to make a simple game than
4 or 5 hours, since you don't have to fiddle with layouts or debugging
all the code that just makes and displays buttons.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top