I want to make a Java program that opens a window, and draws a person moving around on a background, controlled by the person, but I am yet to learn how to open an independent window, or how to draw on that window. Can I get some help with this? I have started with the codeacademy tutorials, but they, so far, have not yielded results. What I do know, is that I need to import a swing class, but I know nothing from there.

In the end, I would like my main class to look something like this:


Code:

//Header{
newWindow mainWindow = new Window;
newPerson player = new Player;
boolean win = false;
While (win == false) {
/*
I want to read keys here, if anyone knows how to do that.
*/
Case (keyPressed) {
 case = 'up'{player.moveV(1);} /break
 case = 'down'{player.moveV(-1);} /break
 case = 'left' {player.moveH(-1)};} /break
 case = 'right' {player.moveH(1)};} /break
}
}
}


My other classes will look like this:


Code:

class newPerson{

public int moveH(int locationY, int move){

//erase prevous location;
locationY+move=locationY
//draw person @ (locationX, locationY)
return locationY

}

}

and same for x[/code]
I personally would try to put it all into one class, as it can be easier to share variables.
_iPhoenix_ wrote:
I personally would try to put it all into one class, as it can be easier to share variables.


Sorry, but that has to be one of the lamest excuses out there for cramming everything into one class. Java is an object oriented language! You want to keep all your different game behaviors in separate classes.

Your first task is to find a suitable library. Java has notoriously terrible built-in libraries. I hear LibGDX is the de facto standard nowadays, but back then I tried Slick2D. I recommend LibGDX, but only because I have bad memories associated with Slick2D (it's problematic and outdated).

Your second task is to learn that library. There is usually a tutorial on your first LibGDX game. The great thing about LibGDX is that despite its relatively complex configuration, it is possible to compile your code for desktop, web, and Android simultaneously.

Finally, you can start working on whatever you'd like. I am personally terrible at making games and working with the rather complex structure that games demand. Game development requires the application of all of a programmer's knowledge and skills, from configuring toolchains to understanding when to use certain programming paradigms to calculating cross products and transformation matrices.

But most of all, you will require experience. Rest assured you will not finish your first game, or your second or third. That's completely fine; most projects never take off. But eventually there will come a time where a project takes off and gets an audience of its own. This is what all game developers aspire to achieve.

Yes, I understand that I totally sidestepped your question. But before you can ask these questions, from the looks of your Java code, you need to refine your style and understand what you're doing before you take big steps.

If you just want to make a game and forget about everything else, I highly recommend Python with Pygame; I do believe this is more suitable for you. You can use my game as an example (start with main.py).

Thank you for reaching out.
oldmud0 wrote:
_iPhoenix_ wrote:
I personally would try to put it all into one class, as it can be easier to share variables.


Sorry, but that has to be one of the lamest excuses out there for cramming everything into one class. Java is an object oriented language! You want to keep all your different game behaviors in separate classes.

Your first task is to find a suitable library. Java has notoriously terrible built-in libraries. I hear LibGDX is the de facto standard nowadays, but back then I tried Slick2D. I recommend LibGDX, but only because I have bad memories associated with Slick2D (it's problematic and outdated).

Your second task is to learn that library. There is usually a tutorial on your first LibGDX game. The great thing about LibGDX is that despite its relatively complex configuration, it is possible to compile your code for desktop, web, and Android simultaneously.

Finally, you can start working on whatever you'd like. I am personally terrible at making games and working with the rather complex structure that games demand. Game development requires the application of all of a programmer's knowledge and skills, from configuring toolchains to understanding when to use certain programming paradigms to calculating cross products and transformation matrices.

But most of all, you will require experience. Rest assured you will not finish your first game, or your second or third. That's completely fine; most projects never take off. But eventually there will come a time where a project takes off and gets an audience of its own. This is what all game developers aspire to achieve.

Yes, I understand that I totally sidestepped your question. But before you can ask these questions, from the looks of your Java code, you need to refine your style and understand what you're doing before you take big steps.

If you just want to make a game and forget about everything else, I highly recommend Python with Pygame; I do believe this is more suitable for you. You can use my game as an example (start with main.py).

Thank you for reaching out.


What can be refined?
Copy and pasted from another post by me:
This code can be used like a template. Currently it just draws a magenta square at the location of your mouse.
[spoiler]

Code:



/**
 *
 * Copyright @German Kuznetsov
 */

package your.package;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;

/**
 *
 * some code ripped from
 * http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418
 */
public class Main {

    private static JFrame frame; //Lets define some stuff we will be using
    private static Canvas canvas;
    private static BufferStrategy buffer;

    public static final int WIDTH = 600; //Our window size
    public static final int HEIGHT = 400;

    public static int MouseX, MouseY; //These variables will hold values for the mouse.
    public static boolean LeftMouseButtonDown, MouseWheelDown, RightMouseButtonDown;

    public static final boolean[] keyDown = new boolean[65536]; //This list will hold the position of keys (up/down)

    public static void main(String[] args) {
        //create our window
        frame = new JFrame();
        frame.setIgnoreRepaint(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //create our canvas to draw on
        canvas = new Canvas();
        canvas.setIgnoreRepaint(true);
        canvas.setSize(WIDTH, HEIGHT);

        //pack our canvas into our frame
        frame.add(canvas);
        frame.pack();
        frame.setVisible(true);

        //add a mouselistener to our window (mouse button pressed / released)
        canvas.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
                switch (e.getButton()) {
                    case 1:
                        LeftMouseButtonDown = true;
                        break;
                    case 2:
                        MouseWheelDown = true;
                        break;
                    case 3:
                        RightMouseButtonDown = true;
                        break;
                }
            }

            public void mouseReleased(MouseEvent e) {
                switch (e.getButton()) {
                    case 1:
                        LeftMouseButtonDown = true;
                        break;
                    case 2:
                        MouseWheelDown = true;
                        break;
                    case 3:
                        RightMouseButtonDown = true;
                        break;
                }
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }

        });
       // //add a mouse motion listener to our window (Mouse moved)
        canvas.addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseDragged(MouseEvent e) {
                MouseX = e.getX();
                MouseY = e.getY();
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                MouseX = e.getX();
                MouseY = e.getY();
            }
        });

       //add Key Listener (Key pressed/ released)
        canvas.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent ke) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                keyDown[e.getKeyCode()] = true;
            }

            @Override
            public void keyReleased(KeyEvent e) {
                keyDown[e.getKeyCode()] = false;
            }
        });
