Pages

Sunday, October 3, 2021

Migrating Async Tasks - Android 11

Background:
AsyncTask is deprecated from Android 11. Google has recommened to use 
Executors and Handlers to achieve the background computation and UI updates. 

Challenge:
For larger enterprise applications/projects (with more AsyncTasks), 
replacing the asynctask with Executors/Handlers, could be an uphill task.  
It would be even more challenging, if the existing async task does lot 
of complex background computations and more UI interactions and the 
author/SME is not around. 

Migrating them would require more code changes, more testing and 
it involves more risk of breaking the functionality. 

If your project is still in JAVA, please continue reading here. 
For Kotlin, you might want to leverage the benefits of coroutines, 
which is not explained here.

Solution:
Create a base class that exposes similar AsyncTask API but internally 
it uses executors and handlers. For migration, instead of extending 
AsyncTask, just extend the new base class.

GitHub:
https://github.com/sudhans/ExecutorAsyncTask

Limitations/Differences:
The above snippet is not strict like the async task in throwing 
exceptions if the execute() method is called after the asynctask is complete.

Implement a ThreadFactory, if you would like to give the custom 
thread names for the executor.


Code Snippet:
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.AnyThread;
import androidx.annotation.MainThread;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;

public abstract class AsyncTaskV2<Params, Progress, Result> {

    public enum Status {
        FINISHED,
        PENDING,
        RUNNING
    }

    // This handler will be used to communicate with main thread
    private final Handler handler = new Handler(Looper.getMainLooper());
    private final AtomicBoolean cancelled = new AtomicBoolean(false);

    private Result result;
    private Future<Result> resultFuture;
    private ExecutorService executor;
    private Status status = Status.PENDING;

    // Base class must implement this method
     protected abstract Result doInBackground(Params params);

    // Methods with default implementation
    // Base class can optionally override these methods.
    protected void onPreExecute() {}
    protected void onPostExecute(Result result) {}
    protected void onProgressUpdate(Progress progress) {}
    protected void onCancelled() {}
    protected void onCancelled(Result result) {
        onCancelled();
    }


    @MainThread
    public final Future<Result> execute(@Nullable Params params) {
        status = Status.RUNNING;
        onPreExecute();
        try {
            executor = Executors.newSingleThreadExecutor();
            Callable<Result> backgroundCallableTask = () ->  doInBackground(params);
            // Execute the background task
            resultFuture = executor.submit(backgroundCallableTask);
            
            // On the worker thread - wait for the background task to complete
            executor.submit(this::getResult);
            return resultFuture;
        } finally {
            if (executor != null) {
                executor.shutdown();
            }
        }
    }

    private Runnable getResult() {
        return () -> {
            try {
                if (!isCancelled()) {
                    // This will block the worker thread, till the result is available
                    result = resultFuture.get();
                    
                    // Post the result to main thread
                    handler.post(() -> onPostExecute(result));
                } else {
                    // User cancelled the operation, ignore the result
                    handler.post(this::onCancelled);
                }
                status = Status.FINISHED;
            } catch (InterruptedException | ExecutionException e) {
                Log.e("Exception while trying to get result ", e.getMessage());
            }
        };
    }

    @WorkerThread
    public final void publishProgress(Progress progress) {
        if (!isCancelled()) {
            handler.post(() -> onProgressUpdate(progress));
        }
    }

    @MainThread
    public final void cancel(boolean mayInterruptIfRunning) {
        cancelled.set(true);
        if (resultFuture!= null) {
            resultFuture.cancel(mayInterruptIfRunning);
        }
    }

    @AnyThread
    public final boolean isCancelled() {
        return cancelled.get();
    }

    @AnyThread
    public final Status getStatus() {
        return status;
    }

}

Sunday, October 16, 2011

Line Arts - Android Live Wallpaper

Hello Guys,
  This article is not a tutorial on Android canvas or Android Live Wallpaper. Given below is the code snippet of Android Canvas that draws a line diagram of Actors Dhanush and Chaya Singh from the movie 'Thiruda Thirudi'.  The code was originally written by me using C and then modified to fit Android Canvas.
   I made a live wall paper out of it and it is available in Android market as Free  for 2.1 or more versions of Android. The app supports all screen sizes and rotates the wall paper based on the angle in which the phone is tilted. However, via settings, you can enable/disable rotation based on sensor and control the gradient change speed. You can double tap on the screen to scale the image to your choice.

 https://market.android.com/details?id=com.sudhan.livewp&feature=search_result





If you are looking for Live Wall Paper Tutorial, check it out here,
    http://developer.android.com/resources/articles/live-wallpapers.html http://www.vogella.de/articles/AndroidLiveWallpaper/article.html http://blog.androgames.net/58/android-live-wallpaper-tutorial/
Code Snippet:


