Welcome to Abyssal Gaming Network

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

[[Template core/front/global/updateWarning is throwing an error. This theme may be out of date. Run the support tool in the AdminCP to restore the default theme.]]
Drak

Basic Java - Simple Mistakes (Java coders needed!)

4 posts in this topic

Ohai there.

I recently have gotten into writing simple Java programs (i.e. calenders, ping-pong, and a semi-complex JDK compiler program [does the job of javac, but faster and has a GUI]), and my latest program is a complex Java game.

The game's purpose is this: You're a chicken who has lost its eggs, and you must find your way through a 2D world to find them. Gold coins can be collected for points, and there are monsters such as zombies (chicken-eating zombies) and pythons (Haven't gotten to either yet... Might need help. *wink*).

So far, I've got the basic GUI set up, and I'm almost ready to work on sprites. I'm having one problem, though, on lines 75-81. Java's having troubles converting the int flag into a boolean. *sadface.png*

Here's my main class file (the GUI and startup screen):

package com.chicken;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Graphics2D;

import javax.swing.JFrame;

/**

* This is the main game class

* @author = Drak1234

* Note from Drak:

* Thanks for trying my first game ever!

* :o

* ~Made with love~

*

*/

public class Chicken extends JFrame implements Runnable {

private boolean running = false;

/* double buffering */

private Image dbImage = null;

private Graphics dbgx;

private Graphics2D dbg;

final Font font = new Font("Arial", 16, 16);

private boolean screenIndex = 0; //0- menu, 1- game, 2- game over

/* Screens for the main menu, ingame, and game over */

enum Screen {

MENU, GAME, GAME_OVER

}

/**

* The constructor

*/

public Chicken() {

super("Chicken");

setSize(640, 480);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setResizable(false);

setLocation(200, 200);

(new Thread(this)).start();

setVisible(true);

}

public void update(Graphics g) {

paint(g);

}

/**

* Used to draw the screen

* @param g - the graphics object

*/

public void paint(Graphics g) {

if (dbImage == null) {

dbImage = createImage(60, 480);

dbgx = dbImage.getGraphics();

dbg = (Graphics2D) dbgx;

}

dbg.setColor(Color.WHITE);

dbg.fillRect(0, 0, 640, 480);

switch (screenIndex) {

case 0:

dbg.getFont();

dbg.setColor(Color.BLACK);

dbg.drawString("Press [ENTER] to begin!", 250, 250);

break;

}

g.drawImage(dbImage, 0, 0, null);

}

/**

* The main method

* @params args - the command line args

*/

public static void main(String[] args) {

new Chicken();

}

/**

* The run method - called upon a new thread startup

*/

public void run() {

running = true;

while (running) {

process();

repaint();

try {

Thread.sleep(20);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public void process() {

}

public int isScreenIndex() {

return screenIndex;

}

public void setScreenIndex(int screenIndex) {

this.screenIndex = screenIndex;

}

}

/*

* Please do not use my source code (freely distributed) and claim it as your own.

* Hunt - The Lost Eggs was created under the Creative Commons license.

* Feel free to modify the source code! It's here for a reason. ;D

* Contact drak.of.minecraftia@gmail.com for more info, or if you wish to help him on his latest Java projects!

* I'm still learning Java code, so if you are too, let's learn together!

*/

Ignore the copyright annotations. Those are for when I finish this project and package it up into a nice little application. I'll most likely post a link on this thread, so keep an eye out for updates if you're interested! ;)

Share this post


Link to post
Share on other sites

Primatives int and boolean are not compatible types. If you are set on converting an int into a boolean I would set it up in an if statement.

ex. if(flag==0){ bool = true;}

else{ bool= false;}

If there are more than 2 states I would just nested ifs to make it do the correct command or a switch statement.

Ostrad

Share this post


Link to post
Share on other sites

Alright, I fixed that issue. Apparently it preceded all the way through to dbg.getFont(setVar); and setGraphics2D(dbgx); flags.

My next problem is that the GUI fails to display any content. The basic frame is showing up just fine, which is good, but the imagery that was generated isn't being read...

Any ideas?

Share this post


Link to post
Share on other sites

even tho this is kinda late... i still feel like mentioning a few things^^

starting with the spagetti code... I'd split your class into model, view and controller classes for easier debugging (plus it looks better)

also you should use get/set methods to access the variables

third thing would be the fact that you're overwriting the paint and updade methods of your jframe without using the parent's methods (super.paint, super.update)

hope this helps at least a little bit :)

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now