在 Java 中对齐图形时遇到问题

Having trouble aligning graphics in Java

所以我正在使用 JFrame 对象打开 window 并先添加一堆图形我正在添加图像然后我试图添加一些行,但似乎行从上一张图片的 Y 中心 我希望它从页面顶部开始 这是我的 JFrame 代码:

    JFrame f = new JFrame();
    JLabel trebeclef = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("Some image")));
    Draw d = new Draw();
    f.add(trebeclef);
    f.add(d);   
    f.setSize(1000,1000);
    f.getContentPane().setBackground(Color.white);
    f.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
    f.pack();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

这是抽奖的代码class

public void paint(Graphics g2) {
        super.paintComponent(g2);
        Graphics2D g = (Graphics2D) g2;
        g.setStroke(new BasicStroke(2));
        g.drawLine(0, 0, 100, 0);
    }

结果是这样

感谢任何帮助,谢谢

setLayout之后添加。人们经常使用带有额外参数的重载 add 来实现布局的特定约束。

默认情况下,框架的 add 所在的内容窗格有一个 BorderLayout 居中、居左等等。

流中的水平“线”垂直居中。设置首选高度可确保填充线条组件。

d.setPreferredSize(new Dimension(100, 1000)); 

当然,其他布局可能会更好;取决于你想要什么 - 只是实验。

像这样尝试布局组件并不是一件容易的事情,尤其是当您考虑 JLabel 的复杂性和其他布局约束的可能性时。

如果你有图像并且你通过自定义绘画绘制线条,我会自定义绘制整个东西

开始于...

我们可以生产类似...

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public final class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new ClefWithLinesPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class ClefWithLinesPane extends JPanel {

        private BufferedImage trebbleClef;

        public ClefWithLinesPane() throws IOException {
            trebbleClef = ImageIO.read(getClass().getResource("/images/ClefLines.png"));
            setBackground(Color.WHITE);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = 0;
            int y = (getHeight() - trebbleClef.getHeight()) / 2;
            g2d.drawImage(trebbleClef, x, y, this);

            int[] lines = new int[] {
                30, 60, 89, 120, 149
            };

            x = trebbleClef.getWidth();

            g2d.setStroke(new BasicStroke(2));
            for (int line = 0; line < lines.length; line++) {
                y = lines[line];
                g2d.drawLine(x, y, getWidth(), y);
            }

            g2d.dispose();
        }

    }
}

但是...如您所见,这些线条并不“完全”匹配,现在这只是源图像的问题,您可以花一些时间清理它,或者,您可以直接分配用谱号本身的线条做你自己的那些,例如...

开始于...

我们可以制作类似...

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public final class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new ClefWithOutLinesPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class ClefWithOutLinesPane extends JPanel {

        private BufferedImage trebbleClef;

        public ClefWithOutLinesPane() throws IOException {
            trebbleClef = ImageIO.read(getClass().getResource("/images/Clef.png"));
            setBackground(Color.WHITE);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            int minLineY = 30;
            int maxLineY = 150;

            int lineSpacing = (maxLineY - minLineY) / 4;

            int x = 10;

            g2d.setStroke(new BasicStroke(8));
            g2d.drawLine(x, minLineY + 3, x, maxLineY - 3);
            int y = (getHeight() - trebbleClef.getHeight()) / 2;
            g2d.drawImage(trebbleClef, x + 10, y, this);

            g2d.setStroke(new BasicStroke(2));
            for (int line = 0; line < 5; line++) {
                y = minLineY + (lineSpacing * line);
                g2d.drawLine(x, y, getWidth(), y);
            }

            g2d.dispose();
        }

    }
}