/* Draw Dhanush And Chaya Singh */
void drawLineDiagram(Canvas canvas) 
{
int x = 120, y = 5;
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(0);
paint.setStyle(Paint.Style.STROKE);
canvas.drawLine(130 - x, 452 + y, 128 - x,448 + y, paint);
canvas.drawLine(128 - x, 448 + y, 126 - x,450 + y, paint);
canvas.drawLine(126 - x, 450 + y, 110 - x,449 + y, paint);
canvas.drawLine(110 - x, 449 + y, 110 - x,436 + y, paint);
canvas.drawLine(110 - x, 436 + y, 107 - x,433 + y, paint);
canvas.drawLine(107 - x, 433 + y, 111 - x,415 + y, paint);
canvas.drawLine(111 - x, 415 + y, 120 - x,395 + y, paint);
canvas.drawLine(120 - x, 395 + y, 121 - x,395 + y, paint);
canvas.drawLine(121 - x, 395 + y, 122 - x,394 + y, paint);
canvas.drawLine(122 - x, 394 + y, 132 - x,350 + y, paint);
canvas.drawLine(132 - x, 350 + y, 133 - x,350 + y, paint);
canvas.drawLine(133 - x, 350 + y, 135 - x,348 + y, paint);
canvas.drawLine(135 - x, 348 + y, 135 - x,345 + y, paint);
canvas.drawLine(135 - x, 345 + y, 138 - x,339 + y, paint);
canvas.drawLine(138 - x, 339 + y, 143 - x,337 + y, paint);
canvas.drawLine(143 - x, 337 + y, 144 - x,336 + y, paint);
canvas.drawLine(144 - x, 336 + y, 144 - x,330 + y, paint);
canvas.drawLine(144 - x, 330 + y, 147 - x,327 + y, paint);
canvas.drawLine(147 - x, 327 + y, 147 - x,324 + y, paint);
canvas.drawLine(147 - x, 324 + y, 148 - x,323 + y, paint);
canvas.drawLine(148 - x, 323 + y, 149 - x,322 + y, paint);
canvas.drawLine(149 - x, 322 + y, 149 - x,321 + y, paint);
canvas.drawLine(149 - x, 321 + y, 148 - x,318 + y, paint);
canvas.drawLine(148 - x, 318 + y, 148 - x,317 + y, paint);
canvas.drawLine(148 - x, 317 + y, 150 - x,314 + y, paint);
canvas.drawLine(150 - x, 314 + y, 150 - x,285 + y, paint);
canvas.drawLine(150 - x, 285 + y, 148 - x,283 + y, paint);
canvas.drawLine(148 - x, 283 + y, 151 - x,280 + y, paint);
canvas.drawLine(151 - x, 280 + y, 151 - x,278 + y, paint);
canvas.drawLine(151 - x, 278 + y, 150 - x,277 + y, paint);
canvas.drawLine(150 - x, 277 + y, 152 - x,276 + y, paint);
canvas.drawLine(152 - x, 276 + y, 151 - x,262 + y, paint);
canvas.drawLine(151 - x, 262 + y, 149 - x,260 + y, paint);
canvas.drawLine(149 - x, 260 + y, 146 - x,250 + y, paint);
canvas.drawLine(146 - x, 250 + y, 145 - x,249 + y, paint);
canvas.drawLine(145 - x, 249 + y, 144 - x,248 + y, paint);
canvas.drawLine(144 - x, 248 + y, 143 - x,248 + y, paint);
canvas.drawLine(143 - x, 248 + y, 137 - x,246 + y, paint);
canvas.drawLine(137 - x, 246 + y, 137 - x,242 + y, paint);
canvas.drawLine(137 - x, 242 + y, 139 - x,240 + y, paint);
canvas.drawLine(139 - x, 240 + y, 141 - x,240 + y, paint);
canvas.drawLine(141 - x, 240 + y, 145 - x,239 + y, paint);
canvas.drawLine(145 - x, 239 + y, 146 - x,236 + y, paint);
canvas.drawLine(146 - x, 236 + y, 140 - x,230 + y, paint);
canvas.drawLine(140 - x, 230 + y, 144 - x,222 + y, paint);
canvas.drawLine(144 - x, 222 + y, 139 - x,223 + y, paint);
canvas.drawLine(139 - x, 223 + y, 140 - x,220 + y, paint);
canvas.drawLine(140 - x, 220 + y, 146 - x,215 + y, paint);
canvas.drawLine(146 - x, 215 + y, 147 - x,210 + y, paint);
canvas.drawLine(147 - x, 210 + y, 150 - x,208 + y, paint);
canvas.drawLine(150 - x, 208 + y, 158 - x,196 + y, paint);
canvas.drawLine(158 - x, 196 + y, 150 - x,192 + y, paint);
canvas.drawLine(150 - x, 192 + y, 140 - x,182 + y, paint);
canvas.drawLine(140 - x, 182 + y, 125 - x,192 + y, paint);
canvas.drawLine(100 - x, 197 + y, 98 - x,187 + y, paint);
canvas.drawLine(98 - x, 187 + y, 102 - x,188 + y, paint);
canvas.drawLine(98 - x, 187 + y, 102 - x,175 + y, paint);
canvas.drawLine(102 - x, 175 + y, 100 - x,173 + y, paint);
canvas.drawLine(100 - x, 173 + y, 102 - x,171 + y, paint);
canvas.drawLine(102 - x, 171 + y, 108 - x,155 + y, paint);
canvas.drawLine(108 - x, 155 + y, 106 - x,155 + y, paint);
canvas.drawLine(106 - x, 155 + y, 114 - x,137 + y, paint);
canvas.drawLine(121 - x, 127 + y, 126 - x,122 + y, paint);
canvas.drawLine(126 - x, 122 + y, 128 - x,122 + y, paint);
canvas.drawLine(128 - x, 122 + y, 132 - x,118 + y, paint);
canvas.drawLine(132 - x, 118 + y, 130 - x,116 + y, paint);
canvas.drawLine(130 - x, 116 + y, 135 - x,114 + y, paint);
canvas.drawLine(135 - x, 114 + y, 136 - x,110 + y, paint);
canvas.drawLine(136 - x, 110 + y, 140 - x,109 + y, paint);
canvas.drawLine(140 - x, 109 + y, 142 - x,105 + y, paint);
canvas.drawLine(142 - x, 105 + y, 140 - x,104 + y, paint);
canvas.drawLine(140 - x, 104 + y, 137 - x,100 + y, paint);
canvas.drawLine(137 - x, 100 + y, 126 - x,75 + y, paint);
canvas.drawLine(126 - x, 75 + y, 127 - x,67 + y, paint);
canvas.drawLine(127 - x, 67 + y, 129 - x,60 + y, paint);
canvas.drawLine(129 - x, 60 + y, 128 - x,50 + y, paint);
canvas.drawLine(128 - x, 50 + y, 132 - x,47 + y, paint);
canvas.drawLine(132 - x, 47 + y, 140 - x,43 + y, paint);
canvas.drawLine(140 - x, 43 + y, 140 - x,40 + y, paint);
canvas.drawLine(140 - x, 40 + y, 147 - x,41 + y, paint);
canvas.drawLine(147 - x, 41 + y, 150 - x,42 + y, paint);
canvas.drawLine(150 - x, 42 + y, 148 - x,38 + y, paint);
canvas.drawLine(148 - x, 38 + y, 154 - x,43 + y, paint);
canvas.drawLine(154 - x, 43 + y, 157 - x,43 + y, paint);
canvas.drawLine(157 - x, 43 + y, 159 - x,41 + y, paint);
canvas.drawLine(159 - x, 41 + y, 161 - x,42 + y, paint);
canvas.drawLine(161 - x, 42 + y, 170 - x,41 + y, paint);
canvas.drawLine(170 - x, 41 + y, 177 - x,44 + y, paint);
canvas.drawLine(177 - x, 44 + y, 184 - x,48 + y, paint);
canvas.drawLine(184 - x, 48 + y, 190 - x,57 + y, paint);
canvas.drawLine(190 - x, 57 + y, 188 - x,58 + y, paint);
canvas.drawLine(188 - x, 58 + y, 189 - x,62 + y, paint);
canvas.drawLine(189 - x, 62 + y, 186 - x,64 + y, paint);
canvas.drawLine(186 - x, 64 + y, 188 - x,68 + y, paint);
canvas.drawLine(188 - x, 68 + y, 188 - x,69 + y, paint);
canvas.drawLine(188 - x, 69 + y, 187 - x,70 + y, paint);
canvas.drawLine(187 - x, 70 + y, 193 - x,78 + y, paint);
canvas.drawLine(193 - x, 78 + y, 193 - x,80 + y, paint);
canvas.drawLine(193 - x, 80 + y, 189 - x,82 + y, paint);
canvas.drawLine(189 - x, 82 + y, 189 - x,85 + y, paint);
canvas.drawLine(189 - x, 85 + y, 186 - x,88 + y, paint);
canvas.drawLine(187 - x, 88 + y, 189 - x,90 + y, paint);
canvas.drawLine(189 - x, 90 + y, 188 - x,94 + y, paint);
canvas.drawLine(188 - x, 94 + y, 190 - x,99 + y, paint);
canvas.drawLine(190 - x, 99 + y, 190 - x,100 + y, paint);
canvas.drawLine(190 - x, 100 + y, 189 - x,101 + y, paint);
canvas.drawLine(189 - x, 101 + y, 186 - x,103 + y, paint);
canvas.drawLine(186 - x, 103 + y, 176 - x,101 + y, paint);
canvas.drawLine(176 - x, 101 + y, 170 - x,107 + y, paint);
canvas.drawLine(170 - x, 107 + y, 175 - x,112 + y, paint);
canvas.drawLine(175 - x, 112 + y, 178 - x,116 + y, paint);
canvas.drawLine(178 - x, 116 + y, 182 - x,119 + y, paint);
canvas.drawLine(182 - x, 119 + y, 186 - x,121 + y, paint);
canvas.drawLine(186 - x, 121 + y, 189 - x,124 + y, paint);
canvas.drawLine(189 - x, 124 + y, 193 - x,128 + y, paint);
canvas.drawLine(193 - x, 128 + y, 196 - x,132 + y, paint);
canvas.drawLine(196 - x, 132 + y, 197 - x,135 + y, paint);
canvas.drawLine(197 - x, 135 + y, 199 - x,140 + y, paint);
canvas.drawLine(199 - x, 140 + y, 205 - x,150 + y, paint);
canvas.drawLine(205 - x, 150 + y, 205 - x,152 + y, paint);
canvas.drawLine(205 - x, 152 + y, 207 - x,155 + y, paint);
canvas.drawLine(207 - x, 155 + y, 208 - x,160 + y, paint);
canvas.drawLine(208 - x, 160 + y, 207 - x,163 + y, paint);
canvas.drawLine(207 - x, 163 + y, 207 - x,165 + y, paint);
canvas.drawLine(207 - x, 165 + y, 208 - x,167 + y, paint);
canvas.drawLine(208 - x, 167 + y, 210 - x,169 + y, paint);
canvas.drawLine(210 - x, 169 + y, 212 - x,170 + y, paint);
canvas.drawLine(212 - x, 170 + y, 212 - x,172 + y, paint);
canvas.drawLine(212 - x, 172 + y, 213 - x,176 + y, paint);
canvas.drawLine(213 - x, 176 + y, 214 - x,182 + y, paint);
canvas.drawLine(214 - x, 182 + y, 218 - x,186 + y, paint);
canvas.drawLine(218 - x, 186 + y, 217 - x,189 + y, paint);
canvas.drawLine(217 - x, 189 + y, 222 - x,191 + y, paint);
canvas.drawLine(222 - x, 191 + y, 225 - x,189 + y, paint);
canvas.drawLine(225 - x, 189 + y, 238 - x,191 + y, paint);
canvas.drawLine(238 - x, 191 + y, 240 - x,192 + y, paint);
canvas.drawLine(240 - x, 192 + y, 244 - x,192 + y, paint);
canvas.drawLine(244 - x, 192 + y, 251 - x,189 + y, paint);
canvas.drawLine(264 - x, 215 + y, 261 - x,215 + y, paint);
canvas.drawLine(261 - x, 215 + y, 261 - x,222 + y, paint);
canvas.drawLine(261 - x, 222 + y, 259 - x,222 + y, paint);
canvas.drawLine(259 - x, 222 + y, 259 - x,224 + y, paint);
canvas.drawLine(259 - x, 224 + y, 257 - x,224 + y, paint);
canvas.drawLine(257 - x, 224 + y, 257 - x,226 + y, paint);
canvas.drawLine(257 - x, 226 + y, 255 - x,226 + y, paint);
canvas.drawLine(255 - x, 226 + y, 255 - x,229 + y, paint);
canvas.drawLine(255 - x, 229 + y, 253 - x,229 + y, paint);
canvas.drawLine(253 - x, 229 + y, 250 - x,228 + y, paint);
canvas.drawLine(250 - x, 228 + y, 249 - x,228 + y, paint);
canvas.drawLine(249 - x, 228 + y, 247 - x,226 + y, paint);
canvas.drawLine(247 - x, 226 + y, 242 - x,223 + y, paint);
canvas.drawLine(242 - x, 223 + y, 241 - x,225 + y, paint);
canvas.drawLine(241 - x, 225 + y, 239 - x,227 + y, paint);
canvas.drawLine(239 - x, 227 + y, 241 - x,229 + y, paint);
canvas.drawLine(241 - x, 229 + y, 241 - x,231 + y, paint);
canvas.drawLine(241 - x, 231 + y, 244 - x,233 + y, paint);
canvas.drawLine(244 - x, 233 + y, 242 - x,237 + y, paint);
canvas.drawLine(242 - x, 237 + y, 232 - x,236 + y, paint);
canvas.drawLine(232 - x, 236 + y, 222 - x,232 + y, paint);
canvas.drawLine(222 - x, 232 + y, 220 - x,232 + y, paint);
canvas.drawLine(220 - x, 232 + y, 219 - x,235 + y, paint);
canvas.drawLine(219 - x, 235 + y, 219 - x,237 + y, paint);
canvas.drawLine(219 - x, 237 + y, 217 - x,239 + y, paint);
canvas.drawLine(217 - x, 239 + y, 214 - x,241 + y, paint);
canvas.drawLine(214 - x, 241 + y, 213 - x,239 + y, paint);
canvas.drawLine(213 - x, 239 + y, 211 - x,241 + y, paint);
canvas.drawLine(211 - x, 241 + y, 211 - x,243 + y, paint);
canvas.drawLine(211 - x, 243 + y, 209 - x,243 + y, paint);
canvas.drawLine(209 - x, 243 + y, 209 - x,245 + y, paint);
canvas.drawLine(209 - x, 245 + y, 204 - x,243 + y, paint);
canvas.drawLine(204 - x, 243 + y, 203 - x,251 + y, paint);
canvas.drawLine(203 - x, 251 + y, 206 - x,252 + y, paint);
canvas.drawLine(206 - x, 252 + y, 209 - x,255 + y, paint);
canvas.drawLine(209 - x, 255 + y, 207 - x,257 + y, paint);
canvas.drawLine(207 - x, 257 + y, 203 - x,255 + y, paint);
canvas.drawLine(203 - x, 255 + y, 200 - x,258 + y, paint);
canvas.drawLine(200 - x, 258 + y, 208 - x,264 + y, paint);
canvas.drawLine(208 - x, 264 + y, 208 - x,266 + y, paint);
canvas.drawLine(208 - x, 266 + y, 210 - x,268 + y, paint);
canvas.drawLine(210 - x, 268 + y, 230 - x,283 + y, paint);
canvas.drawLine(230 - x, 283 + y, 232 - x,283 + y, paint);
canvas.drawLine(232 - x, 283 + y, 234 - x,285 + y, paint);
canvas.drawLine(234 - x, 285 + y, 242 - x,295 + y, paint);
canvas.drawLine(264 - x, 215 + y, 266 - x,217 + y, paint);
canvas.drawLine(266 - x, 217 + y, 267 - x,225 + y, paint);
canvas.drawLine(267 - x, 225 + y, 267 - x,223 + y, paint);
canvas.drawLine(267 - x, 223 + y, 272 - x,231 + y, paint);
canvas.drawLine(272 - x, 231 + y, 274 - x,231 + y, paint);
canvas.drawLine(274 - x, 231 + y, 274 - x,232 + y, paint);
canvas.drawLine(274 - x, 232 + y, 273 - x,237 + y, paint);
canvas.drawLine(273 - x, 237 + y, 273 - x,239 + y, paint);
canvas.drawLine(273 - x, 239 + y, 275 - x,241 + y, paint);
canvas.drawLine(275 - x, 241 + y, 276 - x,241 + y, paint);
canvas.drawLine(276 - x, 241 + y, 276 - x,243 + y, paint);
canvas.drawLine(276 - x, 243 + y, 273 - x,245 + y, paint);
canvas.drawLine(273 - x, 245 + y, 273 - x,247 + y, paint);
canvas.drawLine(273 - x, 247 + y, 272 - x,248 + y, paint);
canvas.drawLine(272 - x, 248 + y, 271 - x,250 + y, paint);
canvas.drawLine(271 - x, 250 + y, 265 - x,265 + y, paint);
canvas.drawLine(265 - x, 265 + y, 250 - x,285 + y, paint);
canvas.drawLine(250 - x, 285 + y, 247 - x,287 + y, paint);
canvas.drawLine(247 - x, 287 + y, 245 - x,290 + y, paint);
canvas.drawLine(245 - x, 290 + y, 242 - x,295 + y, paint);
canvas.drawLine(168 - x, 451 + y, 171 - x,448 + y, paint);
canvas.drawLine(171 - x, 448 + y, 171 - x,445 + y, paint);
canvas.drawLine(171 - x, 445 + y, 170 - x,443 + y, paint);
canvas.drawLine(170 - x, 443 + y, 169 - x,442 + y, paint);
canvas.drawLine(169 - x, 442 + y, 160 - x,439 + y, paint);
canvas.drawLine(160 - x, 439 + y, 167 - x,412 + y, paint);
canvas.drawLine(167 - x, 412 + y, 169 - x,403 + y, paint);
canvas.drawLine(169 - x, 403 + y, 168 - x,395 + y, paint);
canvas.drawLine(168 - x, 395 + y, 170 - x,392 + y, paint);
canvas.drawLine(170 - x, 392 + y, 173 - x,380 + y, paint);
canvas.drawLine(173 - x, 380 + y, 173 - x,378 + y, paint);
canvas.drawLine(173 - x, 378 + y, 175 - x,376 + y, paint);
canvas.drawLine(175 - x, 376 + y, 173 - x,374 + y, paint);
canvas.drawLine(173 - x, 374 + y, 173 - x,371 + y, paint);
canvas.drawLine(173 - x, 371 + y, 172 - x,363 + y, paint);
canvas.drawLine(172 - x, 363 + y, 172 - x,355 + y, paint);
canvas.drawLine(172 - x, 355 + y, 174 - x,353 + y, paint);
canvas.drawLine(174 - x, 353 + y, 172 - x,350 + y, paint);
canvas.drawLine(172 - x, 350 + y, 173 - x,342 + y, paint);
canvas.drawLine(173 - x, 342 + y, 173 - x,344 + y, paint);
canvas.drawLine(173 - x, 344 + y, 174 - x,342 + y, paint);
canvas.drawLine(174 - x, 342 + y, 174 - x,340 + y, paint);
canvas.drawLine(174 - x, 340 + y, 182 - x,364 + y, paint);
canvas.drawLine(182 - x, 364 + y, 188 - x,363 + y, paint);
canvas.drawLine(188 - x, 363 + y, 186 - x,361 + y, paint);
canvas.drawLine(186 - x, 361 + y, 175 - x,337 + y, paint);
canvas.drawLine(175 - x, 337 + y, 177 - x,328 + y, paint);
canvas.drawLine(177 - x, 328 + y, 177 - x,325 + y, paint);
canvas.drawLine(177 - x, 325 + y, 178 - x,323 + y, paint);
canvas.drawLine(178 - x, 323 + y, 180 - x,320 + y, paint);
canvas.drawLine(180 - x, 320 + y, 188 - x,295 + y, paint);
canvas.drawLine(188 - x, 295 + y, 190 - x,295 + y, paint);
canvas.drawLine(190 - x, 295 + y, 222 - x,323 + y, paint);
canvas.drawLine(222 - x, 323 + y, 222 - x,325 + y, paint);
canvas.drawLine(222 - x, 325 + y, 221 - x,334 + y, paint);
canvas.drawLine(221 - x, 334 + y, 221 - x,336 + y, paint);
canvas.drawLine(221 - x, 336 + y, 219 - x,355 + y, paint);
canvas.drawLine(219 - x, 355 + y, 217 - x,370 + y, paint);
canvas.drawLine(217 - x, 370 + y, 215 - x,390 + y, paint);
canvas.drawLine(215 - x, 390 + y, 214 - x,400 + y, paint);
canvas.drawLine(214 - x, 400 + y, 209 - x,405 + y, paint);
canvas.drawLine(209 - x, 405 + y, 209 - x,407 + y, paint);
canvas.drawLine(209 - x, 407 + y, 211 - x,409 + y, paint);
canvas.drawLine(211 - x, 409 + y, 213 - x,410 + y, paint);
canvas.drawLine(213 - x, 410 + y, 215 - x,412 + y, paint);
canvas.drawLine(215 - x, 412 + y, 215 - x,413 + y, paint);
canvas.drawLine(215 - x, 413 + y, 212 - x,420 + y, paint);
canvas.drawLine(212 - x, 420 + y, 210 - x,422 + y, paint);
canvas.drawLine(210 - x, 422 + y, 207 - x,423 + y, paint);
canvas.drawLine(207 - x, 423 + y, 207 - x,426 + y, paint);
canvas.drawLine(207 - x, 426 + y, 204 - x,430 + y, paint);
canvas.drawLine(204 - x, 430 + y, 195 - x,433 + y, paint);
canvas.drawLine(195 - x, 433 + y, 194 - x,439 + y, paint);
canvas.drawLine(194 - x, 439 + y, 195 - x,442 + y, paint);
canvas.drawLine(195 - x, 442 + y, 197 - x,444 + y, paint);
canvas.drawLine(197 - x, 444 + y, 220 - x,444 + y, paint);
canvas.drawLine(220 - x, 444 + y, 230 - x,442 + y, paint);
canvas.drawLine(230 - x, 442 + y, 240 - x,445 + y, paint);
canvas.drawLine(240 - x, 445 + y, 242 - x,445 + y, paint);
canvas.drawLine(242 - x, 445 + y, 252 - x,442 + y, paint);
canvas.drawLine(252 - x, 442 + y, 263 - x,446 + y, paint);
canvas.drawLine(263 - x, 446 + y, 267 - x,446 + y, paint);
canvas.drawLine(267 - x, 446 + y, 285 - x,443 + y, paint);
canvas.drawLine(285 - x, 443 + y, 286 - x,441 + y, paint);
canvas.drawLine(286 - x, 441 + y, 288 - x,439 + y, paint);
canvas.drawLine(288 - x, 439 + y, 288 - x,433 + y, paint);
canvas.drawLine(288 - x, 433 + y, 285 - x,431 + y, paint);
canvas.drawLine(285 - x, 431 + y, 284 - x,430 + y, paint);
canvas.drawLine(284 - x, 430 + y, 275 - x,429 + y, paint);
canvas.drawLine(275 - x, 429 + y, 265 - x,422 + y, paint);
canvas.drawLine(265 - x, 422 + y, 265 - x,420 + y, paint);
canvas.drawLine(265 - x, 420 + y, 262 - x,417 + y, paint);
canvas.drawLine(262 - x, 417 + y, 267 - x,415 + y, paint);
canvas.drawLine(267 - x, 415 + y, 266 - x,408 + y, paint);
canvas.drawLine(266 - x, 408 + y, 267 - x,406 + y, paint);
canvas.drawLine(267 - x, 407 + y, 270 - x,413 + y, paint);
canvas.drawLine(270 - x, 413 + y, 272 - x,413 + y, paint);
canvas.drawLine(272 - x, 413 + y, 273 - x,411 + y, paint);
canvas.drawLine(273 - x, 411 + y, 275 - x,413 + y, paint);
canvas.drawLine(275 - x, 413 + y, 276 - x,413 + y, paint);
canvas.drawLine(276 - x, 413 + y, 281 - x,408 + y, paint);
canvas.drawLine(281 - x, 408 + y, 283 - x,409 + y, paint);
canvas.drawLine(283 - x, 409 + y, 278 - x,402 + y, paint);
canvas.drawLine(278 - x, 402 + y, 271 - x,365 + y, paint);
canvas.drawLine(271 - x, 365 + y, 272 - x,364 + y, paint);
canvas.drawLine(272 - x, 364 + y, 271 - x,363 + y, paint);
canvas.drawLine(271 - x, 363 + y, 268 - x,340 + y, paint);
canvas.drawLine(268 - x, 340 + y, 285 - x,300 + y, paint);
canvas.drawLine(251 - x, 189 + y, 253 - x,191 + y, paint);
canvas.drawLine(253 - x, 191 + y, 255 - x,189 + y, paint);
canvas.drawLine(255 - x, 189 + y, 253 - x,187 + y, paint);
canvas.drawLine(253 - x, 187 + y, 253 - x,185 + y, paint);
canvas.drawLine(253 - x, 185 + y, 254 - x,181 + y, paint);
canvas.drawLine(242 - x, 157 + y, 250 - x,147 + y, paint);
canvas.drawLine(250 - x, 147 + y, 255 - x,144 + y, paint);
canvas.drawLine(255 - x, 144 + y, 257 - x,142 + y, paint);
canvas.drawLine(257 - x, 142 + y, 261 - x,141 + y, paint);
canvas.drawLine(261 - x, 141 + y, 267 - x,135 + y, paint);
canvas.drawLine(267 - x, 135 + y, 268 - x,131 + y, paint);
canvas.drawLine(268 - x, 131 + y, 275 - x,125 + y, paint);
canvas.drawLine(275 - x, 125 + y, 276 - x,122 + y, paint);
canvas.drawLine(276 - x, 122 + y, 278 - x,121 + y, paint);
canvas.drawLine(278 - x, 121 + y, 277 - x,109 + y, paint);
canvas.drawLine(277 - x, 109 + y, 275 - x,109 + y, paint);
canvas.drawLine(275 - x, 109 + y, 271 - x,110 + y, paint);
canvas.drawLine(271 - x, 110 + y, 268 - x,111 + y, paint);
canvas.drawLine(268 - x, 111 + y, 264 - x,111 + y, paint);
canvas.drawLine(264 - x, 111 + y, 261 - x,110 + y, paint);
canvas.drawLine(261 - x, 110 + y, 261 - x,106 + y, paint);
canvas.drawLine(261 - x, 106 + y, 262 - x,105 + y, paint);
canvas.drawLine(262 - x, 105 + y, 258 - x,102 + y, paint);
canvas.drawLine(258 - x, 102 + y, 258 - x,101 + y, paint);
canvas.drawLine(258 - x, 101 + y, 264 - x,99 + y, paint);
canvas.drawLine(264 - x, 99 + y, 264 - x,98 + y, paint);
canvas.drawLine(264 - x, 98 + y, 259 - x,96 + y, paint);
canvas.drawLine(259 - x, 96 + y, 259 - x,95 + y, paint);
canvas.drawLine(259 - x, 95 + y, 261 - x,93 + y, paint);
canvas.drawLine(261 - x, 93 + y, 261 - x,92 + y, paint);
canvas.drawLine(261 - x, 92 + y, 257 - x,90 + y, paint);
canvas.drawLine(257 - x, 90 + y, 257 - x,88 + y, paint);
canvas.drawLine(257 - x, 88 + y, 260 - x,84 + y, paint);
canvas.drawLine(260 - x, 84 + y, 264 - x,80 + y, paint);
canvas.drawLine(264 - x, 80 + y, 264 - x,79 + y, paint);
canvas.drawLine(264 - x, 79 + y, 262 - x,77 + y, paint);
canvas.drawLine(262 - x, 77 + y, 262 - x,75 + y, paint);
canvas.drawLine(285 - x, 52 + y, 288 - x,52 + y, paint);
canvas.drawLine(288 - x, 52 + y, 290 - x,50 + y, paint);
canvas.drawLine(290 - x, 50 + y, 295 - x,42 + y, paint);
canvas.drawLine(312 - x, 42 + y, 314 - x,48 + y, paint);
canvas.drawLine(314 - x, 48 + y, 333 - x,129 + y, paint);
canvas.drawLine(318 - x, 120 + y, 312 - x,90 + y, paint);
canvas.drawLine(312 - x, 90 + y, 302 - x,98 + y, paint);
canvas.drawLine(302 - x, 98 + y, 300 - x,101 + y, paint);
canvas.drawLine(300 - x, 101 + y, 299 - x,102 + y, paint);
canvas.drawLine(299 - x, 102 + y, 299 - x,103 + y, paint);
canvas.drawLine(299 - x, 103 + y, 301 - x,110 + y, paint);
canvas.drawLine(301 - x, 110 + y, 301 - x,111 + y, paint);
canvas.drawLine(301 - x, 111 + y, 302 - x,112 + y, paint);
canvas.drawLine(302 - x, 112 + y, 314 - x,122 + y, paint);
canvas.drawLine(314 - x, 122 + y, 318 - x,120 + y, paint);
canvas.drawLine(333 - x, 129 + y, 337 - x,133 + y, paint);
canvas.drawLine(337 - x, 133 + y, 339 - x,133 + y, paint);
canvas.drawLine(339 - x, 133 + y, 342 - x,136 + y, paint);
canvas.drawLine(342 - x, 136 + y, 348 - x,140 + y, paint);
canvas.drawLine(348 - x, 140 + y, 352 - x,142 + y, paint);
canvas.drawLine(352 - x, 142 + y, 356 - x,146 + y, paint);
canvas.drawLine(356 - x, 146 + y, 360 - x,147 + y, paint);
canvas.drawLine(360 - x, 147 + y, 385 - x,152 + y, paint);
canvas.drawLine(385 - x, 152 + y, 389 - x,154 + y, paint);
canvas.drawLine(389 - x, 154 + y, 405 - x,156 + y, paint);
canvas.drawLine(405 - x, 156 + y, 409 - x,162 + y, paint);
canvas.drawLine(409 - x, 162 + y, 422 - x,164 + y, paint);
canvas.drawLine(422 - x, 164 + y, 422 - x,166 + y, paint);
canvas.drawLine(422 - x, 166 + y, 435 - x,168 + y, paint);
canvas.drawLine(435 - x, 168 + y, 437 - x,167 + y, paint);
canvas.drawLine(437 - x, 167 + y, 445 - x,169 + y, paint);
canvas.drawLine(445 - x, 169 + y, 448 - x,169 + y, paint);
canvas.drawLine(448 - x, 169 + y, 452 - x,171 + y, paint);
canvas.drawLine(452 - x, 171 + y, 453 - x,172 + y, paint);
canvas.drawLine(453 - x, 172 + y, 451 - x,174 + y, paint);
canvas.drawLine(451 - x, 174 + y, 448 - x,174 + y, paint);
canvas.drawLine(448 - x, 174 + y, 444 - x,175 + y, paint);
canvas.drawLine(444 - x, 175 + y, 438 - x,176 + y, paint);
canvas.drawLine(438 - x, 176 + y, 435 - x,176 + y, paint);
canvas.drawLine(435 - x, 176 + y, 434 - x,175 + y, paint);
canvas.drawLine(434 - x, 175 + y, 436 - x,179 + y, paint);
canvas.drawLine(436 - x, 179 + y, 437 - x,179 + y, paint);
canvas.drawLine(437 - x, 179 + y, 435 - x,181 + y, paint);
canvas.drawLine(435 - x, 181 + y, 430 - x,179 + y, paint);
canvas.drawLine(430 - x, 179 + y, 420 - x,178 + y, paint);
canvas.drawLine(420 - x, 178 + y, 415 - x,175 + y, paint);
canvas.drawLine(415 - x, 175 + y, 413 - x,173 + y, paint);
canvas.drawLine(413 - x, 173 + y, 411 - x,173 + y, paint);
canvas.drawLine(411 - x, 173 + y, 406 - x,174 + y, paint);
canvas.drawLine(406 - x, 174 + y, 404 - x,178 + y, paint);
canvas.drawLine(404 - x, 178 + y, 404 - x,180 + y, paint);
canvas.drawLine(404 - x, 180 + y, 405 - x,183 + y, paint);
canvas.drawLine(405 - x, 183 + y, 407 - x,186 + y, paint);
canvas.drawLine(407 - x, 186 + y, 407 - x,188 + y, paint);
canvas.drawLine(407 - x, 188 + y, 406 - x,191 + y, paint);
canvas.drawLine(406 - x, 191 + y, 405 - x,194 + y, paint);
canvas.drawLine(405 - x, 194 + y, 402 - x,195 + y, paint);
canvas.drawLine(402 - x, 195 + y, 399 - x,197 + y, paint);
canvas.drawLine(399 - x, 197 + y, 395 - x,199 + y, paint);
canvas.drawLine(395 - x, 199 + y, 392 - x,198 + y, paint);
canvas.drawLine(392 - x, 198 + y, 389 - x,196 + y, paint);
canvas.drawLine(389 - x, 196 + y, 370 - x,180 + y, paint);
canvas.drawLine(370 - x, 180 + y, 360 - x,176 + y, paint);
canvas.drawLine(360 - x, 176 + y, 355 - x,173 + y, paint);
canvas.drawLine(355 - x, 173 + y, 352 - x,172 + y, paint);
canvas.drawLine(352 - x, 172 + y, 354 - x,177 + y, paint);
canvas.drawLine(354 - x, 177 + y, 353 - x,178 + y, paint);
canvas.drawLine(353 - x, 178 + y, 353 - x,179 + y, paint);
canvas.drawLine(353 - x, 179 + y, 351 - x,177 + y, paint);
canvas.drawLine(351 - x, 177 + y, 351 - x,184 + y, paint);
canvas.drawLine(351 - x, 184 + y, 349 - x,183 + y, paint);
canvas.drawLine(349 - x, 183 + y, 346 - x,186 + y, paint);
canvas.drawLine(346 - x, 186 + y, 345 - x,189 + y, paint);
canvas.drawLine(345 - x, 189 + y, 343 - x,189 + y, paint);
canvas.drawLine(343 - x, 189 + y, 341 - x,180 + y, paint);
canvas.drawLine(341 - x, 180 + y, 339 - x,180 + y, paint);
canvas.drawLine(339 - x, 180 + y, 338 - x,183 + y, paint);
canvas.drawLine(338 - x, 183 + y, 336 - x,177 + y, paint);
canvas.drawLine(336 - x, 177 + y, 338 - x,174 + y, paint);
canvas.drawLine(338 - x, 174 + y, 337 - x,168 + y, paint);
canvas.drawLine(337 - x, 168 + y, 334 - x,166 + y, paint);
canvas.drawLine(334 - x, 166 + y, 332 - x,164 + y, paint);
canvas.drawLine(332 - x, 164 + y, 330 - x,163 + y, paint);
canvas.drawLine(330 - x, 163 + y, 326 - x,163 + y, paint);
canvas.drawLine(326 - x, 163 + y, 322 - x,174 + y, paint);
canvas.drawLine(322 - x, 174 + y, 320 - x,177 + y, paint);
canvas.drawLine(320 - x, 177 + y, 319 - x,180 + y, paint);
canvas.drawLine(319 - x, 180 + y, 316 - x,182 + y, paint);
canvas.drawLine(316 - x, 182 + y, 316 - x,184 + y, paint);
canvas.drawLine(316 - x, 184 + y, 312 - x,189 + y, paint);
canvas.drawLine(312 - x, 189 + y, 310 - x,193 + y, paint);
canvas.drawLine(310 - x, 193 + y, 310 - x,195 + y, paint);
canvas.drawLine(310 - x, 195 + y, 323 - x,207 + y, paint);
canvas.drawLine(323 - x, 207 + y, 325 - x,209 + y, paint);
canvas.drawLine(325 - x, 209 + y, 337 - x,217 + y, paint);
canvas.drawLine(337 - x, 217 + y, 342 - x,222 + y, paint);
canvas.drawLine(342 - x, 222 + y, 344 - x,225 + y, paint);
canvas.drawLine(344 - x, 225 + y, 346 - x,227 + y, paint);
canvas.drawLine(346 - x, 227 + y, 347 - x,229 + y, paint);
canvas.drawLine(285 - x, 300 + y, 288 - x,300 + y, paint);
canvas.drawLine(288 - x, 300 + y, 293 - x,325 + y, paint);
canvas.drawLine(293 - x, 325 + y, 295 - x,328 + y, paint);
canvas.drawLine(295 - x, 328 + y, 300 - x,355 + y, paint);
canvas.drawLine(300 - x, 355 + y, 299 - x,359 + y, paint);
canvas.drawLine(299 - x, 359 + y, 299 - x,360 + y, paint);
canvas.drawLine(299 - x, 360 + y, 304 - x,403 + y, paint);
canvas.drawLine(304 - x, 403 + y, 302 - x,405 + y, paint);
canvas.drawLine(302 - x, 405 + y, 301 - x,415 + y, paint);
canvas.drawLine(301 - x, 415 + y, 308 - x,418 + y, paint);
canvas.drawLine(308 - x, 418 + y, 311 - x,408 + y, paint);
canvas.drawLine(311 - x, 408 + y, 313 - x,407 + y, paint);
canvas.drawLine(313 - x, 407 + y, 315 - x,410 + y, paint);
canvas.drawLine(315 - x, 410 + y, 317 - x,409 + y, paint);
canvas.drawLine(317 - x, 409 + y, 318 - x,409 + y, paint);
canvas.drawLine(318 - x, 409 + y, 320 - x,415 + y, paint);
canvas.drawLine(300 - x, 432 + y, 306 - x,432 + y, paint);
canvas.drawLine(306 - x, 432 + y, 307 - x,431 + y, paint);
canvas.drawLine(307 - x, 431 + y, 313 - x,427 + y, paint);
canvas.drawLine(313 - x, 427 + y, 320 - x,415 + y, paint);
canvas.drawLine(300 - x, 432 + y, 297 - x,436 + y, paint);
canvas.drawLine(297 - x, 436 + y, 297 - x,437 + y, paint);
canvas.drawLine(297 - x, 437 + y, 299 - x,440 + y, paint);
canvas.drawLine(299 - x, 440 + y, 301 - x,442 + y, paint);
canvas.drawLine(301 - x, 442 + y, 302 - x,443 + y, paint);
canvas.drawLine(302 - x, 443 + y, 336 - x,443 + y, paint);
canvas.drawLine(336 - x, 443 + y, 340 - x,439 + y, paint);
canvas.drawLine(340 - x, 439 + y, 341 - x,439 + y, paint);
canvas.drawLine(341 - x, 439 + y, 344 - x,443 + y, paint);
canvas.drawLine(344 - x, 443 + y, 355 - x,441 + y, paint);
canvas.drawLine(355 - x, 441 + y, 357 - x,427 + y, paint);
canvas.drawLine(357 - x, 427 + y, 354 - x,425 + y, paint);
canvas.drawLine(354 - x, 425 + y, 353 - x,423 + y, paint);
canvas.drawLine(353 - x, 423 + y, 349 - x,417 + y, paint);
canvas.drawLine(349 - x, 417 + y, 346 - x,415 + y, paint);
canvas.drawLine(346 - x, 415 + y, 344 - x,413 + y, paint);
canvas.drawLine(344 - x, 413 + y, 344 - x,412 + y, paint);
canvas.drawLine(344 - x, 412 + y, 341 - x,400 + y, paint);
canvas.drawLine(341 - x, 400 + y, 338 - x,397 + y, paint);
canvas.drawLine(338 - x, 397 + y, 337 - x,394 + y, paint);
canvas.drawLine(337 - x, 394 + y, 338 - x,384 + y, paint);
canvas.drawLine(338 - x, 384 + y, 336 - x,360 + y, paint);
canvas.drawLine(336 - x, 360 + y, 333 - x,340 + y, paint);
canvas.drawLine(333 - x, 340 + y, 331 - x,330 + y, paint);
canvas.drawLine(331 - x, 330 + y, 330 - x,320 + y, paint);
canvas.drawLine(330 - x, 320 + y, 335 - x,290 + y, paint);
canvas.drawLine(335 - x, 290 + y, 337 - x,284 + y, paint);
canvas.drawLine(337 - x, 284 + y, 339 - x,281 + y, paint);
canvas.drawLine(339 - x, 281 + y, 341 - x,278 + y, paint);
canvas.drawLine(341 - x, 278 + y, 342 - x,277 + y, paint);
canvas.drawLine(342 - x, 277 + y, 343 - x,274 + y, paint);
canvas.drawLine(343 - x, 274 + y, 344 - x,273 + y, paint);
canvas.drawLine(344 - x, 273 + y, 346 - x,270 + y, paint);
canvas.drawLine(155 - x, 455 + y, 169 - x,450 + y, paint);
canvas.drawLine(346 - x, 270 + y, 348 - x,268 + y, paint);
canvas.drawLine(254 - x, 182 + y, 252 - x,180 + y, paint);
canvas.drawLine(100 - x, 196 + y, 103 - x,199 + y, paint);
canvas.drawLine(103 - x, 199 + y, 105 - x,199 + y, paint);
canvas.drawLine(105 - x, 199 + y, 125 - x,192 + y, paint);
canvas.drawLine(114 - x, 137 + y, 118 - x,129 + y, paint);
canvas.drawLine(119 - x, 129 + y, 120 - x,126 + y, paint);
canvas.drawLine(308 - x, 36 + y, 312 - x,43 + y, paint);
canvas.drawLine(308 - x, 36 + y, 304 - x,36 + y, paint);
canvas.drawLine(304 - x, 36 + y, 295 - x,42 + y, paint);
canvas.drawLine(280 - x, 52 + y, 285 - x,52 + y, paint);
canvas.drawLine(262 - x, 75 + y, 265 - x,65 + y, paint);
canvas.drawLine(265 - x, 65 + y, 268 - x,60 + y, paint);
canvas.drawLine(268 - x, 60 + y, 274 - x,53 + y, paint);
canvas.drawLine(274 - x, 53 + y, 280 - x,52 + y, paint);
RectF oval = new RectF(130 - x, 448 + y, 180 - x,455 + y);
canvas.drawArc(oval, 88, 92, false, paint);
oval = new RectF(312 - x, 207 + y, 350 - x,293 + y);
canvas.drawArc(oval, 330, 55, false, paint);
oval = new RectF(242 - x, 127 + y, 277 - x,183 + y);
canvas.drawArc(oval, 115, 62, false, paint);


}

