| 1 | // Copyright © 2004-2006 University of Helsinki, Department of Computer Science | |
| 2 | // Copyright © 2012 various contributors | |
| 3 | // This software is released under GNU Lesser General Public License 2.1. | |
| 4 | // The license text is at http://www.gnu.org/licenses/lgpl-2.1.html | |
| 5 | ||
| 6 | package fi.helsinki.cs.titokone; | |
| 7 | ||
| 8 | import fi.helsinki.cs.ttk91.TTK91Memory; | |
| 9 | ||
| 10 | import javax.swing.*; | |
| 11 | import java.awt.*; | |
| 12 | import java.awt.image.BufferedImage; | |
| 13 | ||
| 14 | // Display class was added by Toni Ruottu 8.4.2012 | |
| 15 | ||
| 16 | public class Display extends JPanel implements Runnable { | |
| 17 | ||
| 18 | static final int X = 160, Y = 120; | |
| 19 | static final int DEFAULT_START = 0x2000; | |
| 20 | static TTK91Memory mem; | |
| 21 | 3 | boolean updates = false; |
| 22 | //BufferedImage backBuffer; | |
| 23 | BufferedImage compatible; | |
| 24 | 3 | protected int baseAddress = DEFAULT_START; |
| 25 | 3 | protected int lastX = 0; |
| 26 | 3 | protected int lastY = 0; |
| 27 | /* Display methods */ | |
| 28 | ||
| 29 | public Display() { | |
| 30 | } | |
| 31 | ||
| 32 | protected void checkBuffers() { | |
| 33 | 4 | if (lastX != getWidth() || lastY != getHeight()) { |
| 34 | 1 | Graphics2D g = (Graphics2D) getGraphics(); |
| 35 | 1 | GraphicsConfiguration gc = g.getDeviceConfiguration(); |
| 36 | 2 | lastX = getWidth(); |
| 37 | 2 | lastY = getHeight(); |
| 38 | /*backBuffer=new BufferedImage( | |
| 39 | lastX, | |
| 40 | lastY, | |
| 41 | BufferedImage.TYPE_INT_RGB);*/ | |
| 42 | 2 | compatible = gc.createCompatibleImage(lastX, lastY); |
| 43 | 1 | clearBuffer(); |
| 44 | } | |
| 45 | } | |
| 46 | ||
| 47 | public void setMem(TTK91Memory mem) { | |
| 48 | this.mem = mem; | |
| 49 | } | |
| 50 | ||
| 51 | public void setUpdates(boolean updates) { | |
| 52 | 1 | this.updates = updates; |
| 53 | } | |
| 54 | ||
| 55 | public void setBaseAddress(int base) { | |
| 56 | 2 | if (base > 0) { |
| 57 | 1 | this.baseAddress = base; |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | /* Runnable methods */ | |
| 62 | ||
| 63 | public void run() { | |
| 64 | while (true) { | |
| 65 | 1 | if (updates) { |
| 66 | 1 | updateBuffer(); |
| 67 | } | |
| 68 | try { | |
| 69 | 3 | Thread.sleep(40); /* limit to max 25fps*/ |
| 70 | } catch (InterruptedException e) { | |
| 71 | ||
| 72 | } | |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | /* Canvas methods */ | |
| 77 | ||
| 78 | public void update(Graphics g) { | |
| 79 | 5 | g.drawImage(compatible, 0, 0, Color.red, null); |
| 80 | } | |
| 81 | ||
| 82 | public void paint(Graphics g) { | |
| 83 | 1 | update(g); |
| 84 | } | |
| 85 | ||
| 86 | /* private helpers */ | |
| 87 | ||
| 88 | private void updateBuffer() { | |
| 89 | JPanel canvas = this; | |
| 90 | 1 | int canvasWidth = canvas.getWidth(); |
| 91 | 1 | int canvasHeight = canvas.getHeight(); |
| 92 | 1 | int canvasArea = canvasWidth * canvasHeight; |
| 93 | 4 | if (canvasArea < 1) { |
| 94 | return; | |
| 95 | } | |
| 96 | /* make sure we have something valid to draw on*/ | |
| 97 | 1 | checkBuffers(); |
| 98 | 1 | drawMode();//draw using current mode.. |
| 99 | ||
| 100 | 1 | repaint(); |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * see which mode we are in and use it to draw | |
| 105 | */ | |
| 106 | protected void drawMode() { | |
| 107 | 1 | Graphics2D g = (Graphics2D) compatible.getGraphics(); |
| 108 | 1 | if (mem != null) { |
| 109 | 1 | draw12bit(g); |
| 110 | } | |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * gray screen out | |
| 115 | */ | |
| 116 | protected void clearBuffer() { | |
| 117 | 1 | Graphics2D g = (Graphics2D) compatible.getGraphics(); |
| 118 | 1 | g.setColor(Color.GRAY); |
| 119 | 7 | g.fillRect(0, 0, getWidth(), getHeight()); |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * 12bit color mode | |
| 124 | */ | |
| 125 | protected void draw12bit(Graphics2D g) { | |
| 126 | //xscale,yscale set so that we always hit full pixels | |
| 127 | 4 | int xscale = roundUp(((double) lastY) / Y); |
| 128 | 4 | int yscale = roundUp(((double) lastX) / X); |
| 129 | 5 | for (int i = 0; i < lastY; i++) { |
| 130 | 5 | for (int j = 0; j < lastX; j++) { |
| 131 | 1 | int x = j / xscale; |
| 132 | 1 | int y = i / yscale; |
| 133 | ||
| 134 | 8 | if (x < X && y < Y) { |
| 135 | 6 | int color = mem.getValue(baseAddress + (y * X + x)); |
| 136 | 8 | compatible.setRGB(j, i, 0xff000000 | torgb8(color & 0xfff)); |
| 137 | } | |
| 138 | } | |
| 139 | } | |
| 140 | } | |
| 141 | ||
| 142 | protected int roundUp(double d) { | |
| 143 | 4 | return (int) (d + 0.5); |
| 144 | } | |
| 145 | ||
| 146 | static private int torgb8(int rgb4i) { | |
| 147 | long rgb4l = (long) rgb4i; | |
| 148 | 6 | long r4 = (rgb4l & 0xf00) >>> 8; |
| 149 | 6 | long g4 = (rgb4l & 0x0f0) >>> 4; |
| 150 | 3 | long b4 = rgb4l & 0x00f; |
| 151 | 4 | long r8 = (r4 << 4) | r4; |
| 152 | 4 | long g8 = (g4 << 4) | g4; |
| 153 | 4 | long b8 = (b4 << 4) | b4; |
| 154 | 17 | long rgb8l = ((r8 << 16) & 0xff0000) | ((g8 << 8) & 0x00ff00) | (b8 & 0x0000ff); |
| 155 | int rgb8i = (int) rgb8l; | |
| 156 | 1 | return rgb8i; |
| 157 | } | |
| 158 | } | |
Mutations | ||
| 21 |
Substituted 0 with 1 : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE Removed assignment to member variable updates : NO_COVERAGE |
|
| 24 |
Removed assignment to member variable baseAddress : NO_COVERAGE Substituted 8192 with 8193 : NO_COVERAGE Replaced constant value of 8192 with 8193 : NO_COVERAGE |
|
| 25 |
Substituted 0 with 1 : NO_COVERAGE Removed assignment to member variable lastX : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE |
|
| 26 |
Substituted 0 with 1 : NO_COVERAGE Removed assignment to member variable lastY : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE |
|
| 33 |
negated conditional : NO_COVERAGE removed call to fi/helsinki/cs/titokone/Display::getWidth : NO_COVERAGE negated conditional : NO_COVERAGE removed call to fi/helsinki/cs/titokone/Display::getHeight : NO_COVERAGE |
|
| 34 |
removed call to fi/helsinki/cs/titokone/Display::getGraphics : NO_COVERAGE |
|
| 35 |
removed call to java/awt/Graphics2D::getDeviceConfiguration : NO_COVERAGE |
|
| 36 |
Removed assignment to member variable lastX : NO_COVERAGE removed call to fi/helsinki/cs/titokone/Display::getWidth : NO_COVERAGE |
|
| 37 |
removed call to fi/helsinki/cs/titokone/Display::getHeight : NO_COVERAGE Removed assignment to member variable lastY : NO_COVERAGE |
|
| 42 |
Removed assignment to member variable compatible : NO_COVERAGE removed call to java/awt/GraphicsConfiguration::createCompatibleImage : NO_COVERAGE |
|
| 43 |
removed call to fi/helsinki/cs/titokone/Display::clearBuffer : NO_COVERAGE |
|
| 52 |
Removed assignment to member variable updates : NO_COVERAGE |
|
| 56 |
negated conditional : NO_COVERAGE changed conditional boundary : NO_COVERAGE |
|
| 57 |
Removed assignment to member variable baseAddress : NO_COVERAGE |
|
| 65 |
negated conditional : NO_COVERAGE |
|
| 66 |
removed call to fi/helsinki/cs/titokone/Display::updateBuffer : NO_COVERAGE |
|
| 69 |
Replaced constant value of 40 with 41 : NO_COVERAGE removed call to java/lang/Thread::sleep : NO_COVERAGE Substituted 40 with 41 : NO_COVERAGE |
|
| 79 |
removed call to java/awt/Graphics::drawImage : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE |
|
| 83 |
removed call to fi/helsinki/cs/titokone/Display::update : NO_COVERAGE |
|
| 90 |
removed call to javax/swing/JPanel::getWidth : NO_COVERAGE |
|
| 91 |
removed call to javax/swing/JPanel::getHeight : NO_COVERAGE |
|
| 92 |
Replaced integer multiplication with division : NO_COVERAGE |
|
| 93 |
negated conditional : NO_COVERAGE Substituted 1 with 0 : NO_COVERAGE changed conditional boundary : NO_COVERAGE Substituted 1 with 0 : NO_COVERAGE |
|
| 97 |
removed call to fi/helsinki/cs/titokone/Display::checkBuffers : NO_COVERAGE |
|
| 98 |
removed call to fi/helsinki/cs/titokone/Display::drawMode : NO_COVERAGE |
|
| 100 |
removed call to fi/helsinki/cs/titokone/Display::repaint : NO_COVERAGE |
|
| 107 |
removed call to java/awt/image/BufferedImage::getGraphics : NO_COVERAGE |
|
| 108 |
negated conditional : NO_COVERAGE |
|
| 109 |
removed call to fi/helsinki/cs/titokone/Display::draw12bit : NO_COVERAGE |
|
| 117 |
removed call to java/awt/image/BufferedImage::getGraphics : NO_COVERAGE |
|
| 118 |
removed call to java/awt/Graphics2D::setColor : NO_COVERAGE |
|
| 119 |
removed call to java/awt/Graphics2D::fillRect : NO_COVERAGE removed call to fi/helsinki/cs/titokone/Display::getWidth : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE removed call to fi/helsinki/cs/titokone/Display::getHeight : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE |
|
| 127 |
Replaced constant value of 120.0 with 1.0 : NO_COVERAGE Substituted 120.0 with 1.0 : NO_COVERAGE Replaced double division with multiplication : NO_COVERAGE removed call to fi/helsinki/cs/titokone/Display::roundUp : NO_COVERAGE |
|
| 128 |
Substituted 160.0 with 1.0 : NO_COVERAGE removed call to fi/helsinki/cs/titokone/Display::roundUp : NO_COVERAGE Replaced constant value of 160.0 with 1.0 : NO_COVERAGE Replaced double division with multiplication : NO_COVERAGE |
|
| 129 |
Substituted 0 with 1 : NO_COVERAGE negated conditional : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE Changed increment from 1 to -1 : NO_COVERAGE changed conditional boundary : NO_COVERAGE |
|
| 130 |
Substituted 0 with 1 : NO_COVERAGE Substituted 0 with 1 : NO_COVERAGE negated conditional : NO_COVERAGE changed conditional boundary : NO_COVERAGE Changed increment from 1 to -1 : NO_COVERAGE |
|
| 131 |
Replaced integer division with multiplication : NO_COVERAGE |
|
| 132 |
Replaced integer division with multiplication : NO_COVERAGE |
|
| 134 |
Replaced constant value of 120 with 121 : NO_COVERAGE Substituted 160 with 161 : NO_COVERAGE changed conditional boundary : NO_COVERAGE Replaced constant value of 160 with 161 : NO_COVERAGE negated conditional : NO_COVERAGE negated conditional : NO_COVERAGE changed conditional boundary : NO_COVERAGE Substituted 120 with 121 : NO_COVERAGE |
|
| 135 |
Substituted 160 with 161 : NO_COVERAGE Replaced integer addition with subtraction : NO_COVERAGE removed call to fi/helsinki/cs/ttk91/TTK91Memory::getValue : NO_COVERAGE Replaced integer multiplication with division : NO_COVERAGE Replaced constant value of 160 with 161 : NO_COVERAGE Replaced integer addition with subtraction : NO_COVERAGE |
|
| 136 |
Substituted -16777216 with -16777215 : NO_COVERAGE removed call to fi/helsinki/cs/titokone/Display::torgb8 : NO_COVERAGE Substituted 4095 with 4096 : NO_COVERAGE Replaced bitwise AND with OR : NO_COVERAGE removed call to java/awt/image/BufferedImage::setRGB : NO_COVERAGE Replaced constant value of -16777216 with -16777215 : NO_COVERAGE Replaced bitwise OR with AND : NO_COVERAGE Replaced constant value of 4095 with 4096 : NO_COVERAGE |
|
| 143 |
Substituted 0.5 with 1.0 : NO_COVERAGE replaced return of integer sized value with (x == 0 ? 1 : 0) : NO_COVERAGE Replaced constant value of 0.5 with 1.0 : NO_COVERAGE Replaced double addition with subtraction : NO_COVERAGE |
|
| 148 |
Substituted 8 with 9 : NO_COVERAGE Replaced Unsigned Shift Right with Shift Left : NO_COVERAGE Replaced constant value of 3840 with 3841 : NO_COVERAGE Replaced bitwise AND with OR : NO_COVERAGE Replaced constant value of 8 with 9 : NO_COVERAGE Substituted 3840 with 3841 : NO_COVERAGE |
|
| 149 |
Replaced constant value of 240 with 241 : NO_COVERAGE Substituted 4 with 5 : NO_COVERAGE Replaced Unsigned Shift Right with Shift Left : NO_COVERAGE Substituted 4 with 5 : NO_COVERAGE Replaced bitwise AND with OR : NO_COVERAGE Substituted 240 with 241 : NO_COVERAGE |
|
| 150 |
Replaced constant value of 15 with 16 : NO_COVERAGE Substituted 15 with 16 : NO_COVERAGE Replaced bitwise AND with OR : NO_COVERAGE |
|
| 151 |
Replaced Shift Left with Shift Right : NO_COVERAGE Substituted 4 with 5 : NO_COVERAGE Replaced bitwise OR with AND : NO_COVERAGE Substituted 4 with 5 : NO_COVERAGE |
|
| 152 |
Substituted 4 with 5 : NO_COVERAGE Substituted 4 with 5 : NO_COVERAGE Replaced bitwise OR with AND : NO_COVERAGE Replaced Shift Left with Shift Right : NO_COVERAGE |
|
| 153 |
Replaced bitwise OR with AND : NO_COVERAGE Replaced Shift Left with Shift Right : NO_COVERAGE Substituted 4 with 5 : NO_COVERAGE Substituted 4 with 5 : NO_COVERAGE |
|
| 154 |
Replaced bitwise OR with AND : NO_COVERAGE Replaced Shift Left with Shift Right : NO_COVERAGE Substituted 255 with 256 : NO_COVERAGE Replaced bitwise OR with AND : NO_COVERAGE Replaced constant value of 16 with 17 : NO_COVERAGE Replaced bitwise AND with OR : NO_COVERAGE Substituted 65280 with 65281 : NO_COVERAGE Replaced Shift Left with Shift Right : NO_COVERAGE Substituted 8 with 9 : NO_COVERAGE Replaced constant value of 8 with 9 : NO_COVERAGE Substituted 16711680 with 16711681 : NO_COVERAGE Replaced constant value of 65280 with 65281 : NO_COVERAGE Replaced bitwise AND with OR : NO_COVERAGE Replaced constant value of 255 with 256 : NO_COVERAGE Replaced constant value of 16711680 with 16711681 : NO_COVERAGE Replaced bitwise AND with OR : NO_COVERAGE Substituted 16 with 17 : NO_COVERAGE |
|
| 156 |
replaced return of integer sized value with (x == 0 ? 1 : 0) : NO_COVERAGE |