Java For The Win?

DY357LXDY357LX Playing since day 1. Still can't Comm.EnglandMembers, Constellation Join Date: 2002-10-27 Member: 1651Posts: 2,309 Fully active user
Or Is It Limited In Functionality?
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 smile-fix.gif

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.

CODE
// 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) { }
   }
}


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.)
"And so is the Golden City blackened With each step you take in my Hall. Marvel at perfection, for it is fleeting. You have brought Sin to Heaven And doom upon all the world."

Comments

  • moultanomoultano Creator of ns_shiva. Members, NS1 Playtester, Contributor, Constellation, NS2 Playtester, Squad Five Blue, Reinforced - Shadow, WC 2013 - Gold, NS2 Community Developer, Pistachionauts Join Date: 2002-12-14 Member: 10806Posts: 4,219 Advanced user
    In case you haven't found it already, this thing is what makes programming in java enjoyable.
    http://java.sun.com/j2se/1.4.2/docs/api/

    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."
  • SandstormSandstorm Members Join Date: 2003-09-25 Member: 21205Posts: 411
    I remember using a Java version of Netscape on OS/2. It was so much faster than the 16-bit Windows version, and it felt just like a native OS/2 application. This was around late '95 or so.

    If OS/2 had actually survived, I suspect Java would have been way more popular than it is now.
    QUOTE (DOOMGuy)
    You are huge! That means you have huge guts!
    Rip and Tear!
  • DY357LXDY357LX Playing since day 1. Still can't Comm. EnglandMembers, Constellation Join Date: 2002-10-27 Member: 1651Posts: 2,309 Fully active user
    QUOTE (moultano @ Mar 3 2005, 07:44 PM)
    In case you haven't found it already, this thing is what makes programming in java enjoyable.
    http://java.sun.com/j2se/1.4.2/docs/api/

    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."

    LMAO
    i'll show my tutor that tomorrow because he's pointed
    out the "it works on all OS's" part several times.
    "And so is the Golden City blackened With each step you take in my Hall. Marvel at perfection, for it is fleeting. You have brought Sin to Heaven And doom upon all the world."
  • CabooseCaboose title = name(self, handle) Members, Constellation Join Date: 2003-02-15 Member: 13597Posts: 4,708 Advanced user
    Yea, but only some peolple like anal sex biggrin-fix.gif

    And my god, I love that API, so handy.
  • DOOManiacDOOManiac Worst. Critic. Ever. Members, NS1 Playtester Join Date: 2002-04-17 Member: 462Posts: 8,596
    There is no one language that you can say "this is the one true and noble language and there should be no others!" Otherwise, we wouldn't have the THOUSANDS of programming languages that we do. :P

    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
    Russell 'DOOManiac' Weed


    [email protected]

  • Nemesis_ZeroNemesis_Zero Old European Members, Retired Developer, NS1 Playtester, Constellation Join Date: 2002-01-25 Member: 75Posts: 12,841
    So, if I catch your meaning, you'd prefer Java to inserting the binary code manually, although not very much? ;P
    QUOTE
    And if I haven't seen further, it's because those bloody giants have blocked my sight.
  • GeminosityGeminosity :3 Members Join Date: 2003-09-08 Member: 20667Posts: 5,835 Advanced user
    Has Java even remotely got near the speed necessary to make a modern game with yet or is it still chugging along thanks to the whole api machine translation thingy which I forgot the proper term for like a year ago :s
  • TychoCelchuuuTychoCelchuuu Anememone Members Join Date: 2002-03-23 Member: 345Posts: 10,499
    QUOTE (Geminosity @ Mar 3 2005, 06:05 PM)
    Has Java even remotely got near the speed necessary to make a modern game with yet or is it still chugging along thanks to the whole api machine translation thingy which I forgot the proper term for like a year ago :s

    Yes.

    That is, you can make modern games in Java. Nobody does. But you can.
    QUOTE (MOOtant @ Sep 21 2012, 11:06 AM) »
    What is wrong with being a racist?

  • NEO_PhyteNEO_Phyte We need shirtgons! Members, Constellation Join Date: 2003-12-16 Member: 24453Posts: 1,369
    QUOTE (TychoCelchuuu @ Mar 3 2005, 08:33 PM)
    QUOTE (Geminosity @ Mar 3 2005, 06:05 PM)
    Has Java even remotely got near the speed necessary to make a modern game with yet or is it still chugging along thanks to the whole api machine translation thingy which I forgot the proper term for like a year ago :s

    Yes.

    That is, you can make modern games in Java. Nobody does. But you can.

    im pretty sure someone here made a java-based multiplayer FPS.

    not sure if the language was java though...
    image
    QUOTE
    Anyone casting a ninth level spell should not be just bending the fabric of reality, but chewing up the fabric of reality and spitting it out. It represents an expenditure of raw magical force that should be momentous, and should have similar shock value as the events following Grand Moff Tarkin's "You may fire when ready."
    ┌╥┐
    â•ž
    ♥╡
    └╨┘
  • DOOManiacDOOManiac Worst. Critic. Ever. Members, NS1 Playtester Join Date: 2002-04-17 Member: 462Posts: 8,596
    edited March 2005
    QUOTE (Geminosity @ Mar 3 2005, 08:05 PM)
    Has Java even remotely got near the speed necessary to make a modern game with yet or is it still chugging along thanks to the whole api machine translation thingy which I forgot the proper term for like a year ago :s

    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
    Russell 'DOOManiac' Weed


    [email protected]

  • CabooseCaboose title = name(self, handle) Members, Constellation Join Date: 2003-02-15 Member: 13597Posts: 4,708 Advanced user
    The language could be used for high end game programing, if the code was compiled into machine code insted of byte code couldn't it. I mean, things like borlands jbuilder of visual J# compile java into native binaries, why not?

    Or am I completly wrong here?
  • DOOManiacDOOManiac Worst. Critic. Ever. Members, NS1 Playtester Join Date: 2002-04-17 Member: 462Posts: 8,596
    Perhaps, but it still wouldn't be the ideal choice. Due to the very high level OOP encapsulation and etc. Java has a lot of overhead (at least for a 3d engine).

    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.
    Russell 'DOOManiac' Weed


    [email protected]

  • NarfwakNarfwak Members, Super Administrators, Forum Admins, NS1 Playtester, Playtest Lead, Forum Moderators, Constellation, NS2 Playtester, Squad Five Blue, Reinforced - Supporter, Reinforced - Silver, Reinforced - Gold, Reinforced - Diamond, Reinforced - Shadow, Subnautica PT Lead, NS2 Community Developer Join Date: 2002-11-02 Member: 5258Posts: 3,785 admin
    At least your Intro to Programming class isn't in Scheme.

    God, I hate this department.
    UWE Playtest Lead
    Be the change you wish to see.
    Twitter
  • ZeroByteZeroByte Members Join Date: 2002-11-01 Member: 3057Posts: 1,404
    QUOTE (TychoCelchuuu @ Mar 4 2005, 10:33 AM)
    QUOTE (Geminosity @ Mar 3 2005, 06:05 PM)
    Has Java even remotely got near the speed necessary to make a modern game with yet or is it still chugging along thanks to the whole api machine translation thingy which I forgot the proper term for like a year ago :s

    Yes.

    That is, you can make modern games in Java. Nobody does. But you can.

    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.
    image
  • SoulSkorpionSoulSkorpion Members Join Date: 2002-04-12 Member: 423Posts: 4,417 Advanced user
    Part of Java's overhead comes from the way it does certain OO things. In the interests of making life easier for mediocre programmers it makes certain decisions about how to accomplish things rather than let the programmer decide, and doesn't let you have any say in the matter.

    For example, the elements of the language that support polymorphic inheritence are always enabled. Method are always called dynamically, regardless of whether or not they really need to be (the C++ equivalent would add the virtual keyword to every single member function indisicriminately). In addition, objects are always 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 each other, never mind platform or API, besides the fact that Java isn't the only language which can do platform independence.

    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.
    image
  • antichristantichrist Members Join Date: 2003-05-27 Member: 16769Posts: 276
    Will someone please think of the children!! use C#!! wink-fix.gif
    gorge, diary of a serial killer! ^_^

    former Owner of commchair.com may it rest in peace

    QUOTE
    Why not? Because its a personal attack? Tell you what, if God was offended by my remark then he can PM the mods about it.
  • GwahirGwahir Members, Constellation Join Date: 2002-04-24 Member: 513Posts: 4,270
    if I had to choose between C# and Java, I'd choose C#, definitely. It at least gives you the option of going into unsafe mode and have that power if you want it.
    QUOTE
    Sanity is just a weird form of madness.
  • moultanomoultano Creator of ns_shiva. Members, NS1 Playtester, Contributor, Constellation, NS2 Playtester, Squad Five Blue, Reinforced - Shadow, WC 2013 - Gold, NS2 Community Developer, Pistachionauts Join Date: 2002-12-14 Member: 10806Posts: 4,219 Advanced user
    QUOTE (SoulSkorpion @ Mar 4 2005, 03:36 AM)
    Part of Java's overhead comes from the way it does certain OO things. In the interests of making life easier for mediocre programmers it makes certain decisions about how to accomplish things rather than let the programmer decide, and doesn't let you have any say in the matter.

    For example, the elements of the language that support polymorphic inheritence are always enabled. Method are always called dynamically, regardless of whether or not they really need to be (the C++ equivalent would add the virtual keyword to every single member function indisicriminately). In addition, objects are always 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 each other, never mind platform or API, besides the fact that Java isn't the only language which can do platform independence.

    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.

    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.)
Sign In or Register to comment.