Wednesday, October 5, 2011

J2EE WebApp - For Beginners


This article is for beginners who want to try out their first J2EE application. It explains the step by step process to create a web application using JSF 2.0, Rich Faces 3.3 for UI and MYSQL5.5 for database and Jboss 5.0.1GA server for deployment.


    Set up MYSQL5.5

     
  • Download and install the MYSQL Installer pack (32bit or 64bit) from http://dev.mysql.com/downloads/installer/


  • Remember and save the root password for MYSQL that you create during the installation.


  • MYSQL Services will start listening to port 3306 after successful installation.


  • Start MYSQL Command Line Client from Start Menu  and enter the root password.


  • Create a database.  Give the command,  ‘create database company ;’ (without quotes) and hit enter. You will see the response ‘Query OK, 1 row affected’.


  • Create User Id/Password for that database. Give the command, ‘grant all privileges on company.* to johnsmith@localhost identified by 'password-1';’. Here the user name is ‘johnsmith’ and password is ‘password-1’. You will need this user id/password for accessing any table with the ‘company’ database.

  •        
  • Set the ‘company’ database as default. Give the command, ‘use company;’


  • Create a table. Give the command, ‘create table EMPLOYEE (employeeFirstName varchar(20), employeeLastName varchar(20), upddttm DATE);’


  • Insert a sample record into the table. Give the command, ‘insert into EMPLOYEE (employeeFirstName,employeeLastName,upddttm) values ('Alex','Roger',current_timestamp);’



    • Set up JBOSS 5.0.1GA


  • Download and install JBOSS 5.0.1GA from http://www.jboss.org/jbossas/downloads


  • To tell Jboss to connect to your MYSQL5.5 during server startup, drop a file named ‘mysql-ds.xml’ inside the ‘ /server/default/deploy’ folder.


  • The contents of the mysql-ds.xml should contain the connection url, user name, password for the database it wants to connect.



    1. <datasources>
          <local-tx-datasource>
           <jndi-name>MySqlDS</jndi-name>
           <connection-url>jdbc:mysql://localhost:3306/company</connection-url>
           <driver-class>com.mysql.jdbc.Driver</driver-class>
           <user-name>johnsmith</user-name>
           <password>password-1</password>
           <min-pool-size>2</min-pool-size>
           <max-pool-size>10</max-pool-size>
           </local-tx-datasource>
      </datasources>


  • Please note, you have to provide optimized values for the Database connection pooling as per your database usage. Too many or too less will result in performance degrade.



    • Set up Web Application:


  • I assume, you have Eclipse 3.5 or above and JDK 1.6 installed on your machine. I also assume, you have JSF 2.0 libraries (Say Mojara 2.0) on your machine.


  • On Eclipse, Create a new Dynamic Web project – SampleWeb, with target runtime as Jboss 5.0, Dynamic Web Module version 2.5, Added to SampleWebEAR and with Context root ‘SampleWeb’. Check the ‘Generate web.xml deployment descriptor’ option in the Wizard.


  • Right click on SampleWeb  Properties  Project Facets  Check Java Server Faces 2.0. If you see further configurations required at the bottom, click the link and browse your JSF Implementation Library.


  • Rich Faces library has certain dependencies with JSF library. This example uses Rich Faces 3.3 and JSF 2.0.


  • Before we start with our JSP file, ensure, you have the following jars in your ‘lib’ directory inside web-inf folder.


      commons-beanutils.jar
      commons-collections-3.2.1.jar
      commons-digester.jar
      commons-logging.jar
      jboss-faces.jar
      jsf-api.jar
      jsf-impl.jar
      richfaces-api-3.3.3.Final.jar
      richfaces-impl-3.3.3.Final.jar
      richfaces-ui-3.3.3.Final.jar



  • Your web.xml needs the below entry to support Rich Faces.



    1. <context-param>
      <param-name>org.richfaces.SKIN</param-name>
      <param-value>blueSky</param-value>
      </context-param>
      <context-param>
      <param-name>org.richfaces.CONTROL_SKINNING</param-name>
      <param-value>enable</param-value>
      </context-param>
      <filter>
      <display-name>RichFaces Filter</display-name>
      <filter-name>richfaces</filter-name>
      <filter-class>org.ajax4jsf.Filter</filter-class>
      </filter>
      <filter-mapping>
      <filter-name>richfaces</filter-name>
      <servlet-name>Faces Servlet</servlet-name>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>FORWARD</dispatcher>
      <dispatcher>INCLUDE</dispatcher>
      </filter-mapping>


  • Before we get to the Employee. jsp, let’s create a employee bean for the page.



    1. package com.example.richfaces;
      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.Statement;
      import javax.naming.InitialContext;
      import javax.sql.DataSource;

      public class EmployeeBean {

      private String employeeFirstName;
      private String employeeLastName;

      public String getEmployeeFirstName() {
      return employeeFirstName;
      }

      public void setEmployeeFirstName(String employeeFirstName) {
      this.employeeFirstName = employeeFirstName;
      }

      public String getEmployeeLastName() {
      return employeeLastName;
      }

      public void setEmployeeLastName(String employeeLastName) {
      this.employeeLastName = employeeLastName;
      }

      public void doGetEmployeeName() {

      InitialContext ctx = null;
      DataSource ds = null;
      Connection conn = null;
      Statement stmt = null;
      try {
      ctx = new InitialContext();
      ds = (DataSource) ctx.lookup("java:/MySqlDS");
      conn = ds.getConnection();
      stmt = conn.createStatement();
      ResultSet rs = stmt
      .executeQuery("select employeeFirstName, employeeLastName from EMPLOYEE");
      while (rs.next()) {
      setEmployeeFirstName(rs.getString(1));
      setEmployeeLastName(rs.getString(2));
      }

      rs.close();
      stmt.close();
      conn.close();

      } catch (Exception ex) {
      // Implement finally block and better Exception handling here
      ex.printStackTrace();
      }

      }

      }


  • Add the Employee Bean entry to faces-config.xml. This way, we can refer the bean from our JSP page using ‘employee’ object



    1. <?xml version="1.0" encoding="UTF-8"?>
      <faces-config
          xmlns="http://java.sun.com/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
          version="2.0">
      <managed-bean>
      <description>Employee Bean</description>
      <managed-bean-name>employee</managed-bean-name>
      <managed-bean-class>com.example.richfaces.EmployeeBean</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
      <managed-property>
      <property-name>employeeFirstName</property-name>
      <property-class>java.lang.String</property-class>
      <value />
      </managed-property>
      <managed-property>
      <property-name>employeeLastName</property-name>
      <property-class>java.lang.String</property-class>
      <value />
      </managed-property>
      </managed-bean>
      </faces-config>


  • Create a new JSP File (Employee.jsp)



    1. <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
      <!-- RichFaces tag library declaration -->
      <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
      <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
      <html>
      <head>
      <title>RichFaces Example</title>
      </head>
      <body>
      <f:view>
      <a4j:form>
      <rich:panel header="RichFaces Greeter" style="width: 315px">

      <a4j:commandButton value="Get Our Employee" reRender="greeting"
      action="#{employee.doGetEmployeeName}" />

      <h:panelGroup id="greeting">
      <h:outputText value="Hello, "
      rendered="#{not empty employee.employeeFirstName}" />
      <h:outputText value="#{employee.employeeFirstName}" />
      <h:outputText value="#{employee.employeeLastName}"
      rendered="#{not empty employee.employeeFirstName}" />
      </h:panelGroup>
      </rich:panel>
      </a4j:form>
      </f:view>
      </body>
      </html>


  • You are all set!! Get going..














  • Monday, August 1, 2011

    HTML5 - Sliding Tile Puzzle Game


    Presented below is the HTML5 source code for a puzzle game called Sliding Tile puzzle or Mystic Square puzzle. Numbers 1 to 15 are jumbled in a 4x4 board. You have to arrange the numbers in order by moving the blank tile or magic square.
    At the bottom of the board, you can see images in shuffled position. With each move on the board, the images will get re-aligned. Once the puzzle is solved, you can see the complete picture in the Canvas Area.
    HTML5's Drag And Drop, Offline Storage, Canvas features are used. It is tested on Chrome 12.0 and Firefox 5.0 and NOT compatible with mobile devices.

    Screen Shot:



    HTML Code
    <!DOCTYPE HTML>
    <html manifest="cache.manifest">
    <head>

    <link rel=StyleSheet href="slidetile.css" TYPE="text/css">

    <script src="jquery-1.6.2.min.js"></script>
    <script src="slidetile.js"></script>
    </head>
    <body onload="initialize();">

    <div id="row1" class="row">
    <div id="1" class="tile" draggable="true"></div>
    <div id="2" class="tile" draggable="true"></div>
    <div id="3" class="tile" draggable="true"></div>
    <div id="4" class="tile" draggable="true"></div>
    </div>

    <div id="row2" class="row">
    <div id="5" class="tile" draggable="true"></div>
    <div id="6" class="tile" draggable="true"></div>
    <div id="7" class="tile" draggable="true"></div>
    <div id="8" class="tile" draggable="true"></div>
    </div>

    <div id="row3" class="row">
    <div id="9" class="tile" draggable="true"></div>
    <div id="10" class="tile" draggable="true"></div>
    <div id="11" class="tile" draggable="true"></div>
    <div id="12" class="tile" draggable="true"></div>
    </div>

    <div id="row4" class="row">
    <div id="13" class="tile" draggable="true"></div>
    <div id="14" class="tile" draggable="true"></div>
    <div id="15" class="tile" draggable="true"></div>
    <div id="16" class="tile" draggable="true"></div>
    </div>

    <div id="moveStatus" class="row">
    <div id="status">Ready to start?</div>
    </div>

    <canvas id="myCanvas" width="200" height="200" class="canvas">
    </canvas>

    <div id="buttons" class="row">
    <input type="button" id="newGameButton" class="button" value="New"
    onclick="clearStorage();initialize();"></input> <input type="button"
    id="saveGameButton" class="button" value="Save"></input>
    </div>

    <div id="rules" class="rules">
    <div id="rulesInfo" class="infoTitle">
    <strong>Rules:</strong>
    </div>
    <div class="row">1. Arrange the numbers on the board from 1 to
    15.</div>
    <div class="row">2. Move the tiles around by moving only the
    blank tile.</div>
    <div class="row">3. The blank tile can be swapped only with the
    adjacent tiles.</div>
    <div class="row">4. Drag and Drop the tiles using the mouse.
    Images are not draggable.</div>

    <div class="infoTitle">
    <strong>About:</strong>
    </div>
    <div class="row" id="AboutInfo">Sliding tile puzzle is also called Gem Puzzle or Mystic Square puzzle. If you can solve within 80 moves,well, you are lucky. The algorithm is complex to solve and sometimes impossible. Wish you patience and good luck. </div>

    <div class="infoTitle">
    <strong>Note:</strong>
    </div>
    <div class="row" id="NoteInfo">This version of game is NOT compatible with mobile. Offline caching enabled for modern browsers. Tested for Chrome 12.0 and Firefox 5.0</div>

    <div class="infoTitle">
    <strong>Best Finish:</strong>
    </div>
    <div class="row" id="bestMove"></div>
    </div>

    <div class="flashMessage" id="flashMessage"></div>

    </body>
    </html>


    CSS:
    .button {
    color: white;
    padding: 5px 20px;
    text-shadow: 1px 1px 0 #145982;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 15px;
    font-weight: bold;
    text-align: center;
    border: 1px solid #60b4e5;
    margin-right: 20px;
    margin-top: 20px;
    float: left;
    position: relative;
    right: -35%;
    width: 100px;

    /*
    CSS3 gradients for webkit and mozilla browsers fallback color for the rest:
    */
    background-color: #59aada;
    background: -moz-linear-gradient(left center, rgb(200, 0, 0),
    rgb(79, 79, 79), rgb(21, 21, 21) );
    background: -webkit-gradient(linear, left top, right top, color-stop(0, rgb(200, 0,
    0) ), color-stop(0.50, rgb(79, 79, 79) ),
    color-stop(1, rgb(21, 21, 21) ) ); to (#FFA4E6) );
    box-shadow: 3px 3px 3px #000000;
    -moz-box-shadow: 3px 3px 3px #000000;
    -webkit-box-shadow: 3px 3px 3px #000000;
    border: 1px solid #F902B6;
    border-radius: 6px;
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    background-color: #59aada
    }


    .button:hover { /* Lighter gradients for the hover effect */
    text-decoration: none;
    background-color: #F902B6;
    background-image: -moz-linear-gradient(#6bbbe9, #57a5d4);
    background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#6bbbe9),
    to(#57a5d4) );
    box-shadow: 3px 3px 3px #333;
    -moz-box-shadow: 3px 3px 3px #333;
    -webkit-box-shadow: 3px 3px 3px #333;
    }
    /* Prevent the text contents of draggable elements from being selectable. */
    [draggable] {
    -moz-user-select: none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    }

    .row {
    width: 100 %;
    clear: both;
    }

    .infoTitle {
    width: 100 %;
    clear: both;
    margin-top: 20px;
    text-shadow: 1px 0px 0 #145982;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 15px;
    font-weight: bold;
    }

    .rules {
    position: absolute;
    top: 0;
    right: 0;
    width: 200px;
    text-align: left;
    color: #800000;
    font-size: small;
    font-family: Arial, Helvetica, sans-serif;
    }

    .flashMessage {
    position: absolute;
    top: 50px;
    left: 50px;
    width: 220px;
    height: 60px;
    text-align: left;
    margin-right: 10px;
    margin-leftt: 15px;
    line-height: 1.5em;
    color: #fff;
    font-size: large;
    font-family: Arial, Helvetica, sans-serif;
    clear: both;
    margin-top: 20px;
    background: -moz-linear-gradient(left center, rgb(100, 0, 0),
    rgb(179, 79, 79), rgb(121, 21, 21) );
    background: -webkit-gradient(linear, left top, right top, color-stop(0, rgb(100, 0,
    0) ), color-stop(0.50, rgb(179, 79, 79) ),
    color-stop(1, rgb(121, 21, 21) ) );
    }

    #moveStatus {
    clear: both;
    float: left;
    position: relative;
    right: -35%;
    text-align: left;
    margin-top: 20px;
    width: 215px;
    background: -moz-linear-gradient(left center, rgb(200, 0, 0),
    rgb(79, 79, 79), rgb(21, 21, 21) );
    background: -webkit-gradient(linear, left top, right top, color-stop(0, rgb(200, 0,
    0) ), color-stop(0.50, rgb(79, 79, 79) ),
    color-stop(1, rgb(21, 21, 21) ) );
    color: #fff;
    text-shadow: 1px 1px 0 #145982;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 15px;
    font-weight: bold;
    box-shadow: 3px 3px 3px #000000;
    -moz-box-shadow: 3px 3px 3px #000000;
    -webkit-box-shadow: 3px 3px 3px #000000;
    }

    .tile {
    height: 50px;
    width: 50px;
    float: left;
    position: relative;
    right: -35%;
    border: 2px solid #666666;
    background-color: #ccc;
    margin-right: 1px;
    line-height: 2em;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 3px #000;
    box-shadow: inset 0 0 3px #000;
    text-align: center;
    cursor: move;
    background: -moz-linear-gradient(left center, rgb(0, 0, 0),
    rgb(79, 79, 79), rgb(21, 21, 21) );
    background: -webkit-gradient(linear, left top, right top, color-stop(0, rgb(0, 0, 0)
    ), color-stop(0.50, rgb(79, 79, 79) ), color-stop(1, rgb(21, 21, 21) )
    );
    color: #fff;
    font-size: x-large;
    }

    .canvas {
    clear: both;
    height: 200px;
    width: 215px;
    float: left;
    position: relative;
    right: -35%;
    border: 2px solid #666666;
    background-color: #ccc;
    margin-right: 1px;
    margin-top: 10px;
    line-height: 2em;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 3px #000;
    box-shadow: inset 0 0 3px #000;
    background: -moz-linear-gradient(left center, rgb(0, 0, 0),
    rgb(79, 79, 79), rgb(21, 21, 21) );
    background: -webkit-gradient(linear, left top, right top, color-stop(0, rgb(0, 0, 0)
    ), color-stop(0.50, rgb(79, 79, 79) ), color-stop(1, rgb(21, 21, 21) )
    );
    color: #fff;
    font-size: x-large;
    height: 200px;
    }

    .over {
    border: 2px dashed #000;
    }

    Javascript Code:
    // Global Variable
    var myarray = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
    "12", "13", "14", "15", "" ];
    var gameStatus = 0;
    var moveCount = 0;
    var positionDifference = 0;

    // If you have 10 number of 200x200 images, use 9.
    var imageNumber = Math.floor(Math.random()*9);
    var sourceDiv = null;
    var bestMove;
    var bestTime;

    $(document)
    .ready(

    function () {

    Element.prototype.hasClassName = function(name) {
    return new RegExp("(?:^|\\s+)" + name
    + "(?:\\s+|$)").test(this.className);
    };

    Element.prototype.addClassName = function(name) {
    if (!this.hasClassName(name)) {
    this.className = this.className ? [
    this.className, name ].join(' ') : name;
    }
    };

    Element.prototype.removeClassName = function(name) {
    if (this.hasClassName(name)) {
    var c = this.className;
    this.className = c.replace(
    new RegExp("(?:^|\\s+)" + name
    + "(?:\\s+|$)", "g"), "");
    }
    };



    function handleDragStart(e) {
    // Target (this) element is the source node.
    sourceDiv = this;
    this.style.opacity = '0.7';
    e.dataTransfer.effectAllowed = 'move';
    e.dataTransfer.setData('text/html', this.innerHTML);
    }

    function handleDragEnter(e) {
    // this / e.target is the current hover target.
    this.addClassName('over');
    }

    function handleDragOver(e) {
    if (e.preventDefault) {
    e.preventDefault(); // Necessary. Allows us to
    // drop.
    }

    e.dataTransfer.dropEffect = 'move'; // See the
    // section on
    // the
    // DataTransfer
    // object.
    this.addClassName('over');
    return false;
    }

    function handleDragLeave(e) {
    this.removeClassName('over'); // this / e.target
    // is previous
    // target element.
    }

    function handleDrop(e) {
    // this/e.target is current target element.
    this.style.opacity = '1.0';
    sourceDiv.style.opacity = '1.0';

    if (e.stopPropagation) {
    e.stopPropagation(); // Stops some browsers
    // from redirecting.
    }

    // Stop Game when it is over
    if (!gameStatus)
    {

    // Don't do anything if dropping the same column
    // we're dragging.
    if (sourceDiv != this) {

    // Swap boxes only if one of them is BLANK
    if (sourceDiv.innerHTML == ""
    || this.innerHTML == "") {

    // Restrict box movements to one level (up,
    // down, left, right)
    positionDifference = Math.abs(sourceDiv.id
    - this.id);

    if (positionDifference == 1
    || positionDifference == 4) {

    if (positionDifference == 4 || (!((sourceDiv.id%4==0 && this.id%4==1) || (sourceDiv.id%4==1 && this.id%4==0))))
    {
    // Set the source column's HTML to the
    // HTML of the column we dropped on.
    sourceDiv.innerHTML = this.innerHTML;
    if (e.dataTransfer.getData('text/html') == null) {
    this.innerHTML = "";
    } else {
    this.innerHTML = e.dataTransfer
    .getData('text/html');
    }

    moveCount = moveCount + 1;

    // Check Game Status
    gameStatus = checkGameStatus();

    if (gameStatus) {
    if (bestMove == null || (bestMove > moveCount))
    {
    bestMove = moveCount;
    localStorage.setItem('BEST_MOVE',bestMove);
    document.getElementById("bestMove").innerHTML = bestMove + " moves";
    }
    document.getElementById("status").innerHTML = "You are Awesome!!. You took " + moveCount + " moves";
    document.getElementById("flashMessage").innerHTML = "Game Over";
    $('#flashMessage').slideDown('1000');
    $('#flashMessage').delay('3000');
    $('#flashMessage').slideUp('1000');
    } else {
    document.getElementById("status").innerHTML = "Number Of Moves :: "
    + moveCount;
    }

    drawCanvas();

    }
    else {
    displayNotification();
    }
    }
    else {
    displayNotification();
    }
    } else {
    displayNotification();
    }

    }
    }
    return false;
    }

    function handleDragEnd(e) {
    // this/e.target is the source node.
    var cols = document.querySelectorAll('.tile');
    [].forEach.call(cols, function(col) {
    col.removeClassName('over');
    });

    toggleFlashMessage();

    }


    $('#saveGameButton').click(function() {

    if (!gameStatus)
    {
    for ( var i = 0; i < 16; i++) { myarray[i] = document.getElementById(i + 1).innerHTML; } localStorage.setItem('MY_ARRAY', myarray.toString()); localStorage.setItem('MOVE_COUNT', moveCount); localStorage.setItem('IMAGE_NUMBER',imageNumber); localStorage.setItem('GAME_STATUS',gameStatus); console.log("Move Count" +localStorage.getItem('MOVE_COUNT')); console.log("Image Number" +localStorage.getItem('IMAGE_NUMBER')); document.getElementById("flashMessage").innerHTML = "Game saved successfully"; $('#flashMessage').slideDown('1000'); $('#flashMessage').delay('1000'); $('#flashMessage').slideUp('1000'); } }); var cols = document.querySelectorAll('.tile'); [].forEach.call(cols, function(col) { col.addEventListener('dragstart', handleDragStart, false); col.addEventListener('dragenter', handleDragEnter, false); col.addEventListener('dragover', handleDragOver, false); col.addEventListener('dragleave', handleDragLeave, false); col.addEventListener('drop', handleDrop, false); col.addEventListener('dragend', handleDragEnd, false); }); }); function toggleFlashMessage() { $('#flashMessage').slideUp('1000'); } function initialize() { $('#notificationMessage').hide(); $('#flashMessage').hide(); if ((imageNumber = localStorage.getItem('IMAGE_NUMBER')) == null) { imageNumber = Math.floor(Math.random()*5); } if ((moveCount = localStorage.getItem('MOVE_COUNT')) == null) { moveCount = 0; document.getElementById("status").innerHTML = "Ready to start?"; } else { moveCount = parseInt(moveCount); if (moveCount != 0) { document.getElementById("status").innerHTML = "Number Of Moves :: " + moveCount; } else { document.getElementById("status").innerHTML = "Ready to start?"; } } if ((gameStatus = localStorage.getItem('GAME_STATUS')) == null) { gameStatus = 0; } else { gameStatus = parseInt(gameStatus); } if ((bestMove = localStorage.getItem('BEST_MOVE')) == null) { document.getElementById("bestMove").innerHTML = ""; } else { document.getElementById("bestMove").innerHTML = bestMove + " moves"; } if (localStorage.getItem('MY_ARRAY') == null) { // Shuffle Array Contents myarray.sort(function() { return 0.5 - Math.random(); }); } else { myarray = localStorage.getItem('MY_ARRAY').split(','); document.getElementById("flashMessage").innerHTML = "Game restored from previous session"; $('#flashMessage').slideDown('1000'); $('#flashMessage').delay('1000'); $('#flashMessage').slideUp('1000'); } // Populate Boxes with Numbers for ( var i = 0; i < myarray.length; i++) { document.getElementById(i + 1).innerHTML = myarray[i]; } drawCanvas(); } function clearStorage() { localStorage.removeItem('MOVE_COUNT'); localStorage.removeItem('MY_ARRAY'); localStorage.removeItem('IMAGE_NUMBER'); localStorage.removeItem('GAME_STATUS'); } function saveGame() { for ( var i = 0; i < 16; i++) { myarray[i] = document.getElementById(i + 1).innerHTML; } localStorage.setItem('MY_ARRAY', myarray.toString()); localStorage.setItem('MOVE_COUNT', moveCount); localStorage.setItem('IMAGE_NUMBER',imageNumber); localStorage.setItem('GAME_STATUS',gameStatus); console.log("Move Count" +localStorage.getItem('MOVE_COUNT')); console.log("Image Number" +localStorage.getItem('IMAGE_NUMBER')); } function checkGameStatus() { for ( var i = 1; i < myarray.length; i++) { var currentElement = document.getElementById(i); if (currentElement.innerHTML != i) { return 0; } } return 1; } function displayNotification() { document.getElementById("flashMessage").innerHTML = "Move not allowed"; $('#flashMessage').fadeIn('slow'); $('#flashMessage').delay(2000); $('#flashMessage').fadeOut('slow'); } function drawCanvas() { var canvas = document.getElementById("myCanvas"); var context = canvas.getContext("2d"); canvas.width = 200; canvas.height = 200; context.clearRect(0,0,200,200); var imageObj = new Image(); imageObj.onload = function(){ // draw cropped image var x = 0; var y = 0; var x1 = 0; var y1 = 0; var width = 50; var height = 50; var element; var elementValue; var elementId; for (var i=0;i<16;i++) { element = document.getElementById(i+1); elementValue = element.innerHTML; elementId = element.id; // Handle blank box if (elementValue == null || elementValue == "") { elementValue = "16"; } x = ((elementValue-1)%4) * 50; y = parseInt(((elementValue-1)/4)) * 50; x1 = ((elementId-1)%4) * 50; y1 = parseInt(((elementId-1)/4)) * 50; context.drawImage(imageObj, x, y, width, height, x1, y1, width, height); } }; imageObj.src = imageNumber+".jpg"; }



    Instructions: Create .html, .css, .js files with the above code. Download JQuery 1.6.2 to the same folder. Add 10 (jpg) images of exact size 200x200px. Have all the 14 files in the same folder.


    Reference: http://www.html5rocks.com/en/tutorials/dnd/basics/