Home » Posts tagged "Java"

Creating TimeSeries chart with jfreechart

Second chart generated by Mavis – chart with my yearly activities with games and books, by each month. As previously described bar chart I use jfreechart to generate data.

Read more

Creating Bar chart with jfreechart

For last few weeks I was working on Mavis – little tool to generate data for posts on this, and personal blogs. Core feature is generating charts describing my monthly and yearly activities, so I’ll write some posts about generating charts with jfreechart library. Read more

Using Travis.CI with Java Eclipse Project

Lately I set up Travis.CI for my Mavis project. It’s hosted continuous integration service free for open source projects. It’ll build your project and, optionally, run tests after every push to remote repository that contains travis.yml configuration file.

As I never really used ant or travis.ci before it took me too many hours to actually setup it correctly, so I figured I’ll share new knowledge.
Read more

MapView

The most challenging part of Asokoban was creating a map to play on.

Now I wonder if it wasn’t an overkill but with experience from WinForms I decided to implement map with a Canvas on a custom View called MapView, because sokoban is a game that does not require much frame-rate speed – map should be redrawed after each move, and time gap between moves varies according to time player spend on thinking. Read more

Common menu in android apps

Little trick, that goes well with DRY rule.
Every activity on android can contain menu defined in onCreateOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem item) methods. But what to do if we have multiple activities that share the same menu? Copy and paste methods? Better not. API Guides at android developers come with a trick: when multiple activities need to provide the same, common, menu a best way to do it is through inheritance.

In Asokoban I wanted a common menu with “about” and “highscores” options, that would show basic info about program, and highscores activity. And in-game activity menu should also contain a restart option. How?
I created a MenuActivity as a “empty” activity that implemented only methods connected with menu. Standard and one that implements tasks specific for options item. Example:

public class MenuActivity extends Activity {

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_about:
			showAbout();
			return true;
		case R.id.menu_highscores:
			showScores();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void showAbout(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(R.string.about_content)
	       	   .setTitle(R.string.menu_about);
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	private void showScores(){
		Intent intent = new Intent(this, HighscoreActivity.class);
		
		startActivity(intent);
	}
}

Then all that’s left is to extend this class for every activity in program. And – in case of ingame activity with reset function – override onCreate and OnSelected methods with additional menu content. Example:

	@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE,ID_MENU_RESTART,120,R.string.menu_restart);
return true;
}