Advertisement
Giacinto_Di_Santis

Compito Java

Apr 8th, 2020
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import javax.swing.JFrame;
  4.  
  5. /**
  6.  *
  7.  *
  8.  * @author Giacinto Di Santis
  9.  * @version 02.04.2020
  10.  */
  11.  
  12. public class IdenticonFrame extends JFrame {
  13.  
  14.     public final int MARGIN = 50;
  15.  
  16.     public IdenticonFrame() {
  17.         super("IdemticonFrame");
  18.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  19.         this.setSize(300,300);
  20.     }
  21.  
  22.     public boolean avatar[][]={
  23.         {true,  false,  true,  true,  true},
  24.         {true,  false,  false, false, false},
  25.         {true,  false,  true,  false, false},
  26.         {true,  false,  false, false, false},
  27.         {true,  false,  true,  true,  true}
  28.     };
  29.    
  30.     public void setAvatar(boolean[][] avatar){
  31.         this.avatar = avatar;
  32.     }
  33.  
  34.     public void paint(final Graphics g) {
  35.         setAvatar(avatar);
  36.         super.paint(g);
  37.         int x;
  38.         int y;
  39.         int lato1;
  40.         int lato2;
  41.         for(x = 0; x < this.avatar.length; x++){
  42.             for(y = 0; y < this.avatar[x].length; y++){
  43.                 if(avatar[x][y] == true){
  44.                     g.setColor(Color.BLACK);
  45.                 }else{
  46.                     g.setColor(Color.WHITE);
  47.                 }
  48.                 if(getHeight() > getWidth()){
  49.                     lato1 = (getWidth() - (2 * MARGIN)) / this.avatar[x].length;
  50.                     lato2 = (getWidth() - (2 * MARGIN)) / this.avatar.length;
  51.                 }else{
  52.                     lato1 = (getHeight() - (2 * MARGIN)) / this.avatar[x].length;
  53.                     lato2 = (getHeight() - (2 * MARGIN)) / this.avatar.length;
  54.                 }
  55.                 int marginX = (getWidth() - lato1 * this.avatar[x].length - MARGIN * 2) / 2;
  56.                 int marginY = (getHeight() - lato2 * this.avatar.length - MARGIN * 2) / 2;
  57.                 g.fillRect(marginX + (x * lato1) + MARGIN, marginY +  (y * lato2) + MARGIN, lato1, lato2);
  58.             }
  59.         }
  60.     }
  61.  
  62.     public static void main(final String[] args) {
  63.         java.awt.EventQueue.invokeLater(new Runnable() {
  64.             public void run() {
  65.                 new IdenticonFrame().setVisible(true);
  66.             }
  67.         });
  68.     }
  69.    
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement