简体   繁体   中英

Java Swing draws rectangle not as intended?

I tried to place some rectangles in the center. But it's not drawing at the right position. My code:

import javax.swing.JPanel;

public class Canvas extends JPanel {
    
    Player player = new Player(this);
    
    public Canvas() {
        //setBackground(Color.blue);
        setBounds(270, 0, 1230, 1000);
        setLayout(null);
    }
    
    public void start() {
        int FPS = 60;
        double drawIinterval = 1000000000/FPS;
        double delta = 0;
        long lastTime = System.nanoTime();
        long currentTime;
        long timer = 0;
        int drawCount = 0;
        while(true) {
            
            currentTime = System.nanoTime();
            
            delta += (currentTime -lastTime) / drawIinterval;
            timer += (currentTime -lastTime);
            lastTime = currentTime;
            
            if(delta >= 1) {
                update();
                repaint();
                delta--;
                drawCount++;
            }
            
            if(timer >= 1000000000) {
                System.out.println("FPS: " + drawCount);
                drawCount = 0;
                timer = 0;
            }
            
        }
    }
    
      private void update() {
        
          player.update();
        
    }

    @Override
      public void paintComponent(Graphics g) {
          super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            
            player.draw(g2);
            
            g2.drawRect(size().width/2 - 100/2 ,size().height/2 - 100/2, 100, 100);
      }
}

I also tried to place it next to the right border

g2.drawRect(size().width - 100 ,size().height - 100, 100, 100);

but that placed it inside the border. Just placing it on the left or top side worked. Any explanation for that?

Edit: When trying to draw a line through the whole panel

g2.drawLine(0, 0, size().width, size().height);

it doesn't reach the bottom corner and just enters about 20 pixels before the bottom.

Main class:

class Main extends JFrame{

public static void main(String[] args) {
    

    
    
    Canvas canvas = new Canvas();
    
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);
    frame.setSize(1500, 1000);
    frame.setLocationRelativeTo(null);
    frame.add(canvas);
    frame.setVisible(true);
    
    canvas.start();
}

}

The reason your line isn't going to the corner must have something to do with the Window you are displaying your canvas in. I modified your code so it could render the line and this is what I came out with. The line does go all the way to the corner.

在此处输入图像描述

Here's my version of your code.

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Canvas extends JPanel
{
    private static final long serialVersionUID = 1L;

    public Canvas()
    {
        setBackground(Color.blue);
        setForeground(Color.white);
        setBounds(270, 0, 1230, 1000);
        setLayout(null);
    }


    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

//      g2.drawRect(size().width / 2 - 100 / 2, size().height / 2 - 100 / 2, 100, 100);
        g2.setStroke(new BasicStroke(3));
        g2.drawLine(0, 0, size().width, size().height);
}

    public static void main(String args[])
    {
        JFrame frame = new JFrame();
        Canvas canvas = new Canvas();
        frame.setLayout(new BorderLayout());
        frame.add(canvas, BorderLayout.CENTER);
        frame.setBounds(100, 100, 500, 500);
        frame.setVisible(true);
    }
}

You can draw a rectangle around you canvas in paintComponent to prove this idea.

    g2.drawRect(0, 0, getWidth()-1, getHeight()-1);

A real simple way to help you figure out what is taking up the extra space is to add system outs or logging to the paintComponent

        System.out.println(this);

In my example it shows me the visibleRect as I resize the canvas.

Canvas[,0,0,468x412,alignmentX=0.0,alignmentY=0.0,border=,flags=11,maximumSize=,minimumSize=,preferredSize=]
Canvas[,0,0,468x411,alignmentX=0.0,alignmentY=0.0,border=,flags=11,maximumSize=,minimumSize=,preferredSize=]
Canvas[,0,0,468x410,alignmentX=0.0,alignmentY=0.0,border=,flags=11,maximumSize=,minimumSize=,preferredSize=]
Canvas[,0,0,469x410,alignmentX=0.0,alignmentY=0.0,border=,flags=11,maximumSize=,minimumSize=,preferredSize=]
...

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