Java For The Win?
DY357LX
Playing since day 1. Still can't Comm.England Join Date: 2002-10-27 Member: 1651Members, Constellation
in Off-Topic
<div class="IPBDescription">Or Is It Limited In Functionality?</div> I've been learning Java on my I.T course over the past 4/5 days
(all of it I have cruised through and stayed a good 4 steps ahead
of the pack so I can chill out at the end of the weeks) and the only
thing that has really grabbed my attention.... is the Java Programming.
Thankfully, my tutor is pretty clever and he knows what he's doing.
So i'm not stuck reading stupidly complex books.
He does seem very pro Java and doesn't seem to like me asking questions
about C++. (I keep accidently calling "Methods", "Functions" and that
seems to grate on him alittle.)
I asked weather an applet that sets the X and Y co-ordinates of the
mouse pointer was possible but he wasn't sure and said that Java was
very high on keeping things "Secure". And lets be honest, messing with
a users mouse is hardly secure <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo-->
So anyway, I want to know if any of you lot can point me in the direction of
some nice nub-friendly tutorials (I have several sites book-marked already)
especially those that point to playing with the mouse positions.
Here's what I managed today: java guru's can copy and paste this
into their own .java file and then compile it. It's, as far as I know, errorless.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->// Project Name : HelloJava 4
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloJava4
{
public static void main( String [] args )
{
JFrame frame = new JFrame( "HelloJava4" );
frame.getContentPane().add( new HelloComponent4("Look at me, i'm colour blind!" ));
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize (300, 300);
frame.setVisible ( true );
}
}
class HelloComponent4 extends JComponent
implements MouseMotionListener, ActionListener, Runnable
{
String theMessage;
int messageX = 40, messageY = 95; //coordinates of msg
JButton theButton;
int colorIndex; //current index into some colours
static Color[] someColors =
{
Color.black, Color.red, Color.green, Color.blue, Color.cyan, Color.magenta
};
boolean blinkState;
public HelloComponent4( String message )
{
theMessage = message;
theButton = new JButton("Change Colour");
setLayout( new FlowLayout() );
add( theButton );
theButton.addActionListener( this );
addMouseMotionListener( this );
Thread t = new Thread ( this );
t.start();
}
public void paintComponent( Graphics g )
{
g.setColor(blinkState ? getBackground() : currentColor() );
g.drawString(theMessage, messageX, messageY);
}
public void mouseDragged(MouseEvent e)
{
messageX = e.getX();
messageY = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) { }
public void actionPerformed( ActionEvent e)
{
if (e.getSource() == theButton )
changeColor();
}
synchronized private void changeColor()
{
if (++colorIndex == someColors.length)
colorIndex = 0;
setForeground( currentColor() );
repaint();
}
synchronized private Color currentColor( )
{
return someColors[colorIndex];
}
public void run( )
{
try
{
while(true) {
blinkState = !blinkState;
repaint();
Thread.sleep(300);
}
}catch (InterruptedException ie) { }
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Tomorrow (or Monday) i'll be writing a "magic 8 ball" applet.
At the moment I like C++ more than I like Java, but thats
probably because I have a slightly better understaning of it.
(Only slightly, i'm still learning.)
(all of it I have cruised through and stayed a good 4 steps ahead
of the pack so I can chill out at the end of the weeks) and the only
thing that has really grabbed my attention.... is the Java Programming.
Thankfully, my tutor is pretty clever and he knows what he's doing.
So i'm not stuck reading stupidly complex books.
He does seem very pro Java and doesn't seem to like me asking questions
about C++. (I keep accidently calling "Methods", "Functions" and that
seems to grate on him alittle.)
I asked weather an applet that sets the X and Y co-ordinates of the
mouse pointer was possible but he wasn't sure and said that Java was
very high on keeping things "Secure". And lets be honest, messing with
a users mouse is hardly secure <!--emo&:)--><img src='http://www.unknownworlds.com/forums/html/emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo-->
So anyway, I want to know if any of you lot can point me in the direction of
some nice nub-friendly tutorials (I have several sites book-marked already)
especially those that point to playing with the mouse positions.
Here's what I managed today: java guru's can copy and paste this
into their own .java file and then compile it. It's, as far as I know, errorless.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->// Project Name : HelloJava 4
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloJava4
{
public static void main( String [] args )
{
JFrame frame = new JFrame( "HelloJava4" );
frame.getContentPane().add( new HelloComponent4("Look at me, i'm colour blind!" ));
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize (300, 300);
frame.setVisible ( true );
}
}
class HelloComponent4 extends JComponent
implements MouseMotionListener, ActionListener, Runnable
{
String theMessage;
int messageX = 40, messageY = 95; //coordinates of msg
JButton theButton;
int colorIndex; //current index into some colours
static Color[] someColors =
{
Color.black, Color.red, Color.green, Color.blue, Color.cyan, Color.magenta
};
boolean blinkState;
public HelloComponent4( String message )
{
theMessage = message;
theButton = new JButton("Change Colour");
setLayout( new FlowLayout() );
add( theButton );
theButton.addActionListener( this );
addMouseMotionListener( this );
Thread t = new Thread ( this );
t.start();
}
public void paintComponent( Graphics g )
{
g.setColor(blinkState ? getBackground() : currentColor() );
g.drawString(theMessage, messageX, messageY);
}
public void mouseDragged(MouseEvent e)
{
messageX = e.getX();
messageY = e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) { }
public void actionPerformed( ActionEvent e)
{
if (e.getSource() == theButton )
changeColor();
}
synchronized private void changeColor()
{
if (++colorIndex == someColors.length)
colorIndex = 0;
setForeground( currentColor() );
repaint();
}
synchronized private Color currentColor( )
{
return someColors[colorIndex];
}
public void run( )
{
try
{
while(true) {
blinkState = !blinkState;
repaint();
Thread.sleep(300);
}
}catch (InterruptedException ie) { }
}
}
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Tomorrow (or Monday) i'll be writing a "magic 8 ball" applet.
At the moment I like C++ more than I like Java, but thats
probably because I have a slightly better understaning of it.
(Only slightly, i'm still learning.)
Comments
<a href='http://java.sun.com/j2se/1.4.2/docs/api/' target='_blank'>http://java.sun.com/j2se/1.4.2/docs/api/</a>
Most of the time, my thoughts on java can be summed up in this great quotation.
"Liking java because it works on all systems is like liking anal sex because it works on all genders."
If OS/2 had actually survived, I suspect Java would have been way more popular than it is now.
<a href='http://java.sun.com/j2se/1.4.2/docs/api/' target='_blank'>http://java.sun.com/j2se/1.4.2/docs/api/</a>
Most of the time, my thoughts on java can be summed up in this great quotation.
"Liking java because it works on all systems is like liking anal sex because it works on all genders." <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
LMAO
i'll show my tutor that tomorrow because he's pointed
out the "it works on all OS's" part several times.
And my god, I love that API, so handy.
Java, just like any other language, is just another tool in the programmer's toolbox. It definately has its place. You wouldn't use an axe to drive a nail (although it would certainly work), nor would you use a hammer to cut down a tree. And of course, everyone has their favorite tool in the toolbox, such is human nature.
Java's main strong point is running on multiple platforms (which nowadays can include PDAs, cell phones, wristwatches, or even your TV). Another very thing it has going for it, that I sometimes really, really miss with other languages, is the Javadoc documentation system. Also Java is completely and truely 100% object oriented. It's not optional like it is in almost every other OOP-capable language I've worked with, its required. Sometimes this is a very good thing, however it occassionally is a nusiance.
So how do I feel about it? It used to be really, really crappy, but its gotten a lot better within the past 4 years, and nowadays it has firmly claimed its space in the grand scheme of things. While I'd rather use C++, PHP, or even VB.net (again, depending on the application), at least its better than Pascal, COBOL, LISP, ADA, FORTRAN, etc. :P
Yes.
That is, you can make modern games in Java. Nobody does. But you can.
Yes.
That is, you can make modern games in Java. Nobody does. But you can. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
im pretty sure someone here made a java-based multiplayer FPS.
not sure if the language was java though...
Once again this falls under the category of 'the right tool for the right job'.
For a modern 3D game that requires squeezing every tiny bit of performance out of 3D cards, only a fool would even consider Java for the task. By the very core design of the language, it will never ever be ideal for this task.
However, if you're writing a 2.5D (DOOM, Duke3D) or sprite based game for say a cell phone, Java is ideal. You can be sure it is going to work on everybody's phone that supports Java. And you can code fast. I have a friend at school who wrote a Wolf3D equivilant (minus sound, but that's easy too) engine that runs at 10-15fps w/o any optimizations over the weekend.
However, when it comes to game development languages aren't all-or-nothing. Many games will use C++ for the core engine, but another language for other areas. Many games, especially RPGs, will use Python, Java, or even Pascal for their cutscene/dialog scripting. Vampire: The Masquerade (the first one, Redemption) used Java I believe, whereas Bloodlines uses Python. Unreal engine uses their own language, called UnrealScript, which is very Java-like.
I hope I'm not coming off as a know-it-all or anything. Someone else give a dissenting opinion :P
Or am I completly wrong here?
You can do full polygonal 3d engines in java. you can do shaders and whatever if you set your heart to it. and you could even write your own Java compiler that compiled it to x86 Win32/Linux code if you really really wanted. However it still wouldn't be as optimized for the task as C++, and you'd spend a lot of dev time (and hence, money) in the process. But again, remember, I'm only talking about bleeding-edge technology in this case. DOOM3, Source, UnrealEngine3 type stuff.
God, I hate this department.
Yes.
That is, you can make modern games in Java. Nobody does. But you can. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
Puzzle Pirates is programmed in Java and it could be considered as a modern game.
I haven't really touched Java in a year or so. My impressions of it when I was using it though was that it's really great for learning OOP concepts, since as DOOMeh said, OOP isn't really optional in Java.
For example, the elements of the language that support polymorphic inheritence are <i>always</i> enabled. Method are <i>always</i> called dynamically, regardless of whether or not they really need to be (the C++ equivalent would add the <span style='font-family:Courier'>virtual</span> keyword to every single member function indisicriminately). In addition, objects are <i>always</i> referred to by reference variables to facilitate dynamic type identification (whereas in C++ you can have objects on the stack, just like ordinary variables).
You may have guessed, but I don't like Java. To me, the language itself seems to be just a dialect of C++ (but with half the vocabulary), designed for people who find choice confusing and distracting. It's not good practise to write code in which the modules are too heavily dependent on <i>each other</i>, never mind platform or API, besides the fact that Java isn't the only language which can do <a href='http://www.libsdl.org/' target='_blank'>platform independence</a>.
Of course this is all entirely pointless, since in the real world one uses not even necessarily the right tool for the job, but whatever tool one's boss tells them to use.
For example, the elements of the language that support polymorphic inheritence are <i>always</i> enabled. Method are <i>always</i> called dynamically, regardless of whether or not they really need to be (the C++ equivalent would add the <span style='font-family:Courier'>virtual</span> keyword to every single member function indisicriminately). In addition, objects are <i>always</i> referred to by reference variables to facilitate dynamic type identification (whereas in C++ you can have objects on the stack, just like ordinary variables).
You may have guessed, but I don't like Java. To me, the language itself seems to be just a dialect of C++ (but with half the vocabulary), designed for people who find choice confusing and distracting. It's not good practise to write code in which the modules are too heavily dependent on <i>each other</i>, never mind platform or API, besides the fact that Java isn't the only language which can do <a href='http://www.libsdl.org/' target='_blank'>platform independence</a>.
Of course this is all entirely pointless, since in the real world one uses not even necessarily the right tool for the job, but whatever tool one's boss tells them to use. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
Personally, I think most of the things Java removes from C++ are just bad programming practices. Granted, I would like to be able to make convenient bit masks without having to deal with character sets, but C++ lets you do some really heinous ****. I cut my teeth on C, and every time I see an object on the stack, or an object as a return value, some part of me dies. If it can't fit into %eax its not a return value dammit!
For me, if I'm doing io or anything that requires touching raw bytes, I use pure ansi C. If the code is more data structure driven and won't have to do much io, I use java. The biggest thing Java has over C++ is decent type checking. Its not ML, but it cuts down on programming time quite a bit, particularly if you are coding while tired.
(btw, if you ever have a chance to learn SML of NJ, check it out. Functional programming is a **** to wrap your head around if you aren't soaked in recursion, but its a wonderful feeling barely having to test your code, because you can look at it, and it proves its own correctness in front of you.)