8 October, 2011 - 6:38 pm
Tags: flatten, lists, programming, Python
Posted in english | 1 comment
Surprisingly python doesn’t have a shortcut for flatten a list (more generally a list of lists of lists of…). I made a simple implementation that doesn’t use recursion and tries to be written clearly. I get a element from a “bad” list (a list that can have another lists). If the element is not a [...]
/** * Openpixels example in Processing. * This simple example of how to get a sprite * from a sprite sheet. */ PImage bg; PImage sprite_sheet; PImage player; void setup() { // load images bg = loadImage("kitchen.png"); sprite_sheet = loadImage("guy.png"); /* The sprite size is 32×49. Look guy.png, the "stand position" is [...]
This was a project that me and Marco Diego created during our graduation for the Computer Graphics course. It is a ray tracing engine build from scratch in C. It was great exercise of experimentation on how implement object-oriented design patterns in ANSI C. Later Marco continued it in his master’s degree thesis implementing more [...]
Context ctx = getContext(); Display display = ((WindowManager)ctx.getSystemService(ctx.WINDOW_SERVICE)).getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); Yes, there are easier ways to retrieve the screen width on Android but there are cases that this long code is the only solution. You may already have the Context. WindowManager or the Display and so it would be [...]
9 March, 2011 - 12:52 pm
Tags: array, array_shift, PHP, programming
Posted in english | No comments
$bric = array("Brazil", "Russia", "India", "China"); $ric = $bric; // array copy $br = array_shift($ric); // left shift at $ric. $br stores "Brazil" print_r($bric); // $bric remains the same print_r($ric); // $ric lost "Brazil" Output: Array ( [0] => Brazil [1] => Russia [2] => India [3] => China ) Array ( [0] => Russia [...]
29 April, 2010 - 10:29 am
Tags: Class, Java, method, programming, Reflection
Posted in english | 2 comments
import java.lang.reflect.*; public class Foo { public void bar(int param){ System.out.println(param); } public static void main(String args[]){ Object f = new Foo(); try { Method m = f.getClass().getMethod("bar", int.class); m.invoke(f, 42); } catch (Exception e){ System.err.println(e); } } } $ java Foo 42
8 April, 2010 - 1:09 pm
Tags: getruntime, Java, programming, runtime
Posted in english | No comments
I don’t like the approach of calling native shell commands in any language instead of using multi platform libraries, but here is a little prof of concept Java program to call native commands. import java.io.*; import java.util.*; public class Exec { public static void main(String args[]) throws IOException { Process proc = Runtime.getRuntime().e xec(args); BufferedReader [...]
Iterating over a HashMap using the enhanced loop (foreach) in Java is a good way to keep your code smaller, more legible and usually more semantically coherent. import java.util.HashMap; import java.util.Map; class Foo {} public class Main { public static void main(String args[]){ Map<Byte, Foo> mHash; mHash = new HashMap<Byte, Foo>(); [...]
18 March, 2010 - 12:00 am
Tags: Android, branch, cmd, git, programming
Posted in english | 1 comment
Getting the Android’s AlarmClock application source from official repositories: git clone git://android.git.kernel.org/platform/packages/apps/AlarmClock.git To get the head version for an old platform like the 1.4 (codename donut), choose the correspondent branch using -o or –origin: git clone git://android.git.kernel.org/platform/packages/apps/AlarmClock.git –origin donut
This is a simple program I wrote called Who Am I that shows informations about the device which it is running. Which can be useful for developers and maybe advanced users. Download: WhoAmI.tar.bz2 – Eclipse project. It’s configured for Android platform 4 (1.6) but should work without problems in newer Android platform versions. WhoAmI.apk – [...]
13 June, 2010 - 5:05 pm
users can add new ships can u help me??
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class SpaceShip extends JFrame implements KeyListener, MouseListener, Runnable
{
int k=0;
public static void main(String[] args)
{ new SpaceShip().setVisible(true); }
Thread runner;
int speed = 100; //sleeps for 100 milliseconds, decrease if you want faster and increse if you want it slower
JLabel lblShip = new JLabel(new ImageIcon(“c://spaceship.jpg”));
JLabel lblShip2 = new JLabel(new ImageIcon(“c://spaceship1.jpg”));
//1. gemi değişkenleri:
static int x = 450, y = 0, width = 50, height = 50, incAmountX = 0, incAmountY = 0;
boolean shoot = false;
int shootX = 0, shootY = 0;
//2. gemi değişkenleri:
static int x2 = 0, y2 = 200, width2 = 50, height2 = 50, incAmountX2 = 0, incAmountY2 = 0;
boolean shoot2 = false;
int shootX2 = 0, shootY2 = 0;
public SpaceShip()
{
super(“SpaceShip Wars”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLayout(null);
lblShip.setBounds(x, y, 35, 30);
add(lblShip);
lblShip2.setBounds(x2, y2, 35, 30);
add(lblShip2);
addKeyListener(this);
addMouseListener(this);
runner = new Thread(this);
runner.start();
}
public void keyPressed(KeyEvent e)
{
shoot = shoot2 = false;
switch(e.getKeyCode())
{
//1. gemi kontrolleri ok tuşları
case KeyEvent.VK_UP: incAmountY = -1; incAmountX = 0; break;
case KeyEvent.VK_DOWN: incAmountY = 1; incAmountX = 0; break;
case KeyEvent.VK_LEFT: incAmountX = -1; incAmountY = 0; break;
case KeyEvent.VK_RIGHT: incAmountX = 1; incAmountY = 0; break;
//2. gemi kontrolleri w-a-s-d tuşları
case KeyEvent.VK_W: incAmountY2 = -1; incAmountX2 = 0; break;
case KeyEvent.VK_S: incAmountY2 = 1; incAmountX2 = 0; break;
case KeyEvent.VK_A: incAmountX2 = -1; incAmountY2 = 0; break;
case KeyEvent.VK_D: incAmountX2 = 1; incAmountY2 = 0; incAmountX3 = 1; incAmountY3 = 0;
break;
//gemileri durdurmak için escape
case KeyEvent.VK_ESCAPE: incAmountX = incAmountY = incAmountX2 = incAmountY2 = 0; break;
}
lblShip.setBounds(x, y, width, height);
lblShip2.setBounds(x2, y2, width2, height2);
}
public void run()
{
while(true)
{
try
{
runner.sleep(speed);
x += incAmountX;
y += incAmountY;
lblShip.setBounds(x, y, width, height);
x2 += incAmountX2;
y2 += incAmountY2;
lblShip2.setBounds(x2, y2, width2, height2);
lblShip3.setBounds(x3, y3, width3, height3);
x3 += incAmountX3;
y3 += incAmountY3;
repaint();
}
catch (InterruptedException e)
{ e.printStackTrace(); }
}
}
public void paint(Graphics g)
{
super.paint(g);
if(shoot)
g.drawLine(shootX, shootY, x + 20, y + 50);
else if(shoot2)
g.drawLine(shootX2, shootY2, x2 + 20, y2 + 50);
}
public void mousePressed(MouseEvent e)
{
//sol tuş basılıysa 1. gemi ateş eder:
if(e.getButton() == 1)
{
shoot = true;
shootX = e.getX();
shootY = e.getY();
System.out.println(shootX + ” ” + shootY + ” : ” + x2 + ” “+ y2);
//2. gemi menzilde mi diye kontrol et, eğer menzildeyse oyunu bitir:
if(shootX >= x2 – 60 && shootX = y2 – 80 && shootY = x – 30 && shootX2 = y – 40 && shootY2 <= y + 40)
{
runner.stop();
JOptionPane.showMessageDialog(null, "2. gemi 1. gemiyi vurdu, oyun bitti");
}
}
repaint();
}
//elini mouse'tan kaldırınca ateşi kessin.
public void mouseReleased(MouseEvent arg0)
{
shoot = shoot2 = false;
repaint();
}
public void keyReleased(KeyEvent arg0)
{ }
public void keyTyped(KeyEvent arg0)
{ }
public void mouseClicked(MouseEvent e)
{ }
public void mouseEntered(MouseEvent arg0)
{ }
public void mouseExited(MouseEvent arg0)
{ }
}
8 December, 2010 - 5:31 am
What a wonderful article! Thanks for sharing this on your blog…