0% found this document useful (0 votes)
4 views

MCLExp7

Mobile computing 5th Exp

Uploaded by

priti.rumao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MCLExp7

Mobile computing 5th Exp

Uploaded by

priti.rumao
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Vidyavardhini’s College of Engineering & Technology

Department of Computer Engineering


Academic Year: 2023-24

EXPERIMENT NO. 7
AIM: Write an application that draws basic graphical primitives on the screen.

Procedure:
Creating a new project:
1. Open Android Studio and then click on File -> New -> New project.
2. Then type the Application name as “ex.no.8″ and click Next.
3. Then select the Minimum SDK and click Next.
4. Then select the Empty Activity and click Next.
5. Finally click Finish.
It will take some time to build and load the project.
Designing layout for the Android Application:
1. Click on app -> res -> layout -> activity_main.xml.
2. Now click on Text.
3. Then delete the code which is there and type the code as given below.

Code for Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView" />
</RelativeLayout>

4. Now click on Design. This completes design part.


Java Coding for the Android Application:
1. Click on app -> java -> com.example.exno8 -> MainActivity.
2. Then delete the code which is there and type the code as given below.
Code for MainActivity.java:

package com.example.exno7; color & TextSize


import android.app.Activity; Paint paint = new Paint();
import android.graphics.Bitmap; paint.setColor(Color.BLUE);
import android.graphics.Canvas;
import android.graphics.Color; paint.setTextSize(50);
import android.graphics.Paint; //To draw a Rectangle
import canvas.drawText("Rectangle",
android.graphics.drawable.BitmapDrawable 420, 150, paint);
; canvas.drawRect(400, 200, 650,
Vidyavardhini’s College of Engineering & Technology
Department of Computer Engineering
Academic Year: 2023-24

import android.os.Bundle; 700, paint);


import android.widget.ImageView;
public class MainActivity extends Activity //To draw a Circle
{ canvas.drawText("Circle", 120,
@Override 150, paint);
public void onCreate(Bundle canvas.drawCircle(200, 350, 150,
savedInstanceState) paint);
{ //To draw a Square
super.onCreate(savedInstanceState); canvas.drawText("Square", 120,
800, paint);
setContentView(R.layout.activity_main); canvas.drawRect(50, 850, 350,
//Creating a Bitmap 1150, paint);
Bitmap bg = Bitmap.createBitmap(720, //To draw a Line
1280, Bitmap.Config.ARGB_8888); canvas.drawText("Line", 480, 800,
//Setting the Bitmap as background for paint);
the ImageView canvas.drawLine(520, 850, 520,
ImageView i = (ImageView) 1150, paint);
findViewById(R.id.imageView); }
i.setBackgroundDrawable(new }
BitmapDrawable(bg));
//Creating the Canvas Object
Canvas canvas = new Canvas(bg);
//Creating the Paint Object and set its
Output:

Conclusion: Thus, we have successfully implemented an application that draws basic graphical
primitives on the screen.

You might also like