【问题标题】:BufferedImage only showing on 1/3 of JPanelBufferedImage 仅显示在 JPanel 的 1/3
【发布时间】:2013-12-07 19:01:33
【问题描述】:

这是我原来的question,请回答。高度现在设置为我认为正确的尺寸。但我看不到面板底部的 2/3。

我已经阅读、询问、沉思和实验,但我仍然找不到答案。我不需要代码,只需要一点帮助。

我的 JFrame 类;

public Frame(String title) throws FileNotFoundException {

        super(String.format("Title", title));
        this.panel = new Panel();
        this.panel.drawLinesAndTab();
        this.panel.setSize(this.panel.getPreferredSize());
        this.panel.validate();
        this.scroller = new JScrollPane(this.panel);
        //this.scroller.setPreferredSize(new Dimension(this.panel.getPreferredSize()));
        this.scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        //this.scroller.setSize(new Dimension(this.panel.getPreferredSize()));
        this.scroller.getVerticalScrollBar().setUnitIncrement(20);
        this.getContentPane().add(this.scroller);
        //this.pack();
    }

这是 JPanel 类。我知道它很庞大,并且我确实计划重新编写此代码,但我的时间有限,必须尝试至少看到所有输出。

 public Panel() throws FileNotFoundException {

        this.tab = new ReadTabFile("tabSource.txt");
        this.image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
        Graphics g = this.image.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, this.image.getWidth(), this.image.getHeight());
        setBorder(BorderFactory.createLineBorder(Color.black));
        this.setFocusable(true);

    }

    public void drawLinesAndTab() {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        this.list = tab.readTabCode();
        this.a = 20;
        this.b = 100;
        this.c = 60;
        this.x = 40;
        this.y = 100;
        this.beginBarlineX = 20;
        this.beginBarlineY = 100;
        this.endBarY = 980;
        this.endBarY = 100;
        this.title = tab.getTitle();
        g.drawString(this.title, 40, 20);

        for (int i = 0; i < this.list.size(); i++) {
            Bar theBar = (Bar) this.list.get(i);
            drawBarline(a, b, a, b + 125);
            ArrayList<String> stuff = theBar.getLinesInBar();

            for (int j = 0; j < stuff.size(); j++) {
                String line = stuff.get(j);
                theFlag = line.substring(0, 1);
                theNotes = line.substring(1, line.length());
                if (newLine = true) {
                }
                try {

                    System.out.println(theNotes);

                    if (c <= (width - 40)) {

                        newLine = false;
                        String zero = theFlag;
                        drawFlag(zero, x + 5, y - 20);

                        String one = theNotes.substring(0, 1);
                        g.drawLine(a, b, c, b);
                        drawLetter(one, x, y);

                        String two = theNotes.substring(1, 2);
                        drawLetter(two, x, y += 25);
                        g.drawLine(a, b += 25, c, b);

                        String three = theNotes.substring(2, 3);
                        drawLetter(three, x, y += 25);
                        g.drawLine(a, b += 25, c, b);

                        String four = theNotes.substring(3, 4);
                        drawLetter(four, x, y += 25);
                        g.drawLine(a, b += 25, c, b);

                        String five = theNotes.substring(4, 5);
                        drawLetter(five, x, y += 25);
                        g.drawLine(a, b += 25, c, b);

                        String six = theNotes.substring(5, 6);
                        drawLetter(six, x, y += 25);
                        g.drawLine(a, b += 25, c, b);
                        this.repaint();

                        b -= 125;
                        y -= 125;
                        x += 40;
                        a += 40;
                        c += 40;

                    } else {
                        if (height < (b - 100)) {
                            height += 205;

                        }
                        newLine = true;
                        a = 20;
                        x = 20;
                        b += 225;
                        c = 60;
                        y += 225;
                        beginBarlineX = 20;
                        beginBarlineY += 100;
                        endBarX += 100;
                        endBarY = 100;
                        this.repaint();
                    }

                } catch (Exception ex) {
                    System.err.println(ex + " within if drawtab/line for loop");
                }
            }
        }
    }

    public void drawBarline(int xTop, int yTop, int xBot, int yBot) {
        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        g.drawLine(xTop, yTop, xBot, yBot);

    }

    public Point makeBarline(int xTop, int yTop, int xBot, int yBot) {
        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        g.drawLine(xTop, yTop, xBot, yBot);
        return (new Point());
    }

    public Point drawLetter(String letter, int x, int y) throws FontFormatException, IOException {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        g.setFont(letterFont(letter).deriveFont(20.0f));
        g.drawString(letter, x, y);

        return (new Point());
    }

    public Point drawFlag(String letter, int x, int y) throws FontFormatException, IOException {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);
        g.setFont(flagFont(letter).deriveFont(30.0f));
        g.drawString(letter, x, y);

        return (new Point());
    }

    public Font letterFont(String fontString) throws FontFormatException, IOException {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);

        if (!Character.isDigit(fontString.charAt(0))) {
            this.letterFont = Font.createFont(Font.TRUETYPE_FONT, new File("LeRoy.ttf"));
            g.getFontMetrics(this.letterFont);
            g.setFont(this.letterFont);
            return this.letterFont;
        } else {
            return null;
        }
    }

    public Font flagFont(String fontString) throws FontFormatException, IOException {

        Graphics g = this.image.getGraphics();
        g.setColor(Color.black);

        if (!Character.isDigit(fontString.charAt(0))) {
            this.flagFont = Font.createFont(Font.TRUETYPE_FONT, new File("LeroyLuteNotes1.ttf"));
            g.getFontMetrics(this.flagFont);
            g.setFont(this.flagFont);
            return this.flagFont;
        } else {
            return null;
        }

    }

    @Override
    public Dimension getPreferredSize() {
        return (new Dimension(this.width, this.height));
    }

    public BufferedImage getImage() {
        return this.image;
    }

    @Override
    public void paintComponent(Graphics graphics) {

        super.paintComponent(graphics);
        Graphics g = graphics.create();
        g.drawImage(this.image, 0, 0, null);
    }
}

【问题讨论】:

  • 我在面板类的工作过程中增加了高度,我不是 100% 确定我能做到这一点,但它似乎只工作了一半。
  • 抱歉,您需要很多帮助。这与 Swing 的编码方式完全不同。阅读这篇Hangman Swing GUI 文章,了解如何构建 Swing GUI。

标签: java swing jpanel bufferedimage


【解决方案1】:

我不确定您试图用该代码做什么。看起来您可能正在尝试在图像上进行一些自定义绘画。如果是这样,那么我有以下建议:

public Dimension getPreferredSize() {
    return (new Dimension(this.width, this.height));
}

这毫无意义,您是说首选尺寸等于组件的实际尺寸。首选尺寸应该是图片的尺寸。

您的自定义绘画代码完全错误。所有自定义绘画代码都应该通过 paintComponent() 方法完成。因此,首先您要将图像绘制为组件的背景。然后,您将调用其他绘画方法在图像上绘画东西。您可以将 Graphics 对象从 paintComponent() 方法传递给所有其他绘画方法,而不是使用 image.getGraphics()。

Custom Painting 上的 Swing 教程中的一个简单自定义绘画示例开始。一旦你学习了基础知识,你就可以一步一步地定制你的代码。那就是先画背景。确保大小正确并且滚动有效。然后进行下一步并添加另一种方法在图像上绘制其他内容。

我不明白为什么人们在编写数百行代码的过程中没有进行基本测试以确保代码正常工作。

【讨论】:

  • 我一直在测试代码,程序运行正常。它从一开始就起作用。 “这没有意义,你是说首选大小等于组件的实际大小。首选大小应该是图像的大小。” this.width 和 this.height 在构造函数中设置为图像的大小,并在整个 drawLinesAndTab() 方法中更改。我不知道这是否可能。现在阅读了自定义绘画教程后,我意识到我的系统应该设置不同,但我现在没有时间更改它。
  • 所以没有办法显示整个图像?
  • I do not have the time to change it now. - 这两天你一直有问题。也许如果你以正确的方式去做,问题就会消失。 there is no way to display the whole image? - 我已经告诉过你该怎么做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-30
相关资源
最近更新 更多