woensdag 28 mei 2014

getWitdh() and getHeight() example.


I was using getSize().width and getSize().height. Now I discovered that you can just use getWidth() and getHeight() for getting the applet width and height. Below in the sourcecode I am using these commands.




package test001;
import java.awt.*;
import java.applet.*;

public class testing001 extends Applet implements Runnable {
 // Graphics for double buffering.
 Graphics      bufferGraphics;
 Image       offscreen;
 
 public void init() {
     setBackground(Color.black);
     offscreen = createImage(getWidth(),getHeight());
     bufferGraphics = offscreen.getGraphics();
     new Thread(this).start();

 }

    public void run() {
        for(;;) { // animation loop never ends
         repaint();
         try {
             Thread.sleep(16);
             }
             catch (InterruptedException e) {
             }
     }
    }

    public void update(Graphics g){
     bufferGraphics.clearRect( 0 , 0 , getWidth() , getHeight() );
     bufferGraphics.setColor( Color.red );
     bufferGraphics.drawString( "getWidth() and getHeight()" , 10 , 10 );

     bufferGraphics.drawRect( 20 , 20 , getWidth()-40 , getHeight()-40 );
  
       g.drawImage(offscreen,0,0,this);
    }

}

dinsdag 8 april 2014

Planning part of Civilization game code

One of the type of games that I want to  make is the civilization like game. For a long time now I am learning how to program a civilization clone. The last few weeks I have been programming the worker automation and artificial intelligence of it. I got to the point where I have code that creates a map with cities and worker units that improve the map with roads and irrigation. I am working my way towards creating a more completed and civilization alike version. I am getting better at it.

It has been months since I posted on this blog and I think that I need to keep adding code to it. I am currently in the process of repeatitly retyping what I have in my main language blitz basic. I get to about a thousand lines per version. I think I wil be able to create a java version of it. It wil have pathfinding and map generating in it. 

I have no idea when I wil do this but I am sure I wil do it. Keep a look out for it.

Here is a link to a video of a older version.

http://youtu.be/6LNGoTIixJs

dinsdag 29 oktober 2013

Space Invaders Example (moving wave/collision/shooting and moving player)

I made a new example in Jcreator. The example applet size should be 320*240.
I formatted the code with a site tool that I found. No applets anymore since they do not seem to work anymore with my google site.

There is not a lot of comment in the code. I want to keep it clean.


/**
 * @(#)SpaceInvadersExample003.java
 *
 * SpaceInvadersExample003 Applet application
 *
 * @author Rudy van Etten
 * @version 1.00 2013/10/28
 */

import java.awt.*;
import java.applet.*;

public class SpaceInvadersExample003 extends Applet  implements Runnable{
    Graphics bufferGraphics;
    Image offscreen;

    double px = 160-8;
    double py = 200;
    int pwidth = 16;
    int pheight = 16;
    boolean ismovingright = false;
    boolean ismovingleft = false;
    int wavedirection = 1; // 1 = right 0 = left
    long firedelay = 0;
    double bullet[][] = new double[ 32 ][ 5 ]; //active,x,y,my
    double alien[][] = new double[10*5][5];//active,x,y
    public void init() {
           setBackground(Color.black);
          offscreen = createImage(getSize().width,getSize().height);
           bufferGraphics = offscreen.getGraphics();
        int x=0;
        int y=0;
        for(int i=0;i<10*5;i++){
            alien[i][0]=1;
            alien[i][1]=x;
            alien[i][2]=y;
            x+=32;
            if(x>getSize().width-32){
                x=0;
                y+=32;
            }

           }
        new Thread(this).start();

    }
    public void run() {
        for(;;) { // animation loop never ends
            if (ismovingright==true){
                if (px<getSize().width-pwidth)
                    px+=1;
            }
            if (ismovingleft==true){
                if(px>0)
                    px-=1;
            }
            // move the bullets and remove them if they get above the screen
            for(int i=0 ;i<32;i++){
                if(bullet[i][0]==1){
                    bullet[i][2]+=bullet[i][3];
                    if(bullet[i][2]<0){
                        bullet[i][0]=0;
                    }
                }
            }
            // Collision with the bullets and the aliens.
            for(int i=0; i<32;i++){
                if(bullet[i][0]==1){
                for (int ii=0;ii<10*5;ii++){
                if (alien[ii][0]==1){
                    Rectangle rec1 = new Rectangle((int)bullet[i][1],(int)bullet[i][2],16,16);
                    Rectangle rec2 = new Rectangle((int)alien[ii][1],(int)alien[ii][2],16,16);
                    if(rec1.intersects(rec2)){
                        //g.drawString("Filled Rectangle Collision", 50, 60 );
                        bullet[i][0]=0;
                        alien[ii][0]=0;
                    }
                }
                }
                }
            }
            // Move the alien wave
            for(int i=0;i<10*5;i++){
                if(alien[i][0]==1){
                    if(wavedirection==0){
                        alien[i][1]-=0.1;
                    }else{
                        alien[i][1]+=0.1;
                    }
                    if(alien[i][1]>getSize().width-16){
                        wavedirection=0;
                    }
                    if(alien[i][1]<0){
                        wavedirection=1;
                    }
                }
            }
            repaint();
            try {
                Thread.sleep(16);
                }
                catch (InterruptedException e) {
                }
        }
    }

    public void update(Graphics g){
        bufferGraphics.clearRect(0,0,getSize().width,getSize().width);
        bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Space Invaders Example.",20,30);
        // draw player ship
         bufferGraphics.setColor(Color.white);
        bufferGraphics.fillRect((int)px,(int)py,pwidth,pheight);
        //draw aliens
        for(int i=0;i<10*5;i++){
            if(alien[i][0]==1){
                bufferGraphics.fillRect((int)alien[i][1],(int)alien[i][2],16,16);
            }
        }
        // Draw bullets
        for(int i=0;i<32;i++){
            if(bullet[i][0]==1){
                bufferGraphics.fillRect((int)bullet[i][1],(int)bullet[i][2],3,3);
            }
        }
         g.drawImage(offscreen,0,0,this);
    }

        public void createBullet(){
            for(int i=0;i<32;i++){
                if(bullet[i][0]==0){
                    bullet[i][0]=1;
                    bullet[i][1]=px+8;
                    bullet[i][2]=py-16;
                    bullet[i][3]=-2;
                    return;
                }
            }
        }

         public boolean keyDown (Event e, int key){
          if( key == Event.LEFT )
        {
            ismovingleft = true;
        }
        if(key==Event.RIGHT)
        {
             ismovingright = true;
        }

         if( key == 32 ) // space bar for jump
         {
            if(firedelay<System.currentTimeMillis()){
                   firedelay = System.currentTimeMillis() + 200;
                   createBullet();
            }
         }

        System.out.println (" Integer Value: " + key);

         return true;
     }

    public boolean keyUp (Event e, int key){
          if( key == Event.LEFT )
        {
             ismovingleft = false;
        }
        if( key == Event.RIGHT )
        {
             ismovingright = false;
        }

        return true;
    }


}

zondag 27 oktober 2013

Jcreator Le does not work on Windows 8 and other.

I have seen that the news section on the Jcreator website has not been updated since 2012. I think that the program ends there. It does not work on Windows 8 (Jcreator forum post metioned it)

I think I will have to find a new program to edit and compile Java Applets with when I get Windows 8.
notepad++ I have been using for HTML5 and it works ok so I will be trying that. Jcreator is an easy and good editor and compiler and this one I will use for as long as I can.

I have been thinking of what examples to create next for the blog. I will mention the ideas and thoughts on my Twitter.

maandag 3 juni 2013

Setting the drawing color


Below is shown how to set the drawing color to a red green and blue value. This is done by using new Color folowed by the rgb colors.

I am now posting the code directly and not a applet with it because that does not work anymore with google sites(with me anyway)

import java.awt.*;
import java.applet.*;

public class testproject extends Applet implements Runnable {
    // Graphics for double buffering.
    Graphics    bufferGraphics;
    Image        offscreen;
    
    public void init() {
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();
        new Thread(this).start();
    }

    public void run(){
        for(;;){
            repaint();
            try{
                Thread.sleep(10);
            }
            catch (InterruptedException e){    
            }    
        }
    }
    
    public void update(Graphics g){
        bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
        // Here the color is set using new Color followed by
        // the rgb values. In this case 200,0,0
        bufferGraphics.setColor(new Color(200,0,0));
        bufferGraphics.drawString("Test",10,10);
        
        g.drawImage(offscreen,0,0,this);
        
    }

}

Jcreator is no longer free I think

For about 10 years I have been using Jcreator to program and view java sourcecode. Today I saw that there was a new version out and I downloaded it and installed it. I should not have done this because the free version now is a trail version. There was a message that there were only 30 days available.
I tried looking around and saw on the softpedia site that it was free but it downloads the same trail version. I put a comment there that has to be aproved first. In that I wrote that it was not the free version.

I had to look back into my archives and now have a 2004 version of jcreator that works.

I am thinking of picking up java again. Though I may not stick to using it. I am viewing java code atm. Maybe I will post new source code on the blog again.

dinsdag 19 juni 2012

I have decided to stop programming in Java

Since the applets no longer work. I have decided to stop programming in Java. I never really gotten used to Java.

I will keep programming but in another language. I was thinking of buying Play Basic but for the moment I do  my programming exercises in Blitz Basic.

(edit:)
I think the google site is returning errorous data from the applets. Maybe this will go away.