RSS

Monthly Archives: June 2012

Info

So, hey guyz, I am thinking of taking a break just for the weekend. I check up on some really cool stuff. Some of the stuff that I’ve been working on:

  1. Java Networking – I am still looking at that. I know I promised to post the resources and I will I am finalising.
  2. More android stuff.
  3. Java Swings and graphics and anything java
  4. C++

Cheers.

 
Leave a comment

Posted by on June 29, 2012 in Uncategorized

 

A Simple scribbling app

This one shows the power of the MouseListener and the MouseMotionListener. Its advisable to implement it using the MouseAdapter and the MouseMotionAdapter.
Anyways,


package wordpress.mycodeandlife.com;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;

public class PaintStuff extends JFrame{
	int prev_x, prev_y;
	
	public PaintStuff(){
		
		super("Cool");
		// adds a Mouse Listener and so as to avoid implementing all the methods, use the MouseAdapter
		addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				prev_x= e.getX();
				prev_y=e.getY();
				
			}
			
		});
		//Add the mouseMotionListener containing the mouseDragged and mouseMoved methods
		addMouseMotionListener(new MouseMotionAdapter(){
			public void mouseDragged(MouseEvent e){
				int x= e.getX();
				int y = e.getY();
				
				drawStuff(prev_x, prev_y, x, y);
				
				prev_x = x;
				prev_y = y;
			}
			
		});
		
		setVisible(true);
		setSize(800,800);
	}
	
	public void drawStuff(int x0, int y0, int x1, int y1){
		//will draw the line
		this.getGraphics().drawLine(x0, y0, x1, y1);
		
	}
	
	public static void main(String args[]){
		PaintStuff drawing = new PaintStuff();
		
	}
}


 
Leave a comment

Posted by on June 28, 2012 in Uncategorized

 

Java Networking: Enabling your app to access internet through a proxy server

So in my earlier blog post I made a web browser and all, and the first problem I encountered was getting it to connect to the internet. Why this is a problem is because I access the internet through a proxy server. Most institutions have proxies and can only access the internet at a particular port so its kind of nice to let them connect. Its just 3 lines of code:

System.getProperties().put("proxySet","true"); //Tells java that you'll be connecting through a proxy
System.getProperties().put("proxyHost","10.223.0.2"); // and the port the server is listening in
System.getProperties().put("proxyPort","3128"); //specifies the servers ip

 

 
Leave a comment

Posted by on June 28, 2012 in Code, Java

 

Tags: ,

Building the worst browser in Java

This is gonna be the worst browser you’ve ever seen. Java has a very poor support for Css/Javascript. But if you are trying to load a page in the web written a decade ago, then you just struck the jackpot. Anyway I’ll use this later on to show how other swing objects can be used.

and if you wanna try it out and improve it:

package wordpress.mycodeandlife.com;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class WebBrowser extends JFrame implements ActionListener, HyperlinkListener{
	JTextField addressbar;
	JEditorPane page;
	JLabel status;

	WebBrowser(){
		super("gilo");
		addressbar = new JTextField();
		addressbar.addActionListener(this);

		page = new JEditorPane();
		page.setEditable(false);
		page.addHyperlinkListener(this);

		status = new JLabel();
		status.setText("Welcome to gilo");

		add(addressbar,BorderLayout.NORTH);
		add(status, BorderLayout.SOUTH);
		add(new JScrollPane(page),BorderLayout.CENTER);

		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(500,500);

		//These three are the proxy settings
		System.getProperties().put("proxySet","true");
		System.getProperties().put("proxyPort","3128");
		System.getProperties().put("proxyHost","10.223.0.2");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		loadpage(e.getActionCommand());

	}

	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
			loadpage(e.getURL().toString());
		else if(e.getEventType()==HyperlinkEvent.EventType.ENTERED)
			status.setText(e.getURL().toString());
		else if(e.getEventType() == HyperlinkEvent.EventType.EXITED)
			status.setText(" ");

	}

	public void loadpage(String url){

		status.setText("Loading "+ url + "....");
		try {

			page.setPage(url);
			addressbar.setText(url);
			this.setTitle(url);
			status.setText(url);

		} catch (IOException e) {
			status.setText("Error unable to connect to: " + url);
		}
	}

	public static void main(String args[]){

		WebBrowser brow = new WebBrowser();

	}

}

 
Leave a comment

Posted by on June 28, 2012 in Code, Java

 

Tags: ,

Making a toast

One of the components that android phones come with is the Toast. It sort of gives information to the user about something, just like in the above case. Its supposed to inform us that the wallpaper has been set. It’s really reassuring, I gotta say. Ok so, there are a few ways you can do it (its really one way actually but you’ll see):

Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,"This is a toast that will say: Wallpaper set",duration);
toast.show();

or

Toast.makeText(getApplicationContext(), "This is a toast that will say: Wallpaper set", Toast.LENGTH_LONG).show();

So as you can see its the Toast.makeText() that builds the toast. It takes three parameters: the context, text to be displayed and the duration of display. You can still make custom toast that display images and other stuff as opposed to text alone. So watch out for this space.

click here to view the full app code.

 
1 Comment

Posted by on June 27, 2012 in Android, Android tutorials, Code

 

Tags:

Android app Changing the wallpaper

