Dreamscape

I’ll just do that then, since I don’t see errors now I’ll just wait until I find one. I haven’t used swing enough to agree or disagree with what you’ve said, but I do think the large amount of code being executed has something to do with it either way. ^.^

The equivalent (not really) of global variables is static variables.
But you have to put the classes name in front like this:


AClass.AVariable

Static variables are used when using Static classes - they are values of the class and not of any specific class. The best explanation would just be that you put the class name because they don’t belong to any (specific) object. Static variables are used like globals in some cases because of the accessibility in many places - as Wikipedia defines it accessible in every scope. Good discussion here: http://www.artima.com/forums/flat.jsp?forum=1&thread=39370

Even static classes have a separate instance per-classloader. This is way way out of the scope of the OP’s design questions though, and “stay away from globals” is still a good guiding principle to go by.

As with all rules of thumb, there may be exceptions, but if you’re experienced enough to know when to break the rule, you didn’t have to ask the question.

I don’t see when I’d ever need to break the rule now that I have everything working as intended without them.

How did you get along with Swing when your Java skills are that basic?
Just wondering

Because it’s not that difficult. If I use something I remember it and if I don’t I forget it, so they’re only basic in the areas that I don’t use.

Swing is very easy, I’d say one of the easiest parts of Java. Very self explanatory - import javax.swing.*, make a frame, set the size, make it visible. Other subjects like IO are more difficult to learn imo because you have to choose between byte streams, object streams, etc.

I know it’s easy, but it’s very OOP, that’s why I was wondering…

Thanks to a random post I was reading on this forum I figured out how to make a game loop, after that I searched on google for a way to change the code that I already had written for an applet project from my CS class so that it would work in this program. It works, but I ran into a ‘freeze’ effect which I kinda-sorta know the source of but I’m not sure how to fix it.

If anyone has a sec to help me figure this out then here is the explanation or something like that:

I have a game loop that, just for testing purposes, prints as message every time the thread runs. The code for the game loop is as follows:

class TestRender implements Runnable
{
	private JPanel gamePanel;
	Thread thread = new Thread();
	
	public TestRender(JPanel gamePanel)
	{
		this.gamePanel = gamePanel;
	}
	
	public void startThread()
	{
		thread.start();
	}
	
	public void run()
	{
		Initialize(); //From what I can gather, this will run the below Initialize method when this method is run. I'll figure out the rest later.
		
		while(true)
		{
			try
			{
				Thread.sleep(1000);
				System.out.println("Test Successful, sleeping for 1000 milliseconds.");
			}
			
			catch(Exception e) //No idea what this does, a few of the examples I saw had it so I included it just in case.
			{
				e.printStackTrace();
			}
		}
	}
	
	private void Initialize()
	{
	}
}

The game loop is ‘turned on’ after the user finishes creating a character and clicks the ‘Continue’ button. The code for the continue button is:

continueButton.addActionListener(new ActionListener() {  //Action listener for the continueButton
			public void actionPerformed(ActionEvent e)
			{
				if (tempPointsToSpend == 0 && characterNameTextField.getText().trim().isEmpty() == false) //Checks if the user has used all of the spendable points and if the user has entered a name.
				{
					PlayerCharacter characterReference = new PlayerCharacter(tempStr, tempAgil, tempDex, tempInt, tempCon, tempChar, tempSp, tempHp, tempXp, tempHealth, tempMana, tempGold);
					
					characterReference.strSetter(tempStr);
					characterReference.agilSetter(tempAgil);
					characterReference.dexSetter(tempDex);
					characterReference.intSetter(tempInt);
					characterReference.conSetter(tempCon);
					characterReference.charSetter(tempChar);
					characterReference.spwrSetter(tempSp);
					characterReference.hpwrSetter(tempHp);
					characterReference.healthSetter(tempHealth);
					characterReference.manaSetter(tempMana);
					characterReference.nameSetter(characterNameTextField.getText());
					characterReference.alignmentSetter(alignmentComboBox.getSelectedItem().toString());
					
					System.out.println(""+characterReference.strGetter()+"");
					System.out.println(""+characterReference.agilGetter()+"");
					System.out.println(""+characterReference.dexGetter()+"");
					System.out.println(""+characterReference.intGetter()+"");
					System.out.println(""+characterReference.conGetter()+"");
					System.out.println(""+characterReference.charGetter()+"");
					System.out.println(""+characterReference.spwrGetter()+"");
					System.out.println(""+characterReference.hpwrGetter()+"");
					System.out.println(""+characterReference.healthGetter()+"");
					System.out.println(""+characterReference.manaGetter()+"");
					System.out.println(""+characterReference.nameGetter()+"");
					System.out.println(""+characterReference.alignmentGetter()+"");
					
					
					gamePanel.remove(cancelButton); //Remove specified component from the JPanel.
					gamePanel.remove(continueButton);
					gamePanel.remove(strAddButton);
					gamePanel.remove(agilAddButton);
					gamePanel.remove(dexAddButton);
					gamePanel.remove(intAddButton);
					gamePanel.remove(conAddButton);
					gamePanel.remove(charAddButton);
					gamePanel.remove(spAddButton);
					gamePanel.remove(hpAddButton);
					gamePanel.remove(strMinusButton);
					gamePanel.remove(agilMinusButton);
					gamePanel.remove(dexMinusButton);
					gamePanel.remove(intMinusButton);
					gamePanel.remove(conMinusButton);
					gamePanel.remove(charMinusButton);
					gamePanel.remove(spMinusButton);
					gamePanel.remove(hpMinusButton);
					gamePanel.remove(strAttributeLabel1);
					gamePanel.remove(agilAttributeLabel1);
					gamePanel.remove(dexAttributeLabel1);
					gamePanel.remove(intAttributeLabel1);
					gamePanel.remove(conAttributeLabel1);
					gamePanel.remove(charAttributeLabel1);
					gamePanel.remove(spAttributeLabel1);
					gamePanel.remove(hpAttributeLabel1);
					gamePanel.remove(strAttributeLabel2);
					gamePanel.remove(agilAttributeLabel2);
					gamePanel.remove(dexAttributeLabel2);
					gamePanel.remove(intAttributeLabel2);
					gamePanel.remove(conAttributeLabel2);
					gamePanel.remove(charAttributeLabel2);
					gamePanel.remove(spAttributeLabel2);
					gamePanel.remove(hpAttributeLabel2);
					gamePanel.remove(pointsToSpendLabel1);
					gamePanel.remove(pointsToSpendLabel2);
					gamePanel.remove(pointsToSpendLabel3);
					gamePanel.remove(characterNameLabel1);
					gamePanel.remove(characterNameLabel2);
					gamePanel.remove(characterNameTextField);
					gamePanel.remove(alignmentComboBox);
					gamePanel.remove(characterAlignmentLabel1);
					gamePanel.remove(characterAlignmentLabel2);
					gamePanel.remove(classTypeComboBox);
					gamePanel.remove(characterClassTypeLabel1);
					gamePanel.remove(characterClassTypeLabel2);
					gamePanel.remove(characterManaLabel1);
					gamePanel.remove(characterManaLabel2);
					gamePanel.remove(characterManaLabel3);
					gamePanel.remove(characterHealthLabel1);
					gamePanel.remove(characterHealthLabel2);
					gamePanel.remove(characterHealthLabel3);
					
					gamePanel.repaint(); //Re-loads the JPanel, also known as gamePanel, so that the removed components disappear.
					TestRender  gameWindow = new TestRender (gamePanel);
					gameWindow.startThread();
					gameWindow.run();
				}
			}});

From what I can see, the thread is being started too quickly which is causing the ‘gamePanel.repaint();’ from the ‘Continue’ button to freeze before the components on the JPanel can be repainted. Although the repaint freezes the thread runs properly and that. Is there some way to stop everything for a around 500 milliseconds so the repaint can work properly?

