Side Project, Porting C++ to Java

Pages: 12
Apr 25, 2012 at 12:37pm
I've coded a little sum sum in my free time when I'm not blowing off my classes that I'd like to try and port to java. I've heard java is simple to use and is very interface friendly. By that I mean you can take code and make an applet that is easy to create. Well I'd like to take what I've written in C++ and port it to java. I was wondering if someone had any experience with java that would help me write an applet with the code I have. I, by no means, want to get it written for me. I'd like to be able to get walked through it so that I can replicate this for future reference. I've done a fair amount of reading on creating applets and I really have a hard time learning by reading books or articles. I'm more of a walkthrough it first and get it learner. Anyways, if this fits you, please feel free to email me or pm me on here.

Thanks.
Apr 25, 2012 at 1:49pm
Here's the source code to the main applet file of my work-in-progress applet game:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.applet.*;
import java.awt.*;
import game.*;

public class Game_Test extends Applet
{
	Thread gt;
	Game game;
	public void init()
	{
		this.resize(640, 480);
		game = new Game(this);
		setFocusable(true);
	}
	public void start()
	{
		gt = new Thread(game, "Game Thread");
		gt.start();
	}
	public synchronized void stop()
	{
		if(gt != null && gt.isAlive())
		{
			gt.interrupt();
		}
		gt = null;
	}
	
	public void paint(Graphics g){}
}
The "Game" class I made implements Runnable and has some other classes that implement key listeners and mouse listeners.
Apr 25, 2012 at 2:45pm
closed account (S6k9GNh0)
Where's rapidcoder? It's been 5 hours already. :/
Last edited on Apr 25, 2012 at 6:25pm
Apr 25, 2012 at 5:24pm
I'm not sure what you mean by rapidcoder? Is that a person?
Apr 25, 2012 at 7:34pm
No. It is an account on these forums, most likely belonging to a person - not actually being a person.
Apr 25, 2012 at 8:07pm
closed account (1yR4jE8b)
rapidcoder is our resident Java expert.
Apr 25, 2012 at 10:10pm
Here's the full source to my WIP game:
http://www.LB-Stuff.com/Java/Game_Test/..zip (change ..zip to Game_Test.htm for the game in a browser ready to play)
It's going throgh heavy refactoring and this is the most recent 'cleanest' version.

For applets, you'll be interested mainly in game.Game.run(), game.ScreenDrawer.Display, and game.ScreenDrawer.UpdateDrawRegion
Apr 26, 2012 at 8:11am
Well, but what is *the* question? If you know Java and you know C++, porting C++ to Java is quite easy (easier this way than the other way round). You just have to try to do it and post questions whenever you have a concrete problem.
Last edited on Apr 26, 2012 at 8:12am
Apr 26, 2012 at 8:20am
I don't know Java at all. I know C++. I just don't get any of the syntax required to even get a working project in java. Java is the moon to me.
Apr 26, 2012 at 1:43pm
Java and C++ have extremely similar syntax. What is confusing to you?

For starters, you should know Java has nine primitive types:
boolean (C++'s bool)
byte (C++'s signed char, 8 bits)
char (C++'s unsigned short, 16 bits)
short (C++'s signed short, 16 bits)
int (C++'s signed int or signed long, 32 bits)
long (C++'s signed long long, 64 bits)
float (C++'s float, 32 bits)
double (C++'s double, 64 bits)
references (C++'s pointers)

References can only reference class types, they cannot reference primitives. Since references are themselves primitives, you cannot have references to references. They resemble C++'s pointers, not C++'s references. They can be either null or referencing something, and using == on them compares to see if they reference the same thing or no, not if what they reference is equal. (This always tricks people up) You should use obj1.equals(obj2) to test if two referenced objects equal each other.
Last edited on Apr 26, 2012 at 1:47pm
Apr 26, 2012 at 4:48pm
Judging by what you've written there, I'm glad I learned C# instead of Java.
Apr 26, 2012 at 4:50pm
C# is like C++ and Java got married, so that automatically makes it better. [/opinion]
Apr 26, 2012 at 5:50pm
L B wrote:
[/fact]

FTFY :)
Apr 26, 2012 at 11:33pm
Let me clarify. I can write half-assed code in c++. I get the concept of different data types. What I don't get is how to use all the java syntax. I can't understand what to write in a java project. If I did, I wouldn't be here.

I'm referring to all the java.blah.Blah stuff. And I just tried writing a simple multiplication program that multiplied two doubles together. Needless to say, it didn't work.
Last edited on Apr 26, 2012 at 11:44pm
Apr 27, 2012 at 2:06am
1
2
3
4
5
6
7
8
public class MultiplyDoubles
{
    public static void main(String[] args)
    {
        double d1 = 3.0, d2 = 4.0;
        System.out.println(d1 * d2);
    }
}
System is a class:
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html
out is a data member of type PrintStream:
http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html
println is a method that prints out its parameter and adds a new line after it.
Apr 27, 2012 at 7:14am

C# is like C++ and Java got married, so that automatically makes it better


More features in a programming language is not always better. Actually well designed languages are those that achieve good expressivity with few core features. Think: Lisp or Scala. Anyway, C# is actually Java with some minor things fixed. But Microsoft was later, so they would be idiots if they didn't fix some things. Nevertheless, their runtime still sucks for server applications, performance-wise.


I'm referring to all the java.blah.Blah stuff. And I just tried writing a simple multiplication program that multiplied two doubles together. Needless to say, it didn't work.


Try Scala instead. It is easier to learn.

1
2
3
4
5
println("Enter the first number: ")
val a = readInt
println("Enter the second number: ")
val b = readInt
println("Multiplicity of the numbers is: " + a * b)
Last edited on Apr 27, 2012 at 7:16am
Apr 27, 2012 at 1:17pm
Can you make applets or an equivalent in Scala or Lisp?
Apr 27, 2012 at 1:21pm
Why would you want to write applets? Does anybody actually still do that nowadays? There was a cool website with a few emulator applets, but other than that I can't think of any reason.
Apr 27, 2012 at 1:25pm
incremental wrote:
I've coded a little sum sum in my free time when I'm not blowing off my classes that I'd like to try and port to java. I've heard java is simple to use and is very interface friendly. By that I mean you can take code and make an applet that is easy to create. Well I'd like to take what I've written in C++ and port it to java. I was wondering if someone had any experience with java that would help me write an applet with the code I have. I, by no means, want to get it written for me. I'd like to be able to get walked through it so that I can replicate this for future reference. I've done a fair amount of reading on creating applets and I really have a hard time learning by reading books or articles. I'm more of a walkthrough it first and get it learner. Anyways, if this fits you, please feel free to email me or pm me on here.

Thanks.
Apr 27, 2012 at 1:45pm
I think his question was directed at the OP.
Pages: 12