NekoCM Util - Printer

>> Wednesday, April 28, 2010

This is a little utility I'm working on. It's still under development. Basically is a simple way to print something using Java.
Normally to print something you will need to use the Print API, which is pretty annoying, so i googled and found this tutorial, it worked very good! For simple purposes is the easiest way by far.
I used it and by using it I realised I could make an API with the simple stuff I was doing, I planned on improving it later, and that would be some work because you would normally want to do that when using that tutorial source code, so I decided to do that, the result is very nice! It really helped me.
Right now the first NekoCM Util class is about printing, under the package com.blogspot.nekocm.util.print.
The idea behind that is basically make a print preview window, in the window there is a JPanel in which you draw what the page (Currently only A4 size) is gonna look like. And that's it! No worries about anything else, here is the example code.

PrintExample.java

import javax.swing.*;

import com.blogspot.nekocm.util.print.PreviewPrint;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PrintExample extends JFrame {
 public PrintExample() {
  super();
  this.setTitle("NekoCM - Print example");
  this.setSize(new Dimension(300, 200));
  this.setVisible(true);
  this.setLocationRelativeTo(null);
  this.setLayout(new FlowLayout());
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JButton btn_print = new JButton("Print");
  btn_print.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    new PreviewPrint(new PrintExamplePanel());
   }
  });
  this.getContentPane().add(btn_print);

 }

 public static void main(String[] args) {
  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  } catch (Exception e) {
   // best to do nothing here
  }
  new PrintExample();
 }
}

PrintExamplePanel.java

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;

import com.blogspot.nekocm.util.print.PreviewPanel;

public class PrintExamplePanel extends PreviewPanel {

 public PrintExamplePanel() {
  super("A4");
 }

 @Override
 public void paint(Graphics2D g) {
  Graphics2D g2d = (Graphics2D) g;
  g2d.setColor(Color.WHITE);
  g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
  g2d.setColor(Color.BLACK);
  g2d.setFont(new Font("Arial", Font.PLAIN, 20));
  this.writeAligned(20, "center", "Neko CM", g2d);
  g2d.setFont(new Font("Arial", Font.PLAIN, 11));
  this.writeAligned(-1, "left", "\nThis is an example\n"
    + "of NekoCM Print lib", g2d);
  this.writeAligned(-1, "right", "Enjoy~", g2d);
  this.writeAligned(-1, "center", "\n\nhttp://nekocm.blogspot.com", g2d);

 }

}

That will produce something like this



You can now download the Eclipse example project or just download the api.
Hope you find this useful! I will update this as soon as I improve it, although right now I'm more into BIP (which is also improving by the way ;))

Read more...

Generic Game

>> Saturday, April 24, 2010

BIP is 60% finished, and my first project with it, may be an overkill, aiming to improve it, (although it does not requires a tile map! which is what BIP is needing right now) I'll make a fighting game, but i want the game to be somehow like MUGEN, so you can customize the characters, each attack, animation, etc. I have interesting ideas, which I will post if i succeed with the first steps. Whish me luck!

Read more...

BIP - Managers

>> Thursday, April 22, 2010

I added a Sprite Manager and Audio Manager to BIP, now is very easy to load and play sounds. The Sprite Manager basically is a Vector with many sprites, so is easier to update and draw them all, is somewhat similar to DarkGDK way of managing sprites if you want to think it like that, you can check for collision doing something like this:

if(manager.pixelCollide(1, 5)) { /* Collision! */ }

That will check for collision between sprites 1 and 5.
Also I added a Button class inside the "gui" package, it's a very basic abstract class, the way you make buttons will be inherit from that class, and define your own actionPerformed method, then just call it with an image and a location, and optionally an image for the hover state.

BIP seems to be doing pretty nice so far, so I'm planning on using it in a small project now, which do not need tiled maps, to test it and improve it.

Read more...

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP