Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Something I Made In Java (UPDATED)


viciousdead
 Share

Recommended Posts

@Sealbreaker:

> Why would you post something like this to the public?? It's like posting "Look! I learned how to calculate 1+1 !!!"  What should we say about something that isn't a serious attempt in any direction?
>
> -seal

Whatever you feel like saying. o.o

EDIT: And you are right, I did learn 1+1!  :D
Link to comment
Share on other sites

hm… i always used a graphics2d object with bufferedimages, it isn't hard at all and you can do some shit with it (rotation, etc.)
anyhow, for double-buffering you just have a bufferedimage (or whatever image-type you are using) with the size of the screen that you draw on instead of directly drawing to the screen. Then, in your repaint-method you just draw that 1 image which contains all the stuff you've drawn to it... You can look up some tutorials, shoulnd't be too hard to figure that out.

-seal
Link to comment
Share on other sites

@Sealbreaker:

> hm… i always used a graphics2d object with bufferedimages, it isn't hard at all and you can do some shit with it (rotation, etc.)
> anyhow, for double-buffering you just have a bufferedimage (or whatever image-type you are using) with the size of the screen that you draw on instead of directly drawing to the screen. Then, in your repaint-method you just draw that 1 image which contains all the stuff you've drawn to it... You can look up some tutorials, shoulnd't be too hard to figure that out.
>
> -seal

Okay, I'll do that. Thanks.

EDIT:Double buffering makes it laggy(It's redrawing entire screen, off screen, constantly).. No idea how to reduce lag except make it smaller, lol..  ;)
Link to comment
Share on other sites

@ViciousDead:

> Okay, I'll do that. Thanks.
>
> EDIT:Double buffering makes it laggy(It's redrawing entire screen, off screen, constantly).. No idea how to reduce lag except make it smaller, lol..  ;)

You're doing it wrong. Post code, no one will steal.

@Sealbreaker:

> Then, in your repaint-method

duck Repaint
Link to comment
Share on other sites

Posting it, but I would like if no one made fun of my horrid coding skills or my random //descriptions. <3

*You need to resize the applet via HTML.

Player(Sub, Stores Player Variables):
```
import java.awt.*;

public class Player
{
  public int radius = 50; // Circle Size
  public int x = 50; // X Value
  public int y = 90; // Y Value
  public int spd = 10; // Speed Of Circle
  public double hp = 2000; // Health Value

  // Special Damage And Range
  public int sp1_range = 150; // Special One's Range
  public double sp1_dmg = 1.5; // Special One's Damage

  public boolean space; // Pressed Or Not
  public boolean left; // Pressed Or Not
  public boolean right; // Pressed Or Not
  public boolean up; // Pressed Or Not
  public boolean down; // Pressed Or Not
}
```
Mob1(Sub, Stores AI Variables):
```
import java.awt.*;

public class Mob1
{
  public int radius = 30; // Circle Size
  public int x = 550; // X Value
  public int y = 550; // Y Value
  public int spd = 2; // Speed Of Circle
  public double hp = 1000; // Health Value

  public boolean space; // Pressed Or Not
  public boolean left; // Pressed Or Not
  public boolean right; // Pressed Or Not
  public boolean up; // Pressed Or Not
  public boolean down; // Pressed Or Not
}
```
GridKeyListener(KeyEventHandler, ignore the name from an old grid thing I was doing..):
```
import java.awt.event.*;

public class GridKeyListener implements KeyListener
{
  boolean space = false;
  boolean left = false;
  boolean right = false;
  boolean up = false;
  boolean down = false;

  public void keyPressed(KeyEvent e)
  {
    switch (e.getKeyCode())
    {
      case KeyEvent.VK_SPACE:
        space = true;
        break;
      case KeyEvent.VK_LEFT:
        left = true;
        break;
      case KeyEvent.VK_RIGHT:
        right = true;
        break;
      case KeyEvent.VK_UP:
        up = true;
        break;
      case KeyEvent.VK_DOWN:
        down = true;
    }
  }

  public void keyReleased(KeyEvent e)
  {
    switch (e.getKeyCode())
    {
      case KeyEvent.VK_SPACE:
        space = false;
        break;
      case KeyEvent.VK_LEFT:
        left = false;
        break;
      case KeyEvent.VK_RIGHT:
        right = false;
        break;
      case KeyEvent.VK_UP:
        up = false;
        break;
      case KeyEvent.VK_DOWN:
        down = false;
    }
  }

  public void keyTyped(KeyEvent e){}

  public boolean getSpace(){return space;}
  public boolean getLeft(){return left;}
  public boolean getRight(){return right;}
  public boolean getUp(){return up;}
  public boolean getDown(){return down;}

}
```
Game(Main, basically everything is done here..):
```
import java.applet.*;
import java.awt.*;

public class Game extends Applet implements Runnable
{
  // Double Buffer
  Graphics bufferGraphics;
  Image offscreen;
  Dimension dim;

  // Creates Communication Between Other Classes
  public GridKeyListener gkl = new GridKeyListener();
  public Player p1 = new Player();
  public Mob1 m1 = new Mob1();

  // Define Images
  private Image spaceAtk;

  // First Operation
  public void init()
  {
    // Sets Background Color
    setBackground (Color.black);

    // Double Buffer
    dim = getSize();
    offscreen = createImage(dim.width,dim.height);
    bufferGraphics = offscreen.getGraphics();
  }

  // Start The Program
  public void start()
  {
    // Defines And Starts Thread; Adds KeyListener
    Thread th = new Thread (this);
    this.addKeyListener(gkl);
    th.start ();
  }

  public void stop(){}

  public void destroy(){}

  public void run()
  {
    // Priority Level
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while (true)
    {
      // Refresh
      repaint();
      try
      {
        // Stops Thread For 20 Milliseconds
        Thread.sleep (20);
      }
      catch (InterruptedException ex)
      {

      }
      // Priority Level
      Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    }
  }

  public void paint (Graphics g)
  { 
    // Double Buffer
    bufferGraphics.clearRect(0,0,dim.width,dim.width);

    // Load Pictures
    spaceAtk = getImage(getCodeBase(), "test.png");

    // Check Directions - Player
    p1.space = gkl.getSpace();
    p1.left = gkl.getLeft();
    p1.right = gkl.getRight();
    p1.up = gkl.getUp();
    p1.down = gkl.getDown();

    // Check Directions - AI
    if(m1.x > p1.x){m1.left = true;} else {m1.left = false;}
    if(m1.x < p1.x){m1.right = true;} else {m1.right = false;}
    if(m1.y > p1.y){m1.up = true;} else {m1.up = false;}
    if(m1.y < p1.y){m1.down = true;} else {m1.down = false;}

    // Boundaries - Player
    if(p1.y <= 90){p1.up = false;}
    if(p1.y >= 600){p1.down = false;}
    if(p1.x <= 50){p1.left = false;}
    if(p1.x >= 900){p1.right = false;}

    // Boundaries - AI
    if(m1.y <= 90){m1.up = false;}
    if(m1.y >= 600){m1.down = false;}
    if(m1.x <= 50){m1.left = false;}
    if(m1.x >= 900){m1.right = false;}

    // Platforms - Player
    if(p1.x <= 750 && p1.y == 190 - p1.radius){p1.down = false;}
    if(p1.x <= 750 && p1.y == 200){p1.up = false;}
    if(p1.x >= 150 && p1.y == 290 - p1.radius){p1.down = false;}
    if(p1.x >= 150 && p1.y == 300){p1.up = false;}

    // Platforms - AI
    if(m1.x <= 750 && m1.y == 190 - m1.radius){m1.down = false;}
    if(m1.x <= 750 && m1.y == 200){m1.up = false;}
    if(m1.x >= 150 && m1.y == 290 - m1.radius){m1.down = false;}
    if(m1.x >= 150 && m1.y == 300){m1.up = false;}

    // Draws Circle
    if(p1.hp>0)
    {
      bufferGraphics.setColor (Color.red);
      bufferGraphics.fillOval (p1.x - p1.radius, p1.y - p1.radius, 2 * p1.radius, 2 * p1.radius);
    }
    if(m1.hp>0)
    {
      bufferGraphics.setColor (Color.green);
      bufferGraphics.fillOval (m1.x - m1.radius, m1.y - m1.radius, 2 * m1.radius, 2 * m1.radius);
    }

    // Movement - Player
    if(p1.left){p1.x -= p1.spd;}
    if(p1.right){p1.x += p1.spd;}
    if(p1.up){p1.y -= p1.spd;}
    if(p1.down){p1.y += p1.spd;}

    // Movement - AI 
    if(m1.left){m1.x -= m1.spd;}
    if(m1.right){m1.x += m1.spd;}
    if(m1.up){m1.y -= m1.spd;}
    if(m1.down){m1.y += m1.spd;}

    // Special Attacks
    if(p1.hp>0 && p1.space){bufferGraphics.drawImage (spaceAtk, p1.x-2 * p1.radius, p1.y - 2 * p1.radius, this);}

    // Damage Code
    if(p1.hp>0 && (m1.x - m1.radius <= p1.x + p1.sp1_range) && (m1.y - m1.radius <= p1.y + p1.sp1_range) && (m1.x + m1.radius >= p1.x - p1.sp1_range) && (m1.y + m1.radius >= p1.y - p1.sp1_range)){
      if(p1.space){
        m1.hp -= p1.sp1_dmg;
      }
    }

    if((m1.hp > 0) && (p1.x - p1.radius <= m1.x) && (p1.y - p1.radius <= m1.y) && (p1.x + p1.radius >= m1.x) && (p1.y + p1.radius >= m1.y)){
      p1.hp -= 0.5;
    }

    // Fix Negative Health
    if(p1.hp<0){p1.hp = 0;}
    if(m1.hp<0){m1.hp = 0;}

    // Draws Text 
    String s_msg = "Space: " + p1.space;
    String r_msg = "Right: " + p1.right;
    String l_msg = "Left: " + p1.left;
    String u_msg = "Up: " + p1.up;
    String d_msg = "Down: " + p1.down;
    String hp_msg = "Health: " + p1.hp;
    String ehp_msg = "Enemy Health: " + m1.hp;
    String up_msg = "^";
    String down_msg = "v";
    String left_msg = "<";
    String right_msg = ">";
    Font small = new Font("Helvetica", Font.BOLD, 32);
    FontMetrics metr = this.getFontMetrics(small);

    bufferGraphics.setColor(Color.cyan);
    bufferGraphics.setFont(small);
    bufferGraphics.drawString(hp_msg, (290 - metr.stringWidth(hp_msg)), 500);
    bufferGraphics.drawString(ehp_msg, (400 - metr.stringWidth(ehp_msg)), 560);
    bufferGraphics.drawString(s_msg, (260 - metr.stringWidth(s_msg)), 620);
    bufferGraphics.setColor(Color.white);
    bufferGraphics.drawString(right_msg, (100 - metr.stringWidth(right_msg)), 80);
    bufferGraphics.drawString(right_msg, (300 - metr.stringWidth(right_msg)), 80);
    bufferGraphics.drawString(right_msg, (500 - metr.stringWidth(right_msg)), 80);
    bufferGraphics.drawString(right_msg, (700 - metr.stringWidth(right_msg)), 80);
    bufferGraphics.drawString(down_msg, (900 - metr.stringWidth(down_msg)), 80);
    bufferGraphics.drawString(left_msg, (900 - metr.stringWidth(left_msg)), 240);
    bufferGraphics.drawString(left_msg, (700 - metr.stringWidth(left_msg)), 240);
    bufferGraphics.drawString(left_msg, (500 - metr.stringWidth(left_msg)), 240);
    bufferGraphics.drawString(left_msg, (300 - metr.stringWidth(left_msg)), 240);
    bufferGraphics.drawString(down_msg, (100 - metr.stringWidth(down_msg)), 240);

    // Draw Entire Double Buffered Image
    g.drawImage(offscreen,0,0,this);
  }

  public void update(Graphics g)
  {
    paint(g);
  }
}

```
Link to comment
Share on other sites

```
  // Priority Level
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);  // Why are you doing this?
    while (true)                          // I would use a boolean-variable that you can switch to false in order
    {                                        // to exit the program in a clean way
      // Refresh
      repaint();                          // when double-buffering i tend to use 2 methods - render (my own) &
      try                                    // repaint to only draw the offscreen-image. So you would just have
      {                                      // to care for what to render in the render-method...
        // Stops Thread For 20 Milliseconds
        Thread.sleep (20);          // you should implement fps. You measure how much time has passed
      }                                    // to do all the stuff and then let the thread sleep for the rest of the time
      catch (InterruptedException ex)  // that it should have taken in order to fulfill a certain amount of
      {                                              // frames per second.

      }
      // Priority Level
      Thread.currentThread().setPriority(Thread.MAX_PRIORITY);  // why do you do this?
    }
```
```
  // Load Pictures
    spaceAtk = getImage(getCodeBase(), "test.png"); // You don't need to load the image every time
you render the screen... this may cost you alot of fps and is totally unnecessary. If you use the
Image all the time you can just load it in the initiation of your program.
```
Hope this helped a bit for the start,

-seal
Link to comment
Share on other sites

@Sealbreaker:

> ```
>       // Priority Level
>       Thread.currentThread().setPriority(Thread.MAX_PRIORITY);  // why do you do this?
>    
> ```

I'm not really sure, I was told it was neccesary for the thread to run(by a java forum), but I just removed it and it worked fine… I'll try to add fps, thanks for the help.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...