Advertisement
mixster

myGame

Dec 24th, 2010
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. ///// Main.class \\\\\
  2.  
  3. package mapMaker;
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8. import javax.swing.*;
  9.  
  10. public class Main {
  11.    
  12.     private static void createAndShowGUI() {
  13.         JFrame frame = new JFrame("myGame - Main Form");
  14.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.  
  16.         JButton cmd = new JButton("Button 1");
  17.         cmd.addActionListener(new ActionListener() {
  18.             public void actionPerformed(ActionEvent e) {
  19.                 System.out.println("You clicked the button!");
  20.             }
  21.         });
  22.         frame.getContentPane().add(cmd);
  23.         frame.getContentPane().setLayout(new FlowLayout());
  24.         frame.pack();
  25.         frame.setVisible(true);
  26.  
  27.         DataBank sideForm = new DataBank("Side Form");
  28.         sideForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.         sideForm.pack();
  30.         sideForm.setVisible(true);
  31.        
  32.         System.out.print("main() main form");
  33.     }
  34.    
  35.     public static void main(String[] args) {
  36.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  37.             public void run() {
  38.                 createAndShowGUI();
  39.             }
  40.         });
  41.     }
  42. }
  43.  
  44.  
  45. ///// DataBank.class \\\\\
  46.  
  47. package mapMaker;
  48.  
  49. import java.awt.*;
  50. import java.awt.image.*;
  51. import java.io.*;
  52. import java.net.*;
  53.  
  54. import javax.imageio.*;
  55. import javax.swing.*;
  56.  
  57. public class DataBank extends JFrame {
  58.     BufferedImage bground = loadImage("C:/Users/mc_teo/Desktop/pokemon_sprites.jpg");
  59. //  BufferedImage bground = loadOnlineImage("http://www.freewebs.com/pokemontopazproject/pokemon%20sprites%201111112222233333444445555566666677777888889999900000.bmp");
  60.     BufferedImage db[];
  61.  
  62.     int tilew = 17;    // pixels per tile (including whitespace)
  63.     int tileh = 17;    // pixels per tile (including whitespace)
  64.     int realw = 16;    // pixels per tile (not including whitespace)
  65.     int realh = 16;    // pixels per tile (not including whitespace)
  66.    
  67.     public DataBank(String string) {
  68.         setTitle(string);
  69.     }
  70.  
  71.     public void prepim() {
  72.         int cols = bground.getWidth()/tilew;
  73.         int rows = bground.getHeight()/tileh;
  74.         int num = 0;
  75.         db = new BufferedImage[rows*cols];
  76.         for(int y = 0; y < rows; y++) {
  77.             for(int x = 0; x < cols; x++) {
  78.                 db[num] = new BufferedImage(realw, realh, bground.getType());
  79.                 // Tell the graphics to draw only one block of the image
  80.                 Graphics2D g = db[num].createGraphics();
  81. //              g.drawImage(bground, 0, 0, realw, realh, (tilew*x), (tileh*y), ((tilew*x)+realw), ((tileh*y)+realh), null);
  82.                 g.drawImage(bground, 0, 0, realw, realh, (tilew*x), (tileh*y), ((tilew*x)+realw), ((tileh*y)+realh), null);
  83.                 System.out.print("x: " + tilew*x + " y: " + tileh*y + " h: " + ((tileh*y)+realh) + " w: " + ((tilew*x)+realw) + '\n');
  84. //              System.out.print("");
  85.                 g.dispose();
  86.                 num++;
  87.             }
  88.         }
  89.     }
  90.    
  91.     public void init() {
  92. //      setSize(bground.getWidth(), bground.getHeight());
  93.         setSize(500, 500);
  94.         prepim();
  95. //        javax.swing.SwingUtilities.invokeLater(new Runnable() {
  96.  
  97.     }  
  98.    
  99.     public void main() {
  100. //      repaint();
  101.     }
  102.  
  103.     public void paint(Graphics g) {
  104.         int num = 0;
  105.         for(int y = 0; y < bground.getHeight()/tileh; y++){
  106.             for(int x = 0; x < bground.getWidth()/tilew; x++){
  107.                 g.drawImage(db[num], x*realh, y*realh, null);
  108. //              System.out.print("height: " + bground.getHeight() + " width: " + bground.getWidth() + '\n');
  109. //              System.out.print("x: " + x + " y: " + y + " x*y: " + (x*y) + '\n');
  110.                 num++;
  111.             }
  112.         }
  113.     }
  114.    
  115.     public static BufferedImage loadImage(String ref) {  
  116.         BufferedImage bimg = null;  
  117.         try {
  118.             bimg = ImageIO.read(new File(ref));  
  119.         } catch (Exception e) {  
  120.             e.printStackTrace();  
  121.         }  
  122.         return bimg;  
  123.     }
  124.  
  125.     public static BufferedImage loadOnlineImage(String ref) {  
  126.         BufferedImage bimg = null;  
  127.         try {
  128.             bimg = ImageIO.read(new URL(ref));
  129.         } catch (Exception e) {  
  130.             e.printStackTrace();  
  131.         }  
  132.         return bimg;  
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement