簡體   English   中英

在 Java 中對齊圖形時遇到問題

[英]Having trouble aligning graphics in Java

所以我正在使用 JFrame object 打開 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);

這是 Draw class 的代碼

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

它導致這個

在此處輸入圖像描述

任何幫助表示贊賞,謝謝

嘗試像這樣布局組件並不是最容易的事情,尤其是當您考慮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();
        }

    }
}

但是...如您所見,線條並不“完全”匹配,現在這只是源圖像的問題,您可以花一些時間清理它,或者,您可以省去clef 本身並做你自己的那些,例如......

從...開始...

在此處輸入圖像描述

我們可以生產類似...

在此處輸入圖像描述

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

    }
}

setLayout之后添加。 人們經常使用帶有額外參數的重載add來實現布局的特定約束。

默認情況下,框架的add所在的內容窗格具有帶中心、左側等的BorderLayout

流中的水平“線”垂直居中。 設置首選高度可確保填充線條組件。

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

當然,其他布局可能會更好; 取決於你想要什么 - 只是實驗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM