0% found this document useful (0 votes)
37 views2 pages

Android Lab 8

The document describes a Java program that draws a target with five concentric circles. The program creates a TargetView class that extends View and overrides the onDraw method. OnDraw uses two Paint objects, one colored red and one white, to draw ovals representing each circle by calling canvas.drawOval and varying the size and position of the drawn ovals based on a for loop that iterates five times. The main activity creates an instance of TargetView and sets it as the content view.

Uploaded by

Ehab Zaben
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

Android Lab 8

The document describes a Java program that draws a target with five concentric circles. The program creates a TargetView class that extends View and overrides the onDraw method. OnDraw uses two Paint objects, one colored red and one white, to draw ovals representing each circle by calling canvas.drawOval and varying the size and position of the drawn ovals based on a for loop that iterates five times. The main activity creates an instance of TargetView and sets it as the content view.

Uploaded by

Ehab Zaben
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Visual Programming Lab 8 Oraib M Alrashdan

Task 1: Target exercise


package com.example.admin.myapplication;

import android.content.Context;
import android.util.AttributeSet;
import android.graphics.RectF;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


TargetView targetView;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
targetView = new TargetView(this);

setContentView(targetView);
}
}
class TargetView extends View

{ public TargetView(Context context)


{ super(context); }

@Override

protected void onDraw (Canvas canvas)


{
super.onDraw(canvas);

Paint red = new Paint();


red.setARGB(255, 255, 0, 0);
Paint white = new Paint();
white.setARGB(255, 255, 255, 255);
int w = canvas.getWidth();
int h = canvas.getHeight();
for (int i = 0; i < 5; i++) {
canvas.drawOval(new RectF(w * i / 10, h * i / 10, w * (10 - i) / 10, h *
(10 - i) / 10),
i % 2 == 0 ? red : white);
}
}

The output:
Visual Programming Lab 8 Oraib M Alrashdan

Assignments:

Write your java code to draw the following applications:

You might also like