Can you run this code and tell me what would cause missing graphic data like this?
If you are familier with a guitar chord diagram then you will see what I am talking about.
Sorta Like This.... click here
Code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
public class ChordGrid extends JFrame {
PrintPreviewTest printPreviewTest = new PrintPreviewTest();
ChordGrid() {
super("Gridy Grid Grid");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(printPreviewTest);
this.setSize(545, 720);
setVisible(true);
}
public static void main(String[] args) {
new ChordGrid();
}
}
class PrintPreviewTest extends JPanel {
BufferedImage diagram;
private final int DPI = 72, UNIT = 12, PAPER_X = 612, PAPER_Y = 792;
private int strings = 6, frets = 4;
private BufferedImage[][] chordImageGrid = new BufferedImage[3][3];
boolean[][] dChord, aChord, eChord;
public void setTheSize() {
setPreferredSize(new Dimension((int)(10.5 * DPI), 13 * DPI));
}
PrintPreviewTest() {
dChord = new boolean[6][5];
aChord = new boolean[6][5];
eChord = new boolean[6][5];
dChord[2][0] = true;
dChord[3][2] = true;
dChord[4][3] = true;
dChord[5][2] = true;
aChord[1][0] = true;
aChord[2][2] = true;
aChord[3][2] = true;
aChord[4][2] = true;
eChord[0][0] = true;
eChord[1][2] = true;
eChord[2][2] = true;
eChord[3][1] = true;
eChord[4][0] = true;
eChord[5][0] = true;
setChordImage(eChord, 0, 0);
setChordImage(aChord, 1, 0);
setChordImage(dChord, 2, 0);
setChordImage(eChord, 0, 1);
setChordImage(aChord, 1, 1);
setChordImage(dChord, 2, 1);
setChordImage(aChord, 0, 2);
setChordImage(dChord, 1, 2);
setChordImage(eChord, 2, 2);
}
private void setChordImage(boolean[][] chordData, int gx, int gy) {
diagram = new BufferedImage(UNIT*13, UNIT*18, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = diagram.createGraphics();
g2d.setPaint(Color.BLACK);
for(int col = 0; col < strings; col++) {
for(int row = 0; row <= frets; row++) {
//Draw Strings
g2d.drawLine((int)(UNIT*1.5 + UNIT*col*2), UNIT*2, (int)(UNIT*1.5 + col*2*UNIT), (int)(UNIT*2 + frets*UNIT*3.5));
//Draw Frets
g2d.drawLine(UNIT, (int)(UNIT*2 + UNIT*row*3.5), UNIT*12, (int)(UNIT*2 + UNIT*row*3.5));
//Draw Fretted Notes
// ****** Even With This Commented Out it still draws the grid WRONG
if (chordData[col][row]) {
g2d.fillRect((int)(UNIT*.8+UNIT*2*col), (int)(UNIT*3.3*row), (int)(UNIT*1.5), (int)(UNIT*1.5));
}
// ****** Even With This Commented Out it still draws the grid WRONG
}
}
chordImageGrid[gx][gy] = diagram;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.darkGray);
Graphics2D g2 = (Graphics2D) g;
g2.translate(5, 5);
g2.scale(.85, .85);
g.setColor(Color.WHITE);
g.fillRect(0, 0, 612, 792);
//Create 3x3 Grid
g.setColor(new Color(230, 230, 230));
int gridRow = 3, gridCol = 3;
for(int row = 0; row < gridRow; row++) {
for(int col = 0; col < gridCol; col++) {
g.drawRect(DPI + (col * (13*UNIT)), DPI + (row * (18 * UNIT)), 13 * UNIT, 18 * UNIT);
}
}
//Place Chord Dagram
g.setColor(Color.BLACK);
for(int row = 0; row < gridRow; row++) {
for(int col = 0; col < gridCol; col++) {
g2.drawImage(chordImageGrid[col][row], null, DPI + (col * (UNIT*13)), DPI + (row * (UNIT * 18)));
}
}
}
}