Advertisement
raden_rangga

java_swing_frame

Oct 2nd, 2022
865
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | Source Code | 0 0
  1. package learnJava;
  2. import javax.swing.*;
  3. import java.awt.*;
  4.  
  5. public class FirstApp {
  6.     public static void main(String[] args) {
  7. SwingUtilities.invokeLater(new AppComponent().goLauch()); }}
  8. class AppComponent{
  9.     int[][] tileColor(){
  10.         int[][] arr = {{0, 255} , {255, 0}};
  11.         return arr;
  12.     }
  13.     Runnable goLauch(){
  14.         JFrame frame = new JFrame("Basic");
  15.         ImageIcon imgicon = new ImageIcon("./src/lpk/basics/icon.png");
  16.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  17.         frame.setIconImage(imgicon.getImage());
  18.         frame.add(new Tilers(tileColor()));
  19.         frame.pack();
  20.         frame.setVisible(true);
  21.         frame.setLocationRelativeTo(null);
  22.         return null;}}
  23. class Tilers extends JPanel {
  24.     private int[][] tileColors; private int pictureSize = 200;
  25.     public Tilers(int[][] tileColor) {this.tileColors = tileColor;}
  26.  
  27.     public Dimension getPreferredSize() {        
  28.         return new Dimension(pictureSize, pictureSize);
  29.     }
  30.  
  31.     public void paintComponent(Graphics graphics) {
  32.         super.paintComponent(graphics);
  33.         int numberOfTilesPerSide = tileColors.length;
  34.         int sizeofEachTile = pictureSize / numberOfTilesPerSide;
  35.         for (int row = 0; row < numberOfTilesPerSide; row++) {
  36.             for (int column = 0; column < numberOfTilesPerSide; column++) {
  37.                 int offsetX = column * sizeofEachTile;
  38.                 int offsetY = row * sizeofEachTile;
  39.                 int intensity = tileColors[row][column];
  40.                 Color tileColor = new Color(intensity, intensity, intensity);
  41.                 graphics.setColor(tileColor);
  42.                 graphics.fillRect(offsetX, offsetY, sizeofEachTile, sizeofEachTile);}}}}
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement