1 /* 2 * Copyright (c) 2016 Helmut Neemann 3 * Use of this source code is governed by the GPL v3 license 4 * that can be found in the LICENSE file. 5 */ 6 package de.neemann.digital.gui.components.graphics; 7 8 import javax.swing.*; 9 import java.awt.*; 10 11 /** 12 * The component to show the graphics. 13 */ 14 public class GraphicComponent extends JComponent { 15 private static final Color[] PALETTE = createPalette(); 16 17 private final int width; 18 private final int height; 19 private long[] data; 20 private int offs; 21 22 /** 23 * Creates a new instance. 24 * 25 * @param width the width in pixels 26 * @param height the height in pixels 27 */ GraphicComponent(int width, int height)28 public GraphicComponent(int width, int height) { 29 this.width = width; 30 this.height = height; 31 32 int pw = 640 / width; 33 if (pw < 1) pw = 1; 34 int ph = 400 / height; 35 if (ph < 1) ph = 1; 36 int pixSize = (pw + ph) / 2; 37 38 Dimension size = new Dimension(width * pixSize, height * pixSize); 39 setPreferredSize(size); 40 setOpaque(true); 41 } 42 43 /** 44 * Updates the graphics window 45 * 46 * @param data the data to show 47 * @param bank the bank to show 48 */ updateGraphic(long[] data, boolean bank)49 public void updateGraphic(long[] data, boolean bank) { 50 this.data = data; 51 if (bank) 52 offs = width * height; 53 else 54 offs = 0; 55 repaint(); 56 } 57 58 @Override paintComponent(Graphics g)59 protected void paintComponent(Graphics g) { 60 if (data != null) 61 for (int x = 0; x < width; x++) { 62 int xPos = x * getWidth() / width; 63 int dx = (x + 1) * getWidth() / width - xPos; 64 for (int y = 0; y < height; y++) { 65 int p = (int) data[offs + y * width + x]; 66 if (p >= PALETTE.length) p = 1; 67 g.setColor(PALETTE[p]); 68 69 int ypos = y * getHeight() / height; 70 int dy = (y + 1) * getHeight() / height - ypos; 71 72 g.fillRect(xPos, ypos, dx, dy); 73 } 74 } 75 } 76 createPalette()77 private static Color[] createPalette() { 78 Color[] col = new Color[0x10000]; 79 for (int i = 0; i < col.length; i++) 80 col[i] = Color.BLACK; 81 col[0] = Color.WHITE; 82 col[1] = Color.BLACK; 83 col[2] = Color.RED; 84 col[3] = Color.GREEN; 85 col[4] = Color.BLUE; 86 col[5] = Color.YELLOW; 87 col[6] = Color.CYAN; 88 col[7] = Color.MAGENTA; 89 col[8] = Color.ORANGE; 90 col[9] = Color.PINK; 91 92 for (int g = 0; g < 32; g++) { 93 int in = 255 - getComp(g, 32); 94 col[32 + g] = new Color(in, in, in); 95 } 96 97 int index = 64; 98 for (int r = 0; r < 4; r++) 99 for (int g = 0; g < 4; g++) 100 for (int b = 0; b < 4; b++) { 101 col[index] = new Color(getComp(r, 4), getComp(g, 4), getComp(b, 4)); 102 index++; 103 } 104 105 index = 0x8000; 106 for (int r = 0; r < 32; r++) 107 for (int g = 0; g < 32; g++) 108 for (int b = 0; b < 32; b++) { 109 col[index] = new Color(getComp(r, 32), getComp(g, 32), getComp(b, 32)); 110 index++; 111 } 112 113 return col; 114 } 115 getComp(int c, int values)116 private static int getComp(int c, int values) { 117 return (255 * c) / (values - 1); 118 } 119 } 120