//buffer setup
        canvas.createBufferStrategy(2);
        buffer = canvas.getBufferStrategy();

      //our game loop
        Graphics graphics = null;
        while (true) {
            try {
                update();
                render(graphics);
                if (!buffer.contentsLost()) {
                    buffer.show();
                }
            } finally {
                if (graphics != null) {
                    graphics.dispose();
                }

            }

        }
    }

   //RENDER YOUR STUFF HERE
    public static void render(Graphics graphics) {
        graphics = buffer.getDrawGraphics(); //Clears back buffer

        graphics.setColor(Color.BLACK);
        graphics.fillRect(0, 0, WIDTH, HEIGHT);
        graphics.setColor(new Color(255, 0, 255));
        graphics.fillRect(MouseX, MouseY, 5, 5);
    }

    public static void update() {

    }
}

[/spoiler]
oldmud0 wrote:
_iPhoenix_ wrote:
I personally would try to put it all into one class, as it can be easier to share variables.


Sorry, but that has to be one of the lamest excuses out there for cramming everything into one class. Java is an object oriented language! You want to keep all your different game behaviors in separate classes.

Your first task is to find a suitable library. Java has notoriously terrible built-in libraries. I hear LibGDX is the de facto standard nowadays, but back then I tried Slick2D. I recommend LibGDX, but only because I have bad memories associated with Slick2D (it's problematic and outdated).

Your second task is to learn that library. There is usually a tutorial on your first LibGDX game. The great thing about LibGDX is that despite its relatively complex configuration, it is possible to compile your code for desktop, web, and Android simultaneously.

Finally, you can start working on whatever you'd like. I am personally terrible at making games and working with the rather complex structure that games demand. Game development requires the application of all of a programmer's knowledge and skills, from configuring toolchains to understanding when to use certain programming paradigms to calculating cross products and transformation matrices.

But most of all, you will require experience. Rest assured you will not finish your first game, or your second or third. That's completely fine; most projects never take off. But eventually there will come a time where a project takes off and gets an audience of its own. This is what all game developers aspire to achieve.

Yes, I understand that I totally sidestepped your question. But before you can ask these questions, from the looks of your Java code, you need to refine your style and understand what you're doing before you take big steps.

If you just want to make a game and forget about everything else, I highly recommend Python with Pygame; I do believe this is more suitable for you. You can use my game as an example (start with main.py).

Thank you for reaching out.


Yes, it is a lame excuse, but when I first started that's what I would do. It helps you understand standard programming practices. If one is writing a decent sized game, one should use classes like so. I have also found that having too many unnessary classes can significantly slow programs down. (Basically, I wrote a program with a TON of classes and class refrences and wrote one without it. Yes, I over did it, but I was testing a theory)
_iPhoenix_ wrote:
Yes, it is a lame excuse, but when I first started that's what I would do. It helps you understand standard programming practices. If one is writing a decent sized game, one should use classes like so. I have also found that having too many unnessary classes can significantly slow programs down. (Basically, I wrote a program with a TON of classes and class refrences and wrote one without it. Yes, I over did it, but I was testing a theory)


It depends on how you apply classes. A correct application has minimal overhead/footprint and outweighs the organizational chaos that ensues with a one-class program after a couple of weeks.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement