简体   繁体   中英

Java - Top inset not returning the proper value when painting

having a problem with top insets not giving the proper value when painting ... i am looking for y to be just under the title bar but it is not working correctly and putting it 25 pixels from the top where as the title bar only accounts for about 14 pixels .

Game Frame :

import java.awt.Dimension;

import javax.swing.JFrame;


public class gameFrame extends JFrame {

static int width = 400;
static int height = 400;
static int topinset;
static int FPS = 30;

public gameFrame(){
    gamePanel gamePanel = new gamePanel();
    gamePanel.setPreferredSize(new Dimension(width,height));
    Thread thread = new Thread(gamePanel);

    this.add(gamePanel);
    this.setResizable(false);
    this.setSize(width, height);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);

    topinset = this.getInsets().top;

    thread.start();
}

public static void main(String[] args){
    gameFrame gameFrame = new gameFrame();

}
}

Game Panel :

import java.awt.Graphics;

import javax.swing.JPanel;


public class gamePanel extends JPanel implements Runnable {

public gamePanel(){
    this.setBounds(0,0,gameFrame.width,gameFrame.height);
    this.setVisible(true);
}

@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);

    g.drawString("Width: "+ gameFrame.width + " Height: " + gameFrame.height, 0, gameFrame.topinset);
}

@Override
public void run() {
    while(true){
        repaint();
        try {Thread.sleep(1000*gameFrame.FPS);} 
        catch (InterruptedException e) {}
    }
}

}
  1. You are doing your custom painting on the panel so there is no need to worry about the offsets of the frame. Just use the getWidth() and getHeight() methods to get the current size of the panel.

  2. If you want your panel to be (400, 400), then override the getPreferredSize() method of your panel class to return that dimension. Then use frame.pack() and the frame will size itself properly to include the panel size and the frame decorations.

  3. There is no need for static variables. Those variables should just be instance variables or constants.

  4. There is no need for the setBounds() since the layout manager will determine the size/location of the panel in the frame.

  5. There is no need to make the panel visible since is it visible by default.

  6. Follow Java naming conventions. Class names should start with an upper case character.

  7. Why are you doing custom painting? Why not just add a JLabel to the NORTH of the frame and display your text in the label?

Start by taking a look through Working with Text APIs

Text rendering is typically done from the base line, meaning text will painted above the y position you specify

As has already being stated, a components Graphics context is translated before it's painted so that the 0x0 position will be the top/left corner of the component

In the following example, the first String will not appear on the screen, as it's base line is along the top edge of the component, whereas the second String has being adjusted so that it's ascent line is along the top top

文本

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics g2d = (Graphics2D) g.create();
    FontMetrics fm = g2d.getFontMetrics();
    g2d.drawString("Hello", 0, 0);
    g2d.drawString("Hello", fm.stringWidth("Hello"), fm.getAscent());
    g2d.dispose();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM