Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: ArrayList of Arrays & "Array.rotate()" on: 2008-08-30 13:00:43
1  
2  
3  
4  
5  
6  
7  
public void rotateLeft(int[] arr)
{
   int tmp = arr[0];
   for(int i=1; i<arr.length; i++)
      arr[i-1] = arr[i];
   arr[arr.length-1] = tmp;
}


This is exactly what I was looking for. Thank you very much. And sorry for the ambiguous example...
2  Game Development / Newbie & Debugging Questions / ArrayList of Arrays & "Array.rotate()" on: 2008-08-28 01:45:38
(1)
I would like to access the first element of an ArrayList of Arrays:
ArrayList<int[]> MyArrayListOfArrays = new ArrayList<int[]>();

int value = (MyArrayListOfArrays.get(0))[0];
Would that work?

(2)
I have to rotate an array.

Example:
{1,2,3,4}
{2,3,4,1}
{3,4,1,2}
{4,1,2,3}

Any suggestions, please?
3  Game Development / Newbie & Debugging Questions / Re: Sun Tutorial: LoadImageApp on: 2008-08-28 00:06:30
"But you probably don't want to use paint(). Instead, override paintComponent() and put all drawing in there."

That's right, I don't use paint() by myself. But refresh() calls it for me. In order to redraw the screen, all I have to do is typing "MyFrame.refresh()". That's quite comfortable, I think. I do not understand why I should override paintComponent(). What's the intention/advantage of doing so?


"Well, in my opinion, trial and error is certainly an option when coding, but only once you know enough that most of the trials don't turn into errors. In all other cases you need to study the problem with some background info / documentation / tutorials / browsing sourcecode."

Yes, you are right. I forgot to mention that I already have some experience in C++ and in other languages as well. So programming in general isn't new to me. All I have to do is getting familiar with the subtleties of Java. Some of them - especially the fact that there are no pointers in Java - are quite irritating to me at the moment. And that might be the reason why it seems to you that I'm "puzzled by the way the simplest code behaves".
4  Game Development / Newbie & Debugging Questions / Re: Sun Tutorial: LoadImageApp on: 2008-08-26 18:08:55
Okay, it's as I suspected: paint() is called by setVisible(). That's why I initially couldn't figure out how the system works... But now its perfectly clear to me. Thanks. Smiley

I for myself prefer this trial-and-error method. In my opinion, it's far more interesting to simply start working on a game than reading boring books and tutorials. Only if I get stuck, do I try to find a solution. In my experience, that's the most efficient way of learning.
5  Game Development / Newbie & Debugging Questions / Re: Sun Tutorial: LoadImageApp on: 2008-08-25 20:52:00
I did another test replacing...

1  
2  
3  
    public void paint(Graphics g) {
       g.drawImage(img, 0, 0, null);
    }


with

1  
2  
3  
4  
    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null);
        g.drawImage(img2, 0, 0, null);
    }


And now both images are shown! Does "setVisible()" call my "paint()" function!?
6  Game Development / Newbie & Debugging Questions / Re: Sun Tutorial: LoadImageApp on: 2008-08-25 20:35:45
Thank you, but these are not the answers that I expected. I know what a constructor is... Let me explain it again:

I took the previous code and replaced

1  
2  
3  
4  
5  
    public LoadImageApp() {
       try {
           img = ImageIO.read(new File("knight.png"));
       } catch (IOException e) {
       }


with

1  
2  
3  
4  
5  
6  
    public LoadImageApp() {
       try {
           img = ImageIO.read(new File("knight.png"));
           img2 = ImageIO.read(new File("squire.png"));             <==
       } catch (IOException e) {
       }
.

One might expect that the program now adds TWO images instead of one. But it doesn't! It still shows only one - the first one. So how does it know that I want it to display this one and not the other one, or even both of them? That's why I thought that there must be some kind of return value. How does it work? How does add() know that it shall take "img" instead of "img2"?
7  Game Development / Newbie & Debugging Questions / Re: Sun Tutorial: LoadImageApp on: 2008-08-25 11:00:01
Sorry, void is a wrong expression. What I mean is, that LoadImageApp() doesn't return anything. But if it doesn't return anything, and f.add() expects something to be added to the JFrame, how does the system ensure that my program actually knows what I want to be added to the frame?

1  
f.add(new LoadImageApp());
8  Game Development / Newbie & Debugging Questions / Tiles on: 2008-08-25 00:13:16
There is an image file containing a set of small pictures. How do I display one of these small pictures (into a JFrame) if the coordinates relative to the upper left corner are known?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
        JFrame f = new JFrame("Load Image Sample");
           
        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        f.add(new LoadSmallPicture());   <==
        f.pack();
        f.setVisible(true);
9  Game Development / Newbie & Debugging Questions / Sun Tutorial: LoadImageApp on: 2008-08-25 00:03:31
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/**
 * This class demonstrates how to load an Image from an external file
 */

public class LoadImageApp extends Component {
         
    BufferedImage img;

    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null);
    }

    public LoadImageApp() {
       try {
           img = ImageIO.read(new File("knight.png"));
       } catch (IOException e) {
       }

    }

    public Dimension getPreferredSize() {
        if (img == null) {
             return new Dimension(100,100);
        } else {
           return new Dimension(img.getWidth(null), img.getHeight(null));
       }
    }

    public static void main(String[] args) {

        JFrame f = new JFrame("Load Image Sample");
           
        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        f.add(new LoadImageApp());
        f.pack();
        f.setVisible(true);
    }
}


I do not understand what the following line means:
1  
 f.add(new LoadImageApp()); 


LoadImageApp() is void, isn't it? So what does "new LoadImageApp()" return?
How do I remove the image from JFrame f again?
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (46 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (32 views)
2014-07-23 20:56:16
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!