Java 画家的古怪行为

Java 画家的古怪行为,java,codenameone,Java,Codenameone,我试图通过使用背景画师的代码来定制一些UI,但我遇到了奇怪和意外的结果: public class TabView extends Form { private Tabs tabs; public TabView() { super("Radio Web", new BorderLayout()); tabs = new Tabs(Component.BOTTOM); addComponent(BorderLayout.CENT

我试图通过使用背景画师的代码来定制一些UI,但我遇到了奇怪和意外的结果:

public class TabView extends Form {

    private Tabs tabs;

    public TabView() {
        super("Radio Web", new BorderLayout());
        tabs = new Tabs(Component.BOTTOM);
        addComponent(BorderLayout.CENTER, tabs);
        tabs.setSwipeActivated(false);

        Component component = null;

        Button playButton = new  Button(FontImage.createMaterial(FontImage.MATERIAL_PLAY_ARROW,     UIManager.getInstance().getComponentStyle("Title"), 10));

        playButton.getAllStyles().setBorder(RoundBorder.create().color(0x94170C));
        playButton.getAllStyles().setMarginBottom(0);
        playButton.getAllStyles().setMarginLeft(0);
        playButton.getAllStyles().setMarginTop(0);
        Label mediaInfoLabel = new Label("Artist - Title");
        mediaInfoLabel.getAllStyles().setPaddingTop(2);
        mediaInfoLabel.getAllStyles().setPaddingBottom(2);
        Slider volumeSlider = new Slider();
        volumeSlider.setMinValue(0);
        volumeSlider.setMaxValue(100);
        volumeSlider.setEditable(true);
        Container box = GridLayout.encloseIn(1, new Container(), volumeSlider, mediaInfoLabel);

        Container south = new Container(new FlowLayout(Component.LEFT, Component.BOTTOM));

        south.getAllStyles().setBgPainter(new BackgroundPainter(south) {
            @Override
            public void paint(Graphics g, Rectangle rect) {
                int color = g.getColor();
                int alpha = g.getAlpha();

                g.setColor(0xD12115);
                g.setAlpha(255);
                int y = rect.getHeight() / 2;
                g.fillRect(0, y, rect.getWidth(), rect.getHeight() - y);
                g.setColor(color);
                g.setAlpha(alpha);
            }
        });
        south.add(playButton).add(box);

        component = BorderLayout.south(south);
        component.getAllStyles().setBgPainter(new BackgroundPainter(component) {
            @Override
            public void paint(Graphics g, Rectangle rect) {
                int color = g.getColor();
                int alpha = g.getAlpha();

                g.setColor(0x150100);
                g.setAlpha(255);
                g.fillRect(0, 0, rect.getWidth(), rect.getHeight());
                g.setColor(color);
                g.setAlpha(alpha);
            }
        });
        tabs.addTab("", FontImage.MATERIAL_AUDIOTRACK, 5, component);

        BrowserComponent sito = new BrowserComponent();
        sito.setURL("https://www.codenameone.com");
        tabs.addTab("", FontImage.MATERIAL_HTTP, 5, sito);

        BrowserComponent fb = new BrowserComponent();
        fb.setURL("https://www.facebook.com");
    tabs.addTab("", FontImage.MATERIAL_FACE, 5, fb);
    }

}
因此,我可以看到“组件”绘制了自己的黑色背景,而“南部”则没有(应该是半红色)。我做错了吗?此外,GridLayout没有正确处理边距/填充,我必须增加标签填充以避免被剪切


谢谢。

我以前也遇到过类似的问题,所以我不得不放弃背景画,因为我画了两种颜色

从您发布的代码中,可以通过以下操作实现类似的风格:

只要复制并粘贴它,它就会工作,您也可以替换
组件上的BP来使用添加的方法

public class TabView extends Form {

    private Tabs tabs;

    public TabView() {
        super("Radio Web", new BorderLayout());
        tabs = new Tabs(Component.BOTTOM);
        addComponent(BorderLayout.CENTER, tabs);
        tabs.setSwipeActivated(false);

        Component component = null;

        Button playButton = new  Button(FontImage.createMaterial(FontImage.MATERIAL_PLAY_ARROW,     UIManager.getInstance().getComponentStyle("Title"), 10));

        playButton.getAllStyles().setBorder(RoundBorder.create().color(0x94170C));
        playButton.getAllStyles().setMarginBottom(0);
        playButton.getAllStyles().setMarginLeft(0);
        playButton.getAllStyles().setMarginTop(0);
        Label mediaInfoLabel = new Label("Artist - Title");
        mediaInfoLabel.getAllStyles().setPaddingTop(2);
        mediaInfoLabel.getAllStyles().setPaddingBottom(2);
        Slider volumeSlider = new Slider();
        volumeSlider.setMinValue(0);
        volumeSlider.setMaxValue(100);
        volumeSlider.setEditable(true);
        Container box = GridLayout.encloseIn(1, new Container(), volumeSlider, mediaInfoLabel);

        Container south = new Container(new FlowLayout(Component.LEFT, Component.BOTTOM));


        south.add(playButton).add(box);

        paintBackgroundRect2Colors(south);

        component = BorderLayout.south(south);
        component.getAllStyles().setBgPainter(new BackgroundPainter(component) {
            @Override
            public void paint(Graphics g, Rectangle rect) {
                int color = g.getColor();
                int alpha = g.getAlpha();

                g.setColor(0x150100);
                g.setAlpha(255);
                g.fillRect(0, 0, rect.getWidth(), rect.getHeight());
                g.setColor(color);
                g.setAlpha(alpha);
            }
        });
        tabs.addTab("", FontImage.MATERIAL_AUDIOTRACK, 5, component);

        BrowserComponent sito = new BrowserComponent();
        sito.setURL("https://www.codenameone.com");
        tabs.addTab("", FontImage.MATERIAL_HTTP, 5, sito);

        BrowserComponent fb = new BrowserComponent();
        fb.setURL("https://www.facebook.com");
    tabs.addTab("", FontImage.MATERIAL_FACE, 5, fb);
    }

    public static void paintBackgroundRect2Colors(Component cmp) {
        Image i = Image.createImage(cmp.getPreferredW(), cmp.getPreferredH(), 0xffffff);
        Graphics g = i.getGraphics();
        g.setAntiAliased(true);
        boolean antiAliased = g.isAntiAliased();
        int y = i.getHeight() / 2;

        // Top half
        g.setAlpha(255); //Change this to 0 if you want top half to be transparent
        g.setColor(0x150100); // Change this to any color you want, I used "component" color just to blend the look and feel
        g.fillRect(0, 0, i.getWidth(), y);

        // Bottom half
        g.setAlpha(255);
        g.setColor(0xD12115);
        g.fillRect(0, y, i.getWidth(), i.getHeight());

        g.setAntiAliased(antiAliased);

        // Set background image and make sure it fills 
        cmp.getAllStyles().setBgImage(i, true);
        cmp.getAllStyles().setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL, true);
    }

}

感谢@Diamond的快速回复和建议的解决方案。我想我会尝试一下,但问题仍然存在,看到有些地方出了问题,但你不知道是什么让人沮丧。我花了很多时间试图去理解,我不想让它发生在别人身上。你现在遇到的问题是什么?代码在我这边运行良好,给我半个红色。请看添加的屏幕截图。对不起,我想说的是,画家的行为仍然很奇怪,您提出的解决方案是可以的!我做了很多代码,我试过枪生成器,但我没有发现任何感觉,所以如果我能避免它。我来看看你的图书馆。再次感谢。接受答案+1。仅供参考,画师中的问题是因为您从0开始绘制,而不是从矩形的X/Y开始绘制。谢谢Shai,这就是解决方案!