Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import javax.swing.JFrame;
- /**
- *
- *
- * @author Giacinto Di Santis
- * @version 02.04.2020
- */
- public class IdenticonFrame extends JFrame {
- public final int MARGIN = 50;
- public IdenticonFrame() {
- super("IdemticonFrame");
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setSize(300,300);
- }
- public boolean avatar[][]={
- {true, false, true, true, true},
- {true, false, false, false, false},
- {true, false, true, false, false},
- {true, false, false, false, false},
- {true, false, true, true, true}
- };
- public void setAvatar(boolean[][] avatar){
- this.avatar = avatar;
- }
- public void paint(final Graphics g) {
- setAvatar(avatar);
- super.paint(g);
- int x;
- int y;
- int lato1;
- int lato2;
- for(x = 0; x < this.avatar.length; x++){
- for(y = 0; y < this.avatar[x].length; y++){
- if(avatar[x][y] == true){
- g.setColor(Color.BLACK);
- }else{
- g.setColor(Color.WHITE);
- }
- if(getHeight() > getWidth()){
- lato1 = (getWidth() - (2 * MARGIN)) / this.avatar[x].length;
- lato2 = (getWidth() - (2 * MARGIN)) / this.avatar.length;
- }else{
- lato1 = (getHeight() - (2 * MARGIN)) / this.avatar[x].length;
- lato2 = (getHeight() - (2 * MARGIN)) / this.avatar.length;
- }
- int marginX = (getWidth() - lato1 * this.avatar[x].length - MARGIN * 2) / 2;
- int marginY = (getHeight() - lato2 * this.avatar.length - MARGIN * 2) / 2;
- g.fillRect(marginX + (x * lato1) + MARGIN, marginY + (y * lato2) + MARGIN, lato1, lato2);
- }
- }
- }
- public static void main(final String[] args) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new IdenticonFrame().setVisible(true);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement