Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.51 KB | None | 0 0
  1. package project;
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.image.BufferedImage;
  7.  
  8. import javax.swing.JComponent;
  9. import javax.swing.JOptionPane;
  10.  
  11. public class ColorDisplay extends JComponent {
  12.     private final int PAGE_SIZE = 7;
  13.     private java.awt.Color[][] systemPaint;
  14.     private int[][] userPaint;
  15.     private int background;
  16.     private java.awt.Color displayBackground;
  17.     private int grid;
  18.     private java.awt.Color displayGrid;
  19.     private int gridStroke = 2;
  20.     private int sideSize = 40;
  21.     private int horizontalPages = 1;
  22.     private int verticalPages = 1;
  23.     private BufferedImage displayBuffer;  
  24.     private Graphics2D dBufferG;
  25.     private int width;
  26.     private int height;
  27.  
  28.     public ColorDisplay(int background, int grid) {
  29.         this(1, 1, background, grid);
  30.     }
  31.    
  32.     public ColorDisplay(int verticalPages, int horizontalpages, int background, int grid) {
  33.         this(verticalPages, horizontalpages, background, grid, 2, 40);
  34.     }
  35.    
  36.     public ColorDisplay(int verticalPages, int horizontalpages, int grid, int gridStroke, int sideSize, int[][] array) {
  37.         this.verticalPages = verticalPages;
  38.         this.horizontalPages = horizontalpages;
  39.         this.grid = grid;
  40.         this.gridStroke = gridStroke;
  41.         this.sideSize = sideSize;
  42.         this.displayGrid = toColor(grid);
  43.        
  44.         if(sideSize>0) {
  45.             setPreferredSize(new Dimension(horizontalpages*PAGE_SIZE * (sideSize+ gridStroke) + gridStroke,
  46.                                            verticalPages*PAGE_SIZE * (sideSize+ gridStroke) + gridStroke));
  47.         }
  48.         systemPaint = new java.awt.Color[verticalPages* PAGE_SIZE][horizontalpages* PAGE_SIZE];
  49.         userPaint = new int[verticalPages* PAGE_SIZE][horizontalpages* PAGE_SIZE];
  50.        
  51.         for(int i = 0; i < array.length; i++){
  52.             for(int j = 0; j < array[i].length; j++){
  53.                 if(array[i][j] == 1){
  54.                     this.displayBackground = toColor(Color.BLUE);
  55.                 }else if(array[i][j] == 0){
  56.                     this.displayBackground = toColor(Color.BLACK);
  57.                 }
  58.             }
  59.         }
  60.        
  61. //        for(int row = 0; row < systemPaint.length; row++) {
  62. //            for(int col = 0; col < systemPaint[row].length; col++) {
  63. //                //systemPaint[row][col] = this.displayBackground;
  64. //                //userPaint[row][col] = background;
  65. //            }
  66. //        }
  67.     }
  68.    
  69.     private java.awt.Color toColor(int color) {
  70.         return new java.awt.Color(Color.red(color),Color.green(color),Color.blue(color),Color.alpha(color));
  71.     }
  72.    
  73.     public void setBackgroundColor(int background) {
  74.         this.background = background;
  75.         super.repaint();
  76.     }
  77.  
  78.     public void setGridColor(int grid) {
  79.         this.grid = grid;
  80.         super.repaint();
  81.     }
  82.  
  83.     public void setGridStroke(int gridStroke) {
  84.         this.gridStroke = gridStroke;
  85.         super.repaint();
  86.     }
  87.  
  88.     public void setSideSize(int sideSize) {
  89.         this.sideSize = sideSize;
  90.         super.repaint();
  91.     }
  92.  
  93.     public int getBackgroundColor() {
  94.         return background;
  95.     }
  96.  
  97.     public int getGridColor() {
  98.         return grid;
  99.     }
  100.  
  101.     public int getGridStroke() {
  102.         return gridStroke;
  103.     }
  104.  
  105.     public int getSideSize() {
  106.         return sideSize;
  107.     }
  108.  
  109.     public int getHorizontalPages() {
  110.         return horizontalPages;
  111.     }
  112.  
  113.     public int getVerticalPages() {
  114.         return verticalPages;
  115.     }
  116.  
  117.     public void clearDisplay() {
  118.         if(userPaint!=null && systemPaint!=null) {
  119.             for(int row = 0; row < systemPaint.length; row++) {
  120.                 for(int col = 0; col < systemPaint[row].length; col++) {
  121.                     userPaint[row][col] = background;
  122.                 }
  123.             }
  124.             updateDisplay();
  125.         }
  126.     }
  127.    
  128.     public void repaint() {
  129.         updateDisplay();
  130.     }
  131.    
  132.     public void updateDisplay() {
  133.         if(userPaint!=null && systemPaint!=null) {
  134.             for(int row = 0; row<systemPaint.length; row++)
  135.                 for(int col=0; col<systemPaint[row].length; col++) {
  136.                     systemPaint[row][col] = toColor(userPaint[row][col]);
  137.                 }
  138.         }
  139.         super.repaint();
  140.     }
  141.  
  142.  
  143.     public void setDisplay(int[][] colors) {
  144.         setDisplay(colors,0,0);
  145.     }
  146.  
  147.     public void setDisplay(int[][] colors, int verticalPage, int horizontalPage) {
  148.         if(colors.length==PAGE_SIZE && colors[0].length== PAGE_SIZE &&
  149.                 verticalPage>=0 && verticalPage<verticalPages &&
  150.                 horizontalPage>=0 && horizontalPage<horizontalPages) {
  151.             int rowOffset = verticalPage* PAGE_SIZE;
  152.             int colOffset = horizontalPage* PAGE_SIZE;
  153.             for(int row = 0; row < colors.length; row++) {
  154.                 for(int col = 0; col < colors[row].length; col++) {
  155.                     userPaint[rowOffset + row][colOffset + col] = colors[row][col];
  156.                 }
  157.             }
  158.         }
  159.     }
  160.    
  161.    
  162.     protected void paintComponent(Graphics g) {
  163.         super.paintComponent(g);
  164.         if(width!=this.getWidth() || height!=this.getHeight()) {
  165.             width = this.getWidth();
  166.             height = this.getHeight();
  167.             displayBuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  168.             dBufferG = displayBuffer.createGraphics();
  169.         }
  170.         int side = Math.min((getWidth()- gridStroke)/(horizontalPages* PAGE_SIZE)- gridStroke, (getHeight()- gridStroke)/(verticalPages*PAGE_SIZE)- gridStroke);
  171.         int offsetX = (getWidth()-systemPaint[0].length*(gridStroke+side)-gridStroke)/2; // beräknas för centrering
  172.         int offsetY = (getHeight()-systemPaint.length*(gridStroke+side)-gridStroke)/2; // beräknas för centrering
  173.         dBufferG.setColor(displayGrid);
  174.         dBufferG.fillRect(0, 0, getWidth(), getHeight());
  175.         for (int row = 0; row < systemPaint.length; row++) {
  176.             for (int col = 0; col < systemPaint[row].length; col++) {
  177.                 dBufferG.setColor(displayBackground);
  178.                 dBufferG.fillRect(offsetX + gridStroke + col * (side + gridStroke), offsetY + gridStroke + row * (side + gridStroke), side, side);
  179.                 dBufferG.setColor(systemPaint[row][col]);
  180.                 dBufferG.fillRect(offsetX + gridStroke + col * (side + gridStroke), offsetY + gridStroke + row * (side + gridStroke), side, side);
  181.             }
  182.         }
  183.         g.drawImage(displayBuffer, 0, 0, null);
  184.     }
  185.    
  186.     public void pause(long ms) {
  187.         try {
  188.             Thread.sleep(ms);
  189.         }catch(InterruptedException e) {}
  190.     }
  191.    
  192.     public static void main(String[] args) {
  193. //      int[][] arr = {{Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED},
  194. //              {Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED},
  195. //              {Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED},
  196. //              {Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED},
  197. //              {Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED},
  198. //              {Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED},
  199. //              {Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED,Color.RED}};
  200. //     
  201.        
  202.         int[][] a = {
  203.                 {0,0,1,1,1,0,0},       
  204.                 {0,0,1,0,1,0,0},
  205.                 {0,1,0,0,0,1,0},
  206.                 {0,1,1,1,1,1,0},            //A
  207.                 {1,0,0,0,0,0,1},
  208.                 {1,0,0,0,0,0,1},
  209.                 {1,0,0,0,0,0,1}};
  210.        
  211.         int[][] b = {
  212.                 {1,1,1,1,1,1,1},       
  213.                 {1,1,1,1,1,1,1},
  214.                 {0,1,0,0,0,1,0},
  215.                 {0,1,1,1,1,1,0},            //A
  216.                 {1,0,0,0,0,0,1},
  217.                 {1,0,0,0,0,0,1},
  218.                 {1,0,0,0,0,0,1}};
  219.        
  220.         ColorDisplay d = new ColorDisplay(1,1,Color.BLACK,1,100, b);
  221.         //d.setDisplay(a,2,1); // arr har röd-värde i samtliga element
  222.         d.updateDisplay();
  223.  
  224.         JOptionPane.showMessageDialog(null,d);
  225. //      JOptionPane.showMessageDialog(null, new ColorDisplay(Color.BLUE,Color.BLACK));
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement