图形 - 使用参数(getize())在整个窗口中绘制一条线

发布于 2025-01-25 16:33:49 字数 1165 浏览 2 评论 0原文

我在这里需要帮助。我想为drawline()方法提供一个参数,该方法是我从getsize()获得的。我想使用getsize()方法在整个窗口中绘制一条线。

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);
    }
}

I need help here. I want to give a parameter to the drawLine() method which I get from getSize(). I want to draw a line throughout the whole window by using the getSize() method.

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动次打次papapa 2025-02-01 16:33:49

正如安德鲁(Andrew)已经说过的那样,

  • 您不想获得Jframe的尺寸或大小,而是在Jframe的ContentPane中显示的组件,此处是您的MyComponent实例。
  • 获取该信息的最佳场所在绘制行之前的PaintComponent方法本身内部。
  • 并始终首先调用超级绘画方法

,我还建议:

  • 在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);
        });
    }

}

As Andrew already stated,

  • you don't want to get the dimensions or size of the JFrame but rather the component that is being displayed within the JFrame's contentPane, here your MyComponent instance.
  • The best place to get that information is inside of the paintComponent method itself, just prior to drawing the line.
  • And always call the super's painting method first

I also recommend:

  • Draw within a JPanel's paintComponent method, not a JComponent, if you want an opaque image
  • Avoid static methods and fields unless absolutely needed

Note that in the code below, the red line draws through the JPanel's diagonal, and continues to draw the diagonal, even when the JFrame is manually resized:

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);
        });
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文