Graphics - 使用一个参数(getSize())在整个画一条线 window

Graphics - Use a parameter (getSize()) to draw a line throughout the whole window

我需要帮助。我想为从 getSize() 获得的 drawLine() 方法提供一个参数。我想使用 getSize() 方法在整个 window 中画一条线。

package PROG2;

import java.awt.*;
import javax.swing.*;

class MyComponent extends JComponent {

    @Override
    public void paintComponent(Graphics g) {
        g.drawLine(100, 100, 200, 200);
    }
}
    
public class Übung1 extends JFrame{

    public static void berechnen() {
        int height = frame.getHeight(); //Here it says it doesn't know "frame" variable but I don't know how to declare it here.
        int width = frame.getWidth();
    }

    public static void main(String[] args){
            JFrame frame = new JFrame("First window");
            berechnen();
            frame.add(new MyComponent());
            frame.setSize(400, 400);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            Graphics g = frame.getGraphics();

            // int width = frame.getWidth();
            // int height = frame.getHeight();
            System.out.println("Größe:" + frame.getSize());
            //System.out.println(width);
    }
}

正如安德鲁所说,

  • 您不想获取 JFrame 的尺寸或大小,而是想获取在 JFrame 的 contentPane 中显示的组件,这里是您的 MyComponent 实例。
  • 获取该信息的最佳位置是在绘制线条之前的 paintComponent 方法内部。
  • 并且总是先调用super的绘画方法

我还推荐:

  • 如果您想要不透明的图像,请在 JPanel 的 paintComponent 方法中绘制,而不是在 JComponent 中绘制
  • 除非绝对需要,否则避免使用静态方法和字段

请注意,在下面的代码中,红线穿过 JPanel 的对角线,并继续绘制对角线,即使手动调整 JFrame 的大小时也是如此:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;

import javax.swing.*;

public class DrawLine extends JPanel {
    private static final Stroke LINE_STROKE = new BasicStroke(15f);
    private static final Dimension PREF_SIZE = new Dimension(800, 650);

    public DrawLine() {
        setPreferredSize(PREF_SIZE);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        // code to make the line smooth (antialias the line to remove jaggies)
        // and to make the line thick, using a wider "stroke"
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(LINE_STROKE);

        // if we want a red line
        g2.setColor(Color.RED);

        // this is the key code here:
        int width = getWidth();
        int height = getHeight();
        
        // draw along the main diagonal:
        g2.drawLine(0, 0, width, height);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            DrawLine mainPanel = new DrawLine();

            JFrame frame = new JFrame("GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }

}