Hello guys so I modified the earlier code(Camera app) to be able to change the wallpaper at the homescreen. I  should mention that you have to add permission for changing wallpaper:

<uses-permission android:name="android.permission.SET_WALLPAPER"/>

Change it at the android manifest. You may also consider changing the orientation to portrait. The code for changing the wallpaper is

getApplicationContext().setWallpaper(bmp);

It should be surrounded with try-catch statements.
So now the final app looks like:


ps/ that isnt me in the photo.

Finally the code:

package com.learning.gilo;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Camera extends Activity implements View.OnClickListener{

	Button wall,take;
	ImageView pic;
	Intent i;
	int cameraData = 0;
	Bitmap bmp;

	protected void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.camera);

		InputStream is = getResources().openRawResource(R.drawable.back_mao);
		bmp = BitmapFactory.decodeStream(is);
		wall = (Button) findViewById(R.id.bSet);
		take = (Button) findViewById(R.id.bTake);
		pic = (ImageView) findViewById(R.id.ivImage);

		wall.setOnClickListener(this);
		take.setOnClickListener(this);

	}

	public void onClick(View v){
		switch(v.getId()){
		case R.id.bSet:
			try {
			   getApplicationContext().setWallpaper(bmp);
                           Toast.makeText(getApplicationContext(), "This is a toast that will say \"Wallpaper set\"", Toast.LENGTH_LONG).show();
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		case R.id.bTake:
			i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(i,cameraData);
			break;

		}
}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			Bundle extras = data.getExtras();
			bmp = (Bitmap) extras.get("data");
			pic.setImageBitmap(bmp);

		}

	}
}

 
1 Comment

Posted by on June 27, 2012 in Android, Android tutorials, Code

 

Tags:

Android Intent for a camera app

Yesterday I checked out the email Intent that was a bit straightforward than this. I am going to discuss this more in another blog post. But for now here is the source code fo the camera app and how it looks.

package com.learning.gilo;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class Camera extends Activity implements View.OnClickListener{
	
	Button wall,take;
	ImageView pic;
	Intent i;
	int cameraData = 0;
	Bitmap bmp;
	
	protected void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.camera);
		
		InputStream is = getResources().openRawResource(R.drawable.back_mao);
		bmp = BitmapFactory.decodeStream(is);
		wall = (Button) findViewById(R.id.bSet);
		take = (Button) findViewById(R.id.bTake);
		pic = (ImageView) findViewById(R.id.ivImage);
		
		
		wall.setOnClickListener(this);
		take.setOnClickListener(this);
		
		
	}
	
	public void onClick(View v){
		switch(v.getId()){
		case R.id.bSet:
			try {
				
				/*
				 * This is a how a toast is made check out
				 * https://mycodeandlife.wordpress.com/2012/06/27/making-a-toast/
				 */
				getApplicationContext().setWallpaper(bmp);
				Toast.makeText(getApplicationContext(), "This is a toast that will say \"Wallpaper set\"", Toast.LENGTH_LONG).show();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		case R.id.bTake:
			i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(i,cameraData);
			break;
		
		}
}
	
	/*This will enable the menu to show up when you click menu check out
	 * https://mycodeandlife.wordpress.com/
	 */
	public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		MenuInflater showMenu = getMenuInflater();
		showMenu.inflate(R.menu.nice_menu, menu);
		
		return true;
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			Bundle extras = data.getExtras();
			bmp = (Bitmap) extras.get("data");
			pic.setImageBitmap(bmp);
			
		}
		
		
	}
}

 

More Info.

I finally got a way to post my source code in a readable format. Totally love it. I found it here

 
Leave a comment

Posted by on June 27, 2012 in Uncategorized

 

Android Creating an Email Intent

Today I checked out about the android email Intent. Built a pretty simple app that doesn’t do much but passes on some info to the default email application in the phone. Btw guys I am sorry about the format of the code. I also have to mention: I dont know why but you cant send email through your emulator with this app, It only works on a real phone

.


//Code starts here

package com.learning.gilo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SendEmail extends Activity implements View.OnClickListener{

Button send;
EditText email, message,subj;

protected void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.email);
send = (Button) findViewById(R.id.bSend);
email = (EditText) findViewById(R.id.etEmail);
message = (EditText) findViewById(R.id.etBody);
subj = (EditText) findViewById(R.id.etSubject);

send.setOnClickListener(this);

}

public void onClick(View v) {
String emailaddress = email.getText().toString();
String[] address = {emailaddress};

String body = message.getText().toString();

String subject = subj.getText().toString();

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); //This is the email intent
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, address); // adds the address to the intent
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);//the subject

emailIntent.setType("plain/text");

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body); //adds the body of the mail

startActivity(emailIntent);

}
}

 
5 Comments

Posted by on June 26, 2012 in Android, Android tutorials

 

Information

Yesterday was a really cool day, the Java Networking page got some hits and some very interesting criticism that I’d like to address. First off if you haven’t noticed I’m sorta new in blogging stuff, so sorry for the format of the blog especially for the code snippets. If you do know how to display programming codes in wordpress just hit the comment button. Secondly, maybe I did forget to mention but the code is not perfect, it still needs a lot of work. It is also not the current approach to networking. I do hope to check up on that.

Cheers

 

 

 

 
Leave a comment

Posted by on June 26, 2012 in Uncategorized

 

Tags: