Why does the titlebar overlap the pixels of JPanel. Here some code:
protected void init() {
this.setContentPane(new MyPanel());
this.setSize(915, 725);
this.setVisible(true);
}
class MyPanel extends JPanel {
@Override
public void paintComponent(Graphics _g) {
super.paintComponent(_g);
Graphics2D g = (Graphics2D)_g;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Monospaced", Font.PLAIN, 14);
g.setFont(font);
g.setColor(Color.red);
for(int x=0; x<60; x++) {
for(int y=0; y< 50; y++) {
g.drawString("█", x*15, 0);
}
}
}
}
I have added the JPanel to the JFrame and draw some String at y=0.
y = 0is at the top of the panel, and the coordinates grow downwards. If you want a 20-pixel high line of text to appear just at the top of the panel, then you need to draw it aty = 20. More generally, you can use theFontMetricsclass to determine the height of your line of text and use an appropriate offset:In your current code, you’re drawing characters (which are of the order of 20 pixels high) in the cells of a 1-pixel grid. This means that your characters will significantly overlap each other. You’ll want to step your loops by a value much greater than 1 here, to give the characters enough space.