Results 1 to 9 of 9
  1. #1
    ssunlimited is offline Member
    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Default Can Java do much more than this? Can Java create professional video games?

    This is the most advanced thing I have ever done. We did this in class in my second and final semester of Java in my 2-year college.

    Java Code:
    import java.awt.Graphics.*;
    import   java.io.*;
    import  java.applet.Applet;
    import  java.awt.*;
    import  java.awt.event.*;
    import java.applet.*;
    public class CandyMachine extends Applet implements ActionListener{
    
       Label l1, l2, l3, l4, ch, m, p;
       Button b1, b2;
      TextField m1, m2, m3, m4, change;
      Candy mandm, peanuts; //create candy objects
       double d, q, dime,nickel, tot;
    
      public void init(){
       mandm = new Candy(20, .60, "M & M");//create m&m object
       peanuts = new Candy(15, .75, "Nuts"); // create peanut object
       
    	
    	  
       b1 = new Button(mandm.name); //getting "M & M"
       b2 = new Button(peanuts.name); //getting "Nuts"
    
       m1 = new TextField(5);
       m2 = new TextField(5);
       m3 = new TextField(5);
       m4 = new TextField(5);
       change = new TextField("             ");
    
       l1 = new Label("$1.00");
       l2 = new Label(".25");
       l3 = new Label(".10");
       l4 = new Label(".05");
       ch = new Label("Change");
        m = new Label(Double.toString(mandm.price));
       
       p = new Label(Double.toString(peanuts.price));
       add(l1);
       add(m1);
       add(l2);
       add(m2);
       add(l3);
       add(m3);
       add(l4);
       add(m4);
       add("CENTER",ch);
       add(change);
       add(m);
       add(b1);
       add(p);
       add(b2);
       b1.addActionListener(this);
       b2.addActionListener(this);
    }
    
     public void paint(Graphics g){
      //System.out.println("in paint");
       g.drawString("Paint",200,200);}
    
    public void actionPerformed(ActionEvent ae) {  //come here when a button is press
      if (m1.getText().length()==0)
              d=0;
        else d = Double.parseDouble(m1.getText());  //use getText() because m1 is a text field
      if (m2.getText().length()==0)
              q=0;
        else q = Double.parseDouble(m2.getText());  //use getText() because m1 is a text field
      if (m3.getText().length()==0)
              dime=0;
        else dime = Double.parseDouble(m3.getText());  //use getText() because m1 is a text field
      if (m4.getText().length()==0)
              nickel=0;
        else nickel = Double.parseDouble(m4.getText());  //use getText() because m1 is a text field
      
    
       //q = Double.parseDouble(m2.getText());
       //dime= Double.parseDouble(m3.getText());
       //nickel = Double.parseDouble(m4.getText());
       tot = d + q*.25 + dime*.1 + nickel*.05;
      
    
       if (ae.getSource()==b1)
    	    if (mandm.anyCandy())
    	        if (tot>=mandm.price ){
    	            mandm.sell();
    	           tot=(int)((tot-mandm.price+.001)*100.0)/100.0;
    	           change.setText(Double.toString(tot));//convert double to string
    		  }
    	        else {
    		      tot=(int)((mandm.price-tot+.001)*100.0)/100.0;
    		      change.setText("Add " + Double.toString(tot) + "cents");
    		        }
    	    else {change.setText("empty");
                    b1.setLabel("empty"); }
    	    
    	else if(ae.getSource()==b2)
    	         if (peanuts.anyCandy())
    	             if (tot>=peanuts.price){
    	        	     peanuts.sell();
    	                 tot=tot-peanuts.price;
    	                 change.setText(Double.toString(tot)); //convert double to string
    	    		 }
    			 else {
    		       tot=peanuts.price-tot;
    	              change.setText("Add " + Double.toString(tot) + "cents");
    	              }
    	     else change.setText("empty");
    	    
    
    }
    
    public class Candy{
    	private int count;
    		double price;
    		String name;
    		public Candy(int ct, double pr, String name) {
    			count = ct;
    			price = pr;
    			this.name = name;
    }
    	public boolean anyCandy(){
    		if (count > 0)
    			return true;
    		else return false;
    	}
    	public void sell(){
    		if (anyCandy())
    			count--;
    	}
    
    }
    
    }
    This is really simple compared to some programs out there. Can Java do much more than this? Can Java create professional video games?

  2. #2
    noctarius is offline Member
    Join Date
    Jun 2012
    Location
    Remscheid, Germany
    Posts
    63
    Rep Power
    0

    Default Re: Can Java do much more than this? Can Java create professional video games?

    The answer is simple and let me guess, Google had answered it as well :)

    yes and yes

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Can Java do much more than this? Can Java create professional video games?

    Depends what you class as professional.
    One of the most successful games of the past few years was written in Java.

    Now, it's not common admittedly, but it has happened.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can Java do much more than this? Can Java create professional video games?

    Quote Originally Posted by Tolls View Post
    Depends what you class as professional.
    One of the most successful games of the past few years was written in Java.

    Now, it's not common admittedly, but it has happened.
    Correct, but the only reason it was popular was because of what it did; people only grudgingly tolerated that it is written in Java :)

    It has opened up a whole new line of schooling, there are kids out there that get introduced to programming through Minecraft modding. Apparently the code is really well structured to make it easy to adopt the concepts.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    noctarius is offline Member
    Join Date
    Jun 2012
    Location
    Remscheid, Germany
    Posts
    63
    Rep Power
    0

    Default Re: Can Java do much more than this? Can Java create professional video games?

    Not only Minecraft is written in Java. There's lot of serverside Java in most social games as well as a lot of the Flash browsergames, I worked one myself. :)

    Java sneeks its way into the business, slowly but steady. The problem is still that most of the common, long term gamedevs are C++ coders that have a hard time getting used to GC, JIT and Hotspot applied optimizations.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Can Java do much more than this? Can Java create professional video games?

    Quote Originally Posted by gimbal2 View Post
    Correct, but the only reason it was popular was because of what it did; people only grudgingly tolerated that it is written in Java :)

    It has opened up a whole new line of schooling, there are kids out there that get introduced to programming through Minecraft modding. Apparently the code is really well structured to make it easy to adopt the concepts.
    And I have long argued that the big game companies (not so much the designers, but the design-by-committee ones) miss this key. They get very focussed on a very tech-oriented view of it all, and not a game-oriented one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can Java do much more than this? Can Java create professional video games?

    To be fair - the average gamer is way too tech-oriented too. They're a hard bunch to please, which makes Minecraft even more of a surprise. It looks like it was made in the 80's yet the kids adore it as much as they do Pokemon. But games like Far Cry 4 or Watch Dogs get gunned down because they look like they're from '2013'. Minecraft regularly drops under 20fps and its all wonderful; if any other game is locked at 30fps to maintain a stable framerate its a massive crime against humanity.

    I don't get any of it, but I sit back with popcorn and enjoy the ride anyway :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Can Java do much more than this? Can Java create professional video games?

    Quote Originally Posted by gimbal2 View Post
    To be fair - the average gamer is way too tech-oriented too. They're a hard bunch to please, which makes Minecraft even more of a surprise. It looks like it was made in the 80's yet the kids adore it as much as they do Pokemon. But games like Far Cry 4 or Watch Dogs get gunned down because they look like they're from '2013'. Minecraft regularly drops under 20fps and its all wonderful; if any other game is locked at 30fps to maintain a stable framerate its a massive crime against humanity.

    I don't get any of it, but I sit back with popcorn and enjoy the ride anyway :)
    Which only goes to show that the gaming audience is far larger than the rather insular dev community noticed.
    It may be a surprise to some, but playing it (and seeing my kids play it) it's no surprise to me.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can Java do much more than this? Can Java create professional video games?

    I just wonder if this could have happened if Google/Youtube hadn't introduced their partner program to allow people to make money on the videos they create. Without that program, this wouldn't have happened:

    https://www.youtube.com/watch?v=ANgI2o_Jinc

    And that's how me and most other people during the beta days of Minecraft learned to love that silly little tech demo.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Quick Question on 2D Java Games
    By Danieldcc in forum New To Java
    Replies: 1
    Last Post: 04-01-2012, 09:15 PM
  2. rewrite dos abandonware games to java
    By Sangetsu041282 in forum New To Java
    Replies: 3
    Last Post: 03-06-2012, 09:01 PM
  3. Help with Java interaction with flash games?
    By Yoni201 in forum New To Java
    Replies: 3
    Last Post: 01-30-2012, 01:28 AM
  4. how to create a video player in java?
    By asmitarnd in forum Advanced Java
    Replies: 3
    Last Post: 03-21-2011, 01:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •