Java questions about coordinates with Graphics

Deadly 提交于 2021-01-29 13:13:50

问题


If I create a JFrame 800x600 pixels and draw a line from (0,0) to (800,600) it doesn't go from corner to corner, so, where is the (0,0) and where is the (800,600)? Here is the code

import java.awt.Graphics;

import javax.swing.JFrame;

public class Point0_0test extends JFrame {

    public Point0_0test() {
        setTitle("Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawLine(0, 0, 800, 600);
    }

    public static void main(String[] args) {
        Point0_0test test = new Point0_0test();
        test.setVisible(true);
    }

}

Here you can see what appears when the program is running


回答1:


The JFrame size and coordinates count the size of the decorations, such as the top part of the window contains the bar with the close button, the rest contain the extra outline that is added on Windows(Ubuntu, at least, doesn't seem to add an extra outline). In order to get a line like you would want to, you should use JFrame.getInsets(), which returns the size of the GUI that decorates the JFrame, like this:


    import java.awt.Insets;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Insets in = getInsets();
        g.drawLine(in.left, in.top, 800-in.right, 600-in.bottom);
    }

Edit: this means that you don't have an actual 800x600 space. The Insets class seems to be "created" when setVisible(true) is called, as far as I can tell. So this would be how the code for that looks like:


import javax.swing.JFrame;
import java.awt.Insets;
import java.awt.Graphics;

public class InsetsTest extends JFrame {

    public InsetsTest() {
        super();
        setTitle("Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800,600);
        setVisible(true);
        Insets insets= getInsets();
        setSize(800+insets.right+insets.left,600+insets.top+insets.bottom);

        setLocationRelativeTo(null);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Insets in = getInsets();
        g.drawLine(in.left, in.top, 800+in.left, 600+in.top);
    }

    public static void main(String[] args) {
        InsetsTest test = new InsetsTest();
        test.setVisible(true);
    }

}```



回答2:


If you want a drawing area that's 800 x 600 pixels, then set a drawing area that's 800 x 600 pixels. Who cares how big the frame is?

Here's a simple drawing GUI that I created. I made it 400 x 300 pixels so it would fit in the answer easier.

Here's the code. It's a minimal, runnable example for setting the size of the drawing area.

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

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

public class SimpleDrawingArea implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SimpleDrawingArea());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Simple Drawing Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DrawingPanel());
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = 1L;

        public DrawingPanel() {
            this.setPreferredSize(new Dimension(400, 300));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setStroke(new BasicStroke(4f));
            g2d.drawLine(0, 0, 400, 300);
        }

    }

}



回答3:


The window size should be defined without OS specific window decoration.

Try to add

this.setUndecorated(true);

before the

this.setVisible(true);


来源:https://stackoverflow.com/questions/61086862/java-questions-about-coordinates-with-graphics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!