我的JscrollPane不能(垂直)滚动整个图像, [英] My JscrollPane isn't scrolling (vertically) the entire image,

查看:148
本文介绍了我的JscrollPane不能(垂直)滚动整个图像,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我增加preferredSize时,会发现更多的Bufferedimage(在JPanel上,在JFrame上).

when I increase the preferredSize, more of the Bufferedimage(on a JPanel, on a JFrame) is found.

图像的大小将根据我的程序正在读取的文件的大小而变化,

The size of the image is going to vary depending on the size of the file my program is reading,

在其中设置滚动窗格的地方;

where I set the scrollpane;

public Frame(String title, int theWidth, int theHeight) throws FileNotFoundException {

        super(String.format("Title", title));
        this.width = theWidth;
        this.height = theHeight;
        this.panel = new Panel(width, height);
        this.scroller = new JScrollPane(this.panel);
        this.panel.drawLinesAndTab();
        this.scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.scroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.scroller.setPreferredSize(new Dimension(this.width, this.height));
        this.getContentPane().add(this.scroller);
    }

(别开枪),这是我的Panel课;

And (don't shoot me), this is my Panel class;

public class Panel extends JPanel {

    private BufferedImage image;
    private ReadTabFile tab;
    private String title;
    private String theNotes;
    private String theFlag;
    private int a, b, c, x, y, beginBarlineX, beginBarlineY, endBarX, endBarY;
    private ArrayList list;
    private Font letterFont;
    private Font flagFont;
    private int width;
    private int height;
    boolean newLine = false;

    public Panel(int theWidth, int theHeight) throws FileNotFoundException {

        this.width = theWidth;
        this.height = theHeight;
        this.tab = new ReadTabFile("tabSource.txt");
        this.image = new BufferedImage(width, 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){
                    //make some bar lines.
                }

                try {

                    int w = this.image.getWidth();
                    //int h = this.image.getHeight();

                    if (c <= (w - 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 {
                        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 Panel, drawtab 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.image.getWidth(), this.image.getHeight()));
    }

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

    @Override
    public void paintComponent(Graphics graphics) {

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

我想我已经正确设置了它,我没有使用布局管理器,因为我所做的基本上就是在图像/面板上写行和文本.它工作正常,但我需要能够向下滚动到图像底部.

I think I've set it up correctly, I've not used a layout manager because all I'm doing is basically writing lines and text to the image/panel. It is working fine, but I need to be able to scroll right down to the bottom of the image.

非常感谢您的帮助.

推荐答案

  1. JScrollPane遵循组件的首选大小来设置其内容视图,而不是组件的内容.
  2. 如果您要相对于content:image滚动,请覆盖Panel类的getPreferreedSize(Dimension)函数,并返回图像大小作为组件的首选大小.
  3. 我们应该始终在已覆盖的paintComponent(Graphics)函数中调用super.paintComponent(g),而现在您不需要这样做.
  1. JScrollPane respects component preferred size to set it's content view, not the component's content.
  2. If you wish to scroll with respect to content: image, Override getPreferreedSize(Dimension) function of the Panel class and return the image size as the component's preferred size.
  3. We should always call super.paintComponent(g) inside the overriden paintComponent(Graphics) function, which you are not doing for now.

这篇关于我的JscrollPane不能(垂直)滚动整个图像,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