• JUCE框架教程(3)—— Component Class&Graphics入门


    JUCE框架教程(3)—— Component Class

    前言
    Graphic文档: https://docs.juce.com/develop/classGraphics.html
    Component文档: https://docs.juce.com/develop/classComponent.html

    首先,我们创建一个GUI项目取名为sample,并通过projucer的加号按钮添加一个名为Blue的组件
    在这里插入图片描述
    在MainComponent.h里添加blue

    private:
    	Blue blue;
    
    • 1
    • 2

    MainComponent.cpp文件里,可以对我们的UI界面进行更改。

    MainComponent::MainComponent()
    {
        addAndMakeVisible(blue);
        setSize (400, 300);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    MainComponent这个函数里,我们使用addAndMakeVisible()注册了blue组件,使用setSize()确定了页面的宽度和长度。

    void MainComponent::paint (juce::Graphics& g)
    {
        //the backgroundColor
        g.fillAll (juce::Colours::pink);
    
        g.setFont (juce::Font (32.0f));
        //g.fillEllipse(getWidth() / 2 - 100, getHeight() / 2 - 100, 200, 200);
        g.setColour (juce::Colours::black);
        g.drawText ("AirGain", getLocalBounds(), juce::Justification::centred, true);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ptaint这个函数里,我们定义了一个名为g的Graphics类。
    通过fillAll 改变背景颜色,setFont修改字体大小,setColour设置字体颜色,drawText写字。

    //define child components
    void MainComponent::resized()
    {
        blue.setBounds(0, 0, getWidth(), getHeight()/3);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    resized这个函数中,可以用来定义子组件(我们之前注册的Blue组件)。通过setBounds来确定这个子组件在页面中显示的位置。

    此时的Blue.cpp:

    #include 
    #include "Blue.h"
    
    //==============================================================================
    Blue::Blue()
    {
        // In your constructor, you should add any child components, and
        // initialise any special settings that your component needs.
    
    }
    
    Blue::~Blue()
    {
    }
    
    void Blue::paint (juce::Graphics& g)
    {
        g.fillAll(juce::Colours::skyblue);
    }
    
    void Blue::resized()
    {
      
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    我们单纯地把Blue组件的背景色设置成了蓝色。

    最终,编译项目,得到以下的页面:
    在这里插入图片描述
    以上是JUCE组件和Graphics的基本操作,更多操作请查看JUCE官方文档

  • 相关阅读:
    【Python入门指北】 发邮件与正则表达式
    Sophos Firewall OS (SFOS) 19.0 GA
    Rust权威指南 全书笔记
    Nginx安全加固
    低代码在物品领用领域数字化转型的案例分析
    Logback 日志配置
    【web前端开发】HTML知识点超详细总结
    (附源码)计算机毕业设计SSM基于渐进式网页应用的大众社交软件
    .gitignore不生效的解决方案
    mybatis批量插入
  • 原文地址:https://blog.csdn.net/m0_56699208/article/details/126531239