When I say the repaint ‘freezes’ I mean that the whole JFrame, JPanel and all of the components become unresponsive so that the only way to stop the program is CTRL+C in the cmd and all of the components stay on the JPanel as they looked when the repaint was (called?) in the ‘Continue’ button’s code.

Edit: I hardly know what I’m doing with the game loop other than some theory’s on how I’ll use it so if anyone has suggestions on how to change it, please say so!

You have to pass your Runnable to the Thread as parameter

Thread thread = new Thread(this);

and start it just like this

gameWindow.startThread();

without:

gameWindow.run();

EDIT:
better do like this
instead of

 gameWindow.startThread();
               gameWindow.run();

put

new Thread(new TestRender).start();

Could you explain a bit about what

new Thread(new TestRender).start();

does, I prefer not to use things that I don’t understand when I can help it. Your first answer fixed the problem though.

Look at the code, its exactly the same as before, just that the Thread is started from your other class. I edited it because (without knowing your architecture), it just felt a little more right to me :wink:

Thanks for the help. ;D

While I’ve been waiting a few minutes in between page refreshes for the answer which you’ve provided I came up with yet another question to ask anyone reading. I’ve seen that without double buffering there is very noticeable (I’ll describe it as stutter I guess) when for example, moving a string by -10 pixels per few milliseconds on an applet. Because of this stutter I noticed when working with an applet awhile ago I decided to look up some information about buffering in Java; In my little google search I ran across http://c2.com/cgi/wiki?DoubleBufferedGraphicsInJava which, in my opinion, basically said that double buffering is a waste of memory and that you shouldn’t use it.
The previously linked (article?) made me a bit unsure about using a double buffer. Is a double buffer the best way to get rid of ‘stutter’?

I honestly haven’t read the most of the earlier posts, but what I do is make my rendering logic part of a class that implements Runnable and extends Canvas. Then at program startup I create a JFrame, attach my rendering class to it (as it’s a canvas), pack, show the frame, then start the render control as a thread:

        final JFrame frame = new JFrame(GAME_TITLE);
        final MainWindow window = new MainWindow();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(window);
        frame.pack();
        frame.setVisible(true);
        frame.requestFocus();

        window.start();

The run has a loop in it (well, at the BASIC level it’s a loop :p) that looks like this:

       final BufferStrategy bs = getBufferStrategy();
        if (bs == null) {
            createBufferStrategy(2);
            return;
        }

        final Graphics g = bs.getDrawGraphics();
        // DRAW LOGIC HERE

        g.dispose();
        bs.show();
        bs.dispose();

Please note that this is inside of a render() type function, not inside the run() directly, so the “return” only leaves the local render function, not the top level loop.

CreateBufferStrategy does the best it can to double buffer using your system’s hardware acceleration, but hides the actual implementation from you. You simply draw what you want in the DRAW LOGIC HERE part, and the rest will be flicker free. There is no memory wasted because, at least with my (2), it only has a couple buffers.

You may want to read up more on threads, and in particular implementing classes as Runnable. With that and a global ExecutorService object, you can have worry-free threading where needed with minimal coding.

Just managed to get keyboard controls working perfectly, now to figure out how collision works…

Not too much has been done in the past two days; I’ve spent around 5 or so hours attempting to figure out/implement collision but so-far my attempts have failed, if anyone can give me a few pointers it will be appreciated. Although I’ve not done too much I did manage to get keyboard controls working as I’ve previously stated and I’ve just recently expanded them so that you can move with either WASD or the Arrow keys.

;D

It took a small chunk of my Christmas vacation but I finally managed to figure out a way to draw a map onto the JPanel. There are a few bugs to iron out and a lot to expand on but at least the basic idea worked. ;D

Pretty simple actually… If you don’t want to have fancy stuff with Polygons and rotation and what-not, simply use java.awt.Rectangle. To test whether two rectangles collide use [icode]rect0.intersects(rect1);[/icode].

I guess this should help you :slight_smile:

For collision, there is also checking the hypoteneuse for circle-circle collision… Depends on what shapes are being collided with.

(radius circle A + radius circle B)^2 < (x1-x2)^2 + (y1-y2)^2