Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
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 / Simple-XML and class attribute issues when dealing with multiple projects on: 2014-01-12 10:07:03
Complex problem here, since it involves also Java for Android. I'll ask anyway.

I'm trying to use SimpleXML to serialize/deserialize data that must be used by a main program (running on Android platform) and an editor (running on a Desktop platform).

The editor extends a ParentClass (contained in a "common libraries" project) with a ChildClass to give it more functionality (such as graphic properties and so on).

I would like to not save any reference to ChildClass in the XML files, to avoid circular dependencies problems between projects.

This is a summary of how the projects are structured:
First project(EDITOR)
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  
// I project (DESKTOP EDITOR, requires COMMON CLASSES to build)

@Element(name = "Tile2", type = Tile2.class, required = false)
public class ChildClass extends ParentClass {
    BufferedImage thumbnailToDisplay = null;
    // Additional methods and variables that are not annotated,
   // since they don't have to go into the XML, like these ones
   public void generateThumb() {
        getGraphics(super.imageName);
    }

    public BufferedImage getGraphics(String imgName) {
        // various graphic routines
   }
}

public class EditorWindow(Container contentToModify) {
    private mCont;
    // Constructor
   public EditorWindow(Container contentToModify) {
        this.mCont = contentToModify;
        this.initialize();
    }
    // Various methods
   // Must access all the elements in contentToModify
   // as if their type is ChildClass
   public void initialize() {
        for (int i = 0; i < this.mCont.getItems().size(); i++)
            ChildClass tmp = (ChildClass)this.mCont.getItems().get(i);
            tmp.generateThumb();
    }
 }

 // This is the main class, that starts the editor GUI, initializes stuffand so on
public class Gui() {
    Container mainContainer;
    EditorWindow edWin;

    public static main() {
        mainContainer = Xml_IO.XmlLoad(new File("XmlPath/filename"));
        edWin = new EditorWindow(mainContainer);

        // Stuff that could modify the content of mainContainer
       File file = saveFile.getSelectedFile();
        XML_IO.XmlSave(file, this.mainContainer);
    }
 }


Second project (common libraries)

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  
49  
50  
51  
// II project (COMMON CLASSES)
@Root(strict=false)
public class ParentClass {
    @Attribute
    float size;
    @Attribute
    float position;
    @Element
    String imageName;
}
@Root(name = "Container", strict=false)
public class Container {
    @ElementList(type = parentClass.class, required = false,inline=true, entry="container")
    private ArrayList<ParentClass> mItems = new ArrayList<ParentClass>();

    public ArrayList<XItem> getItems() {
        return mItems;
    }

}

public class Xml_IO {
    // Deserializer method (could be moved to I project(DESKTOP EDITOR), if needed)
   public static Container XmlLoad(File inputXmlFile) {
        Container tmpContainer = new Container();
        Serializer serializer = new Persister();

        try {
            tmpContainer = serializer.read(Container.class, inputXmlFile);
        } catch (Exception e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
        }

        return tmpContainer;
    }

    // Serializer method (could be moved to I project(DESKTOP EDITOR), if needed)
   public static void XmlSave(File outputXmlFile, Container containerToSave) {
        Format format = new Format(
                "<?xml version=\"1.0\" encoding= \"UTF-8\" ?>");
        Serializer serializer = new Persister(format);

        try {
            serializer.write(containerToSave, outputXmlFile);
        } catch (Exception e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
        }
    }
}


Third project (Android application)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// III project (ANDROID MAIN PROGRAM, requires COMMON CLASSES to build, must NOT require EDITOR to build) 
public class Activity extends CustomActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        // Custom serializer
       Container contentToRead = new Container();
        Serializer serializer = new Persister();

        try {
            contentToRead = serializer.read(Container.class, this.getResources().openRawResource(R.raw.xml_resource), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


This is instead an example of the XML generated with current setup:
1  
2  
3  
4  
5  
6  
7  
8  
9  
<?xml version="1.0" encoding= "UTF-8" ?>
<Container>
    <ParentClass class="project1.ChildClass" size="1.0" position="2.5">
       <mImage>image1</mImage>
    </ParentClass>
    <ParentClass class="project1.ChildClass" size="2.0" position="1.5">
       <mImage>image2</mImage>
    </ParentClass>
</Container>


The I/O on the editor works well, but if I try to deserialize the XML on Android, it fails with the following error: java.lang.ClassNotFoundException: Didn't find class "project1.ChildClass" on path: DexPathList[dexElements=[zip file "/mnt/asec/projectname/pkg.apk"],nativeLibraryDirectories=[/mnt/asec/projectname/lib, /vendor/lib, /system/lib]]

This is what I tried so far:

    1.I tried the @ElementListUnion annotation, as suggested here, but it does not remove the class attribute.
    2.tried to inline the list, as suggested here, but the class attribute did not disappear
    3.tried to understand how to implement a Converter, as suggested here but I cannot figure out how to solve the problems of circular dependencies it would introduce
    4.tried to manually remove the class="project1.ChildClass" attributes: the Android application in this case works, but the editor stops importing XML files, since it would not be possible to cast the ParentClass into a ChildClass. Even if I would solve that issue, it would still save the XML file with the "class" attribute

What I would like is to serialize XML files from the editor without any reference to the ChildClass (so the Android application could read them), but to deserialize the ParentClass items as ChildClass into the editor.
2  Game Development / Game Play & Game Design / Building a sprite sheet. on: 2013-08-06 14:12:51
I have my animation stored in 14 png files.
Does somebody know a software or something to store them in a single spritesheet? 
3  Game Development / Newbie & Debugging Questions / Re: Loading a Tilemap as Background on: 2013-08-03 05:27:02
Jeremy, this is very informative. Thank you. But some new questions arise, before trying to code something.
Why do you use the abstract Image class instead of BufferedImage?
What do you want to represent with the field m_sourceRectangle?

For the World class:
Does m_worldWidth represent the number of pixels being rendered on the screen?

I don't understand this line
1  
2  
//Create our array.
         m_tiles = new Tile[worldWidth * worldHeight];

I thought you had to use a two dimensional array (not a single dimensional), so you can easily retrieve a block from whereever you like.

And another thing: My approach is to draw from a class called GampePanel and then have a class dedicated only to the TileMap.
Is this ok?
4  Game Development / Newbie & Debugging Questions / Loading a Tilemap as Background on: 2013-08-02 11:54:56
I've been trying to find some deep and complete explanations on Backgroung tiling maps for weeks, but I still can't be confident at it. I need help.
I have my GamePanel class with the gameloop and the graphics g used to paint things on the screen. Quite well, my character is moving around and jumping happily.
Now comes the hard part, the TileMap class.
First question: which variables shall I use?
For now I'm thinking about those
         
       
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
//coordinates that I may use to offset the screen
  public int Tilemapx;
   public int Tilemapy;
   //number of tiles in the image
  private int TotalRows;
   private int TotalColumns;
   private int Rows;
   private int Cols;
   //size of the Tile
  public int TileSize;
        //Two Buffered images, one for the entire map and one for the single tile
  public BufferedImage EntireMap;
   public BufferedImage Tile;
   //a two dimensional array so I can reach any tile with two coordinates
  public int [] []  map;


Going on step by step I'd make a contructor and put on TotalRows and TotalColumns the columns and Rows to be rendered(+2 offscreen)
1  
2  
3  
4  
5  
public TileMap (int TileSize){
      this.TileSize = TileSize;
      TotalRows = Pannello.WIDTH / TileSize + 2;
      TotalColumns = Pannello.HEIGHT / TileSize + 2;
   }


So far so good(I hope).

Now, everybody speaks about this for loop, and I imagine that I have to retrieve a tile (or tiles?) with the subimage
and for the sake of learning I would be happy to display just one tile(the text map that drives the graphics is something I want to study when I'm more prepared).
So, here I have a method to load the map that I painted in Photoshop and the for loop that is supposed to draw a tile on 0, 0.

 
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
 public void loadTileImg (){
       String TileMapPath = "res/Tiles.png";
       try {
         EntireMap = ImageIO.read(new File(TileMapPath));
      } catch (IOException e) {
         e.printStackTrace();
      }
       for (int Cols = 0; Cols < TotalColumns; Cols++){
          for (int Rows = 0; Rows < TotalRows; Rows++){
              Tile = EntireMap.getSubimage(0, 0, TileSize, TileSize);
                         
          }


That's where my lacking of experience and newbieness comes in. The screen is actually flashing like a strobo and the tile isn't getting displayed.


5  Game Development / Newbie & Debugging Questions / Re: painting and animating in Jpanel on: 2013-08-01 15:52:24
Ok, after testing and studying I am at a conclusion. Correct me if I'm wrong.
You can mix awt and Swing, especially if it's a Canvas inside a JFrame, so you won't deal with other components like JButton and stuff. Canvas let you bypass the JVM (so you won't be overriding paint) with the bufferstrategy inside EDT.
Once you did that, you can draw anything you like with g object.
Jpanel can bypass too the JVM with a BufferedImage to avoid the overwritten PaintComponent and thus putting everything into the EDT.
6  Game Development / Newbie & Debugging Questions / Re: painting and animating in Jpanel on: 2013-08-01 09:13:29
And while I'm reading Oracle, I'm submitting to you this.
Quote
Swing starts with AWT's basic painting model and extends it further in order to maximize performance and improve extensibility
7  Game Development / Newbie & Debugging Questions / Re: painting and animating in Jpanel on: 2013-08-01 07:18:57
Ok, very weird things happening. First of all, I've changed this line
1  
repaint();


to this

1  
paint(this.getGraphics());


And the flickering is gone.

And in the main class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public class Game {

   
   public static void main(String[] args) {
   JFrame frame = new JFrame();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setContentPane(new GamePanel());
   frame.setResizable(false);
   frame.pack();
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
   
   }

}


Changing the setResizable from a line to another determines the flickering too. Wow, I've found this by trial and error, am I supposed to find documentation about that?
The problem is that you rely on the JVM to do your painting. You only use BufferedImages to buffer the loading process, the drawing with the Graphics object is still unbuffered and therefore flickers like hell Smiley.
@sirkarpfen: How do you buffer the graphic object?
8  Game Development / Newbie & Debugging Questions / Re: painting and animating in Jpanel on: 2013-08-01 06:39:56
I'm new here but I really like this forum! Thanks for the answers, so far. I think I'll have to try everything before finding which method works best for me.
Anyway, concurrency docs say that swing painting operations must reside in the EDT, aka run Thread.

I cannot call paint component directly, so I'm using a BufferedImage painted by a (g) graphic object. It moves but the problem now is that it flickers like crazy.
Jpanel is double buffered by default so I must be offroad, somehow.

Now I get this (add notify let you draw the Panel and when it's ready it will show up, and most of all, it saves me a line of code since I don't have to call the class, it starts automatically):
I've modified the game loop just to be sure that it has not the culprit.

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  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
public class GamePanel extends JPanel implements Runnable {
   
   public static int WIDTH = 1024;
   public static int HEIGHT = WIDTH / 16 * 9;
   private Thread t1;
   boolean running;
   private int FPS = 60;
   private long optimalTime = 1000 / FPS;
   private int heroX = 200;
   private int heroY = 200;
   private BufferedImage Base;
   public Graphics g;
   
   
   public void addNotify() {
      super.addNotify();
      Dimension size = new Dimension(WIDTH,HEIGHT);
      setPreferredSize(size);
      setFocusable(true);
      requestFocus();
      running = true;
      t1 = new Thread(this);
      t1.start();
   }

   public void init() {
      Base = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
      g = (Graphics) Base.getGraphics();
   }
   
   
   public void run() {
      init();
      long startTime;
      long passedTime;
      long waitTime;
      while (running){
      //   startTime = System.nanoTime();
     //   System.out.println("Runs");
        update();
         draw();
         repaint();
      //   passedTime = System.nanoTime() - startTime;
     //   waitTime = optimalTime - passedTime / 1000000;
        try {
         Thread.sleep(17);
         } catch (InterruptedException e) {
           e.printStackTrace();
         }
      }
     
   }



   public void draw() {
     
      g.setColor(Color.WHITE);
      g.fillRect(0, 0, WIDTH, HEIGHT);
      g.setColor(Color.BLACK);
      g.fillOval(heroX,heroY,30,30);
      g = this.getGraphics();
      g.drawImage(Base,0,0,null);
   }



   private void update() {
     
      heroX += 2;
     
   }

}
9  Game Development / Newbie & Debugging Questions / Re: painting and animating in Jpanel on: 2013-07-31 14:32:21
Herjan how can you say they are bad ideas when they come from Oracle? And even when I was posting to other forums with Canvas examples everybody said to me to use JPanel.
 Huh
10  Game Development / Newbie & Debugging Questions / Re: painting and animating in Jpanel on: 2013-07-31 10:22:41
Thanks, Sirkarpfen. But I've already accomplished to use Canvas. Then I stumbled upon this article, which basically says that is a bad idea to mix lightweight(JFrame)components with heavyweight(Canvas) ones.
http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html
As matter of fact, Canvas was superseded many years ago by Swing, even though it is still popular among some programmers.

So, maybe I'm thinking that I don't have to use paintComponent at all (since I cannot use it outside the EDT), but just a graphic object. Still looking for a solution.
11  Game Development / Newbie & Debugging Questions / Re: painting and animating in Jpanel on: 2013-07-31 08:27:44
Yes, it's true. But I still don't understand what I'm doing wrong.
12  Game Development / Newbie & Debugging Questions / Re: painting and animating in Jpanel on: 2013-07-31 07:59:52
The loop is looping, yes, the debugger says.
About BufferedImage. Do I need it along with paintComponent? Or paintComponent is enough to draw bitmaps?
13  Game Development / Newbie & Debugging Questions / painting and animating in Jpanel on: 2013-07-31 07:26:33
I drew an Oval and that is supposed to move right, when infact it doesn't. I'm also a little confused about when invoking a BufferedImage.
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  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
public class GamePanel extends JPanel implements Runnable {
   
   public static int WIDTH = 1024;
   public static int HEIGHT = WIDTH / 16 * 9;
   private Thread t1;
   boolean running;
   private int FPS = 60;
   private long optimalTime = 1000 / FPS;
   private int heroX = 200;
   private int heroY = 200;
   
   
   public void addNotify(){
      Dimension size = new Dimension(WIDTH,HEIGHT);
      setPreferredSize(size);
      setFocusable(true);
      requestFocus();
      running = true;
      t1 = new Thread(this);
      t1.start();
   }
   

   
   public void paintComponent (Graphics g){
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setColor(Color.WHITE);
      g2.fillRect(0, 0, WIDTH, HEIGHT);
      g2.setColor(Color.BLACK);
       g2.fillOval(heroX, heroY, 50, 50);
       g2.dispose();
   }

   
   public void run() {
      long startTime;
      long passedTime;
      long waitTime;
      while (running){
         startTime = System.nanoTime();
         System.out.println("Runs");
         update();
         repaint();
         passedTime = System.nanoTime() - startTime;
         waitTime = optimalTime - passedTime / 1000000;
         try {
            if (waitTime <= 0){
               waitTime = 2;
            }
            Thread.sleep(waitTime);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
     
   }


   private void update() {
     
      heroX += 2;
     
   }

}
14  Game Development / Newbie & Debugging Questions / Re: Background image in a 2d platform game on: 2013-07-23 18:34:19
Oskuro, as I've understood, I should just paint single tiles (maybe in three different layers as the link you posted) and then combine them back in the code.
The concept is fairly easy. What I'm still unsure about is the workflow. I come from a painting background, I imagine that life would be a lot easier if I could paint the map as I see it and only then disassemble all into tiles.
I apologize if I'm being too vague, but I'm still at the beginning.
15  Game Development / Newbie & Debugging Questions / Background image in a 2d platform game on: 2013-07-22 19:39:18
Whazzup guys.
So I'm doing my first Java Game. I've got a single screen with a character moving and jumpin around. I would like to do a scrolling map like Super Mario or Sonic, with hills and stuff.
Basically, I'm trying to understand how to implement that. If somebody knows a good tutorial about it, I will study that!
16  Game Development / Newbie & Debugging Questions / Re: Moving logic in game loop on: 2013-07-19 15:16:33
Ok, it moves, by simply putting the timer in the init part.
But now I'm not using threads anymore!
I know my code is messy, but how can I put all the stuff back in the run method, using a separate thread for the game loop?
17  Game Development / Newbie & Debugging Questions / Moving logic in game loop on: 2013-07-19 14:40:49
In my way to build my first game, I'm trying to move my character.
I don't know if it's the right way to do it, but basically I'm using a timer inside a run method and the game loop inside the actionlistener.
Even debugging, I don't know why it doesn't move.

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  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
public synchronized void start (){
      running = true;
      t1 = new Thread (this);
      t1.start();
     
      }
 
   
   public synchronized void stop (){
      running  = false;
      try {
         t1.join();
         System.out.println("The game stopped");
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
      }
   }
   
   //INIT
  public Panel(){
     
      setPreferredSize(new Dimension(WIDTH, HEIGHT));
      setFocusable(true);
      addKeyListener(this);
      requestFocus();
      load();
     
     
     
   }
   
   ActionListener gameloop = new ActionListener() {
     
      @Override
      public void actionPerformed(ActionEvent e) {
         go();
         repaint();
         System.out.println("The game runs");
      }
   };
   
   
   
   //MAIN PROCESS
 
   public void run(){
     
      Timer timer = new Timer(1000/60,gameloop);
      timer.start();
     
      try {
         Thread.sleep(1000/60);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
      }
           
   }
   
   
   //PAINT IMAGES
  public void paintComponent (Graphics g){
      super.paintComponent(g);
      g.drawImage(sfondo, 0, 0, this);
      g.drawImage(boaris, cordX, cordY, this);
     
   
   }
   
   //LOADS
  public void load (){
            try {
         String sfondopath = "res/sfondo.png";
         sfondo = ImageIO.read(new File (sfondopath));
         String imgpath = "res/img.png";
         img = ImageIO.read(new File (imgpath));
      } catch (IOException e) {
         
         e.printStackTrace();
      }
           
   }


   
   public void go(){
      cordX += vX;
      cordX += vY;
       
   }
   
   public void gameupdate(){
      vX=0;
      vY=0;
      if (down) vY = speed;
      if (up) vY = -speed;
      if (left) vX = -speed;
      if (right) vX = speed;
   }
   

   
   public void keyPressed(KeyEvent ke) {
       switch (ke.getKeyCode()) {
         //if the right arrow in keyboard is pressed...
        case KeyEvent.VK_RIGHT: {
             right = true;
         }
         break;
         //if the left arrow in keyboard is pressed...
        case KeyEvent.VK_LEFT: {
             left = true;
         }
         break;
         //if the down arrow in keyboard is pressed...
        case KeyEvent.VK_DOWN: {
             down = true;
         }
         break;
         //if the up arrow in keyboard is pressed...
        case KeyEvent.VK_UP: {
             up = true;
         }
         break;
     }
     gameupdate();
   }
18  Game Development / Newbie & Debugging Questions / Re: Drawing images in Jpanel, JFrame and game loop on: 2013-07-18 20:52:47
Thanks Andy and Longarmx.
I did a stupid error in the other class. And more, now I've used paintComponent.
Let me ask you another thing. I don't see flickering in the movement.
Does that mean that I don't need to manually setup double buffering?
19  Game Development / Newbie & Debugging Questions / Drawing images in Jpanel, JFrame and game loop on: 2013-07-18 07:19:16
Guys, my first post, here.
I'm learning to program a game. I just want to create a Runnable thread, putting inside the run method a static image.
I just want to understand the little steps that I'm thinking about. I'll deal with fps,doublebuffering and keylistener later.
The problem is that now when I run the program, nothing is displayed.
I have another class, which is just a jframe calling this other class and putting everything inside with setContentPane.
I've also tried with paintcomponent but nothing changes.

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  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
public class myPanel extends JPanel implements Runnable{

//FIELDS
public static int WIDTH = 1024;
public static int HEIGHT = WIDTH / 16 * 9;
private BufferedImage bg;
private BufferedImage charac;
private boolean running;
private Thread t1;
private int startposX = WIDTH / 2;
private int startposY = HEIGHT / 2;
private int cordX = startposX;
private int cordY = startposY;
int speed = 50;


//METHODS  
public synchronized void start (){
    running = true;
    t1 = new Thread (this);
    t1.start();
}

public synchronized void stop (){
    running  = false;
    try {
        t1.join();
        System.out.println("The game stopped");
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
       e.printStackTrace();
              }
 }

 //INIT
public myPanel(){

   setPreferredSize(new Dimension(WIDTH, HEIGHT));
   setFocusable(true);
   requestFocus();
    }

 //MAIN RUN METHOD

 public void run(){
           while (running){
           load();
           System.out.println("The game runs");
           repaint();
          }
 }


  //PAINT WITH GRAPHICS METHOD
 public void paint (Graphics g){
    super.paint(g);
    g.drawImage(bg, 0, 0, this);
    g.drawImage(charac, 110, 280, this);

}

//LOAD IMAGES IN MEMORY
public void load (){
            try {
        String path1 = "res/bg.png";
        bg = ImageIO.read(new File (path1));
        String path2 = "res/charac.png";
        charac = ImageIO.read(new File (path2));
    } catch (IOException e) {

        e.printStackTrace();
    }

}
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.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (59 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

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

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

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

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

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!