Please login or register.

Login with username, password and session length
Advanced search  

News:

Planet Explorers Released!

Author Topic: who likes java?  (Read 985 times)

cupid_the_conqueror

  • Settler
  • ***
  • Karma: -32
  • Posts: 125
    • View Profile
who likes java?
« on: July 19, 2014, 05:03:19 PM »

I like java! heres a sample of something basic I made today in 30 minutes or so , cause I was bored!


[spoiler]import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class thescreen<ballcollison> extends JFrame  {
   
   int ballx = 250 ; int bally = 50;
   
   int xmove = 2; int ymove = 2 ;
   
   boolean run = true;
   
   
   Ellipse2D.Float ball = new Ellipse2D.Float(ballx, bally, 50, 50);
   
   Graphics buffergraphics ;
   
   Image offscreen;

   Dimension dim = getSize();
   
   ballmovement ballhandler = new ballmovement() ;
    refreshrate refresh = new refreshrate ();
    ballcollison collide = new ballcollison();
   
   
   
   public thescreen() {
      
      super("I'm just a ball");

      try {
         UIManager
               .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
      } catch (ClassNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (InstantiationException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IllegalAccessException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (UnsupportedLookAndFeelException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      // set screen size and window nature
      
      setSize(500, 250);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      
      
      
      ballhandler.start();
      refresh.start();
      collide.start();
      
   }
   
   public void paint(Graphics g) {
      
      dim = getSize();
      
      offscreen = createImage(dim.width, dim.height);
      
      buffergraphics = offscreen.getGraphics();
      
      buffergraphics.clearRect(0, 0, dim.width, dim.width);
      
      buffergraphics.setColor(Color.red);
      
      
      buffergraphics.fillOval(ballx, bally, 50, 50);
      
      g.drawImage(offscreen, 0, 0, this);
      
      
   }
   
   public void update(Graphics g) {
      paint(g);
   
   }
   
   public class ballmovement extends Thread {
      
      public void run () {
         
         while (run==true){
         
         ballx = ballx - xmove;
         bally = bally - ymove;
      
         try {
            Thread.sleep(30);
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
      
      }
      
   }
   
   public class refreshrate extends Thread {

      public void run() {
                   
         while (run == true){
            repaint();

            // okay , below is the delay behinde frames / prints
            // odly enough, a higher vaule such as 8 or 10 seems to make the
            // game run
            // smoother... you would think that a lower vaule would do
            // better
            // beacuse then you would have more frames, but i guess not
            try {
               Thread.sleep(12);
            } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
         
         }
         
      }
      }

   
   public class ballcollison extends Thread {
      
      public void run() {
         
         
         
         while (run==true){
            
            dim.getSize();
            
            if (ballx == dim.width-50){
               if (xmove < 0){
               xmove = xmove- (xmove*2);
               
               System.out.println("ball redirected x");
               }
            }
            
            
            if (bally == dim.height-50){
               
               if (ymove < 0){
               
               ymove = ymove - (ymove*2);
               System.out.println("ball redirected y " + ymove);
               }
            }
            
            
            if (bally == 20){
               
               if (ymove > 0){
               
               ymove = ymove - (ymove*2);
               System.out.println("ball redirected y " + ymove);
               }
            }
            
            if (ballx == 0){
            
               if (xmove > 0){
               xmove = xmove- (xmove*2);
               System.out.println("ball redirected x");
               }
            }
            
            try {
               Thread.sleep(4);
            } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
            
         }
         
      }
      
   }
   
   
}
[/spoiler]

I've done some different stuff like space invaders and pong, post your stuff and I'll try to one up you! XD
Logged
the wheel is turning but the hamster is dead

also can lantoaster or whoever stop karma bombing me? its rather silly. *gets neged for 80 more* ..dammit...