Hallo!
Ich möchte eine Tastatur zeichnen.
Dafür habe ich folgendes
Irgendwie ist da recht noch zu viel Platz
warum?
Oder gibt es vielleicht einfachere Methode?
Ich möchte eine Tastatur zeichnen.
Dafür habe ich folgendes
Java:
package com.luxaaa.pbvortrag.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class KeyBoard extends JPanel {
private static final long serialVersionUID = 1L;
String[][] keys = new String[][] {
new String[] {"^", "1","2" ,"3" ,"4" ,"5" ,"6" ,"7","8", "9", "0", "ß"},
new String[] {"*", "Q", "W","E" ,"R" ,"T" ,"Z" ,"U" ,"I","O", "P", "Ü"},
new String[] {"CL", "A", "S","D" ,"F" ,"G" ,"H" ,"J" ,"K","L", "Ö", "Ä"},
new String[] {"SH","Y", "X","C" ,"V" ,"B" ,"N" ,"M" ,",",".", "-"}
};
private int keyPadding = 10;
int keyW, keyH;
float spacePercentage = 0.6f;
private float[][] keyScales = new float[][] {
new float[] {1f, 1f,1f ,1f ,1f ,1f ,1f ,1f,1f, 1f, 1f, 1f},
new float[] {1.2f, 1f,1f ,1f ,1f ,1f ,1f ,1f,1f, 1f, 1f, 1f, 1f},
new float[] {1.4f, 1f,1f ,1f ,1f ,1f ,1f ,1f,1f, 1f, 1f, 1f, 1f},
new float[] {1.6f, 1f,1f ,1f ,1f ,1f ,1f ,1f,1f, 1f, 1f, 1f}
};
private int width, height;
// graphic stuff
Color colBg = Color.DARK_GRAY;
Color colKey = Color.LIGHT_GRAY;
public KeyBoard(int w) {
this.width = w;
keyW = calcKeyW(w);
keyH = keyW;
height = (keys.length+1)*keyPadding + (keys.length+1)*keyH + keyPadding;
setPreferredSize(new Dimension(width, height));
System.out.println(keyW);
}
private int calcKeyW(int width) {
// longest key row
float longestRow = 0;
for (int i=0; i<keyScales.length; i++) {
float sum =0;
for(int j=0; j<keyScales[i].length; j++) {
sum += keyScales[i][j];
}
if(sum > longestRow) longestRow = sum;
}
int maxKeys = 0;
for(String[] keyRow : keys) {
if(keyRow.length > maxKeys) maxKeys = keyRow.length;
}
// padding
int padding = (int)(keyPadding*(maxKeys+1));
// key size
return (int)((width-padding) / longestRow);
}
public void paintComponent(Graphics g) {
// paint background
g.setColor(colBg);
g.fillRect(0, 0, width, height);
// draw KeyBoard
g.setColor(colKey);
for(int i=0; i<keys.length; i++) {
for (int j=0; j<keys[i].length; j++) {
int padding = (j+1)*keyPadding;
int keyX = 0;
if(j>0) {
for(int k =0; k<j; k++) {
keyX += (int) (keyScales[i][k]*keyW);
}
}
keyX +=padding;
g.drawRect(keyX , i*keyH + i*keyPadding + keyPadding, (int)(keyScales[i][j]*keyW), keyH);
g.drawString(keys[i][j], keyX, i*keyH + i*keyPadding + keyPadding);
}
}
// draw space key
int spaceW = (int)( (float)width*spacePercentage);
int spaceX = (width-spaceW) /2;
int spaceY = height - keyH - keyPadding;
g.drawRect(spaceX, spaceY, spaceW, keyH);
}
}
Irgendwie ist da recht noch zu viel Platz
warum?
Oder gibt es vielleicht einfachere Methode?