Ciao PGi sono riuscito a scrivere questo codice ed ottenere quel disegno che avevvo messo in allegato, adesso devo riuscire a generalizzarlo ? Ti sembra un buon inizio ?
Codice PHP:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.font.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
buildAndShow();
}
});
}
private static void buildAndShow() {
JFrame frame = new JFrame("Grid sample");
GridPanel panel = new GridPanel();
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
class GridPanel extends JComponent {
private int colWidth = 50;
private int rowHeight = 50;
private int rowCount = 5;
Point2D origine = new Point2D.Double(10,10);
private int rowHeightbis = 50;
private double y = origine.getY();
private Font fontEtichette = new Font("Serif", Font.PLAIN, 12);
public Dimension getPreferredSize() {
Dimension standardSize = super.getPreferredSize();
standardSize.width = colWidth;
standardSize.height = rowCount * rowHeight;
return standardSize;
}
public void paint(Graphics graphics) {
Graphics2D g = (Graphics2D)graphics;
int [] valoriDecibel = {10, 20, 25, 30, 40};
g.setPaint(Color.BLACK);
for(int i=0; i<valoriDecibel.length; i++){
if(valoriDecibel[i] == 25||valoriDecibel[i]==30) rowHeightbis = rowHeightbis/2;
Rectangle2D.Double area = new Rectangle2D.Double(
origine.getX(),
y,
colWidth,
rowHeightbis);
g.draw(area);
disegnaStringaInBassoADestra(area, g , fontEtichette , String.valueOf(valoriDecibel[i]));
y += rowHeightbis;
rowHeightbis = 50;
}
}
private void disegnaStringaInBassoADestra(Rectangle2D area, Graphics2D graphics, Font font, String
text) {
FontRenderContext context = graphics.getFontRenderContext();
TextLayout layout = new TextLayout(text, font, context);
Rectangle2D bounds = layout.getBounds();
double y = area.getY() + bounds.getHeight() + (area.getHeight()-bounds.getHeight());
double x = area.getX() + area.getWidth() - bounds.getWidth();
graphics.setPaint(Color.BLACK);
layout.draw(graphics, (float)(x), (float)(y));
}
}