Samuel_Berkat_Hulu

dice roll app - ppb tugas 4

Apr 16th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package com.example.dicerollerapplication
  2.  
  3. import android.os.Bundle
  4. import androidx.activity.ComponentActivity
  5. import androidx.activity.compose.setContent
  6. import androidx.compose.foundation.Image
  7. import androidx.compose.foundation.layout.Column
  8. import androidx.compose.foundation.layout.Spacer
  9. import androidx.compose.foundation.layout.fillMaxSize
  10. import androidx.compose.foundation.layout.height
  11. import androidx.compose.foundation.layout.wrapContentSize
  12. import androidx.compose.material3.Button
  13. import androidx.compose.material3.MaterialTheme
  14. import androidx.compose.material3.Surface
  15. import androidx.compose.material3.Text
  16. import androidx.compose.runtime.Composable
  17. import androidx.compose.runtime.getValue
  18. import androidx.compose.runtime.mutableStateOf
  19. import androidx.compose.runtime.remember
  20. import androidx.compose.runtime.setValue
  21. import androidx.compose.ui.Alignment
  22. import androidx.compose.ui.Modifier
  23. import androidx.compose.ui.res.painterResource
  24. import androidx.compose.ui.res.stringResource
  25. import androidx.compose.ui.tooling.preview.Preview
  26. import androidx.compose.ui.unit.dp
  27. import androidx.compose.ui.unit.sp
  28. import com.example.dicerollerppb_f.R
  29. import com.example.dicerollerppb_f.ui.theme.DiceRollerPPBFTheme
  30.  
  31. class MainActivity : ComponentActivity() {
  32.     override fun onCreate(savedInstanceState: Bundle?) {
  33.         super.onCreate(savedInstanceState)
  34.         setContent {
  35.             DiceRollerPPBFTheme {
  36.                 // A surface container using the 'background' color from the theme
  37.                 Surface(
  38.                     modifier = Modifier.fillMaxSize(),
  39.                     color = MaterialTheme.colorScheme.background
  40.                 ) {
  41.                     DiceRollerApp()
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48.  
  49. @Preview
  50. @Composable
  51. fun DiceRollerApp() {
  52.     DiceWithButtonAndImage(modifier = Modifier
  53.         .fillMaxSize()
  54.         .wrapContentSize(Alignment.Center)
  55.     )
  56. }
  57.  
  58. @Composable
  59. fun DiceWithButtonAndImage(modifier: Modifier = Modifier) {
  60.     var result by remember { mutableStateOf( 1) }
  61.     val imageResource = when(result) {
  62.         1 -> R.drawable.dice_1
  63.         2 -> R.drawable.dice_2
  64.         3 -> R.drawable.dice_3
  65.         4 -> R.drawable.dice_4
  66.         5 -> R.drawable.dice_5
  67.         else -> R.drawable.dice_6
  68.     }
  69.     Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
  70.         Image(painter = painterResource(imageResource), contentDescription = result.toString())
  71.  
  72.         Button(
  73.             onClick = { result = (1..6).random() },
  74.         ) {
  75.             Text(text = stringResource(R.string.roll), fontSize = 24.sp)
  76.         }
  77.     }
  78. }
Add Comment
Please, Sign In to add comment