[gameprogrammer] Re: I wonder if I can go Remedial Applet on you all... :)

  • From: "Mike Gillissie" <Niyoto@xxxxxxxxxx>
  • To: <gameprogrammer@xxxxxxxxxxxxx>
  • Date: Wed, 30 Mar 2005 22:32:56 -0500

I like the fun stuff... ;)

OK, latest revision does the following:

- creates the JPanels with the sizes I'd specified
- loads an image I've got (grass) into a BufferedImage object
- tiles the image on the screen (by drawing it x*y times) - this will change 
soon to allow me to draw appropriate tile images based on which part of the 
map I'm showing

Two main questions so far:

1) Is my method of using BufferedImage and Graphics2D.drawImage the most 
efficient method? Although the overall map will be very large, the visible 
portion will be quite managable - but am I making things more difficult for 
myself than need be?

2) What's the deal with Paint and Update? Resizing the window is 
automatically painting on me, which isn't bad, but sometimes resizing just 
leaves me with an empty-looking window - should I be overriding some sort of 
window resize events, painting only when the resize is done? Or would I do 
better to (*gag*) lock the window size?

For the umpteenth time, thanks! :)
-Mike

-- Code so far, probably worth a chuckle or 12... ----------------
package map;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ThisIsTheGame extends JFrame implements GameSettings
{
 public static ThisIsTheGame app;
 private static final int APP_WIDTH = 800;
 private static final int APP_HEIGHT = 600;
 public Container   map;
 public Container   navigator;

 public static void main(String[] args)
 {
  app = new ThisIsTheGame();
  app.setVisible(true);
  app.setTitle("Good... Bad... I'm the guy with the game.");
 }

 public ThisIsTheGame()
 {
  super();

  setSize(new Dimension(APP_WIDTH, APP_HEIGHT));
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  getContentPane().setLayout(new java.awt.BorderLayout());
  buildInterface(getContentPane());
 }

 public void buildInterface(java.awt.Container contentPane)
 {
  navigator = buildNavigatorPane();
  map = buildGamePane();

  contentPane.add(navigator, java.awt.BorderLayout.WEST);
  contentPane.add(map, java.awt.BorderLayout.CENTER);
 }

 public javax.swing.JComponent buildNavigatorPane()
 {
  JPanel board = new JPanel();
  board.setPreferredSize(new Dimension(200, 600));
  board.setMinimumSize(new Dimension(200, 600));
  return board;
 }

 public javax.swing.JComponent buildGamePane()
 {
  JPanel board = new JPanel();
  board.setPreferredSize(new Dimension(600, 600));

  return board;
 }

 public void paint(Graphics gfx)
 {
  System.out.println("PAINT");

  //super.paint(gfx);

  Graphics2D workAreaGfx = (Graphics2D) map.getGraphics();

  String name = WORKING_DIRECTORY + "/grass.gif";
  Image image = Toolkit.getDefaultToolkit().getImage(name);

  File f = new File(name);
  BufferedImage bufi = null;
  try
  {
   bufi = ImageIO.read(f);
  } catch (Exception e)
  {
   e.printStackTrace();
  }

  for (int x = 0; x < map.getWidth(); x += bufi.getWidth())
   for (int y = 0; y < map.getWidth(); y += bufi.getHeight())
    workAreaGfx.drawImage(bufi, x, y, this);

  workAreaGfx = (Graphics2D) navigator.getGraphics();
 }

 public void update(Graphics arg0)
 {
  System.out.println("UPDATE");
 }
}

----- Original Message ----- 
From: "Dave Slutzkin" <daveslutzkin@xxxxxxxxxxx>
To: <gameprogrammer@xxxxxxxxxxxxx>
Sent: Wednesday, March 30, 2005 9:12 PM
Subject: [gameprogrammer] Re: I wonder if I can go Remedial Applet on you 
all... :)


> On Wed, 30 Mar 2005 20:27:04 -0500, "Mike Gillissie" <Niyoto@xxxxxxxxxx>
> said:
>> Very nice - I'm all over that one... ;)
>>
>> The immediate result of your help was this - doesn't look very "fun," but
>
> The 'fun' stuff is easy, this is the hard part.  ;-)
>
>> it
>> actually draws the portions of the screen the size I want, and repaints
>> mostly right (still have to learn some of the settings related to
>> "preferred
>> size" I think:
>
> Yeah, the best way to work that stuff out is to experiment with it.
> Lots and lots.  It's can be a little tricky to get exactly right.
>
> Good luck,
>
> Dave.
> -- 
>  Dave Slutzkin
>  Melbourne, Australia
>  daveslutzkin@xxxxxxxxxxx
>
>
>
> ---------------------
> To unsubscribe go to http://gameprogrammer.com/mailinglist.html
>
>
> 




---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: