// RandomCircles Example // Source code file: MainActivity.kt // Draw 100 random circles using 7 colors. // Redraw 100 more random circles when // the button is clicked. package it372.ssmith.randomcircles import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.foundation.Canvas import androidx.compose.foundation.background import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.size import androidx.compose.material3.Button import androidx.compose.material3.Slider import androidx.compose.material3.Text import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.draw.clipToBounds import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { MainLayout() } } } @Composable fun MainLayout( ) { // When trigger is changed by the Button onClick lambda, // the Canvas is recomposed so that the circles are // redrawn var trigger by remember { mutableStateOf(0)} Column( modifier = Modifier.padding(20.dp) .background(Color(0xFFE0E0E0)) .fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally ) { // Click Button to redraw random circles Button( onClick = { trigger++; }, modifier = Modifier.padding(all = 15.dp) ) { Text("Draw Circles", fontSize = 30.sp) } Canvas( modifier = Modifier .padding(10.dp) .size(350.dp, 350.dp) .background(Color(0xFFC0C0C0)) .clipToBounds( ) .fillMaxSize( )) { // onDraw lambda, which recomposes the // Canvas and redraws the circles when // trigger is updated. var currentTrigger = trigger; // Number of circles to draw and redraw. var n = 100; var colors = listOf(Color.Red, Color.Yellow, Color.Green, Color.Cyan, Color.Magenta, Color.Blue, Color.White) var colorIndex = 0; for (i in 1..n) { colorIndex = (colorIndex + 1) % 7; var x = (10..900).random().toFloat( ) var y = (10..900).random().toFloat( ) var r = (5..100).random().toFloat( ) // Draw filled circle with specified // color. drawCircle( color = colors[colorIndex], radius = r, center = Offset(x = x, y = y) ) // Draw circle border. drawCircle( color = Color.Black, radius = r, center = Offset(x = x, y = y), style = Stroke(5f) ) } } } } @Preview(showBackground = true) @Composable fun MainLayoutPreview() { MainLayout() }