Codename One:2D绘图应用程序

时间:2017-02-26 08:28:26

标签: codenameone

我想知道,我在哪里使用GUI Builder(旧版)初始化我的DrawingCanvas类(在2d绘图应用程序中引用)。

另外,如何更改引用GUI Builder的每个按钮的笔触颜色?

package userclasses;
import com.codename1.ui.Component;
import com.codename1.ui.Form;
import com.codename1.ui.events.ActionEvent;
import generated.StateMachineBase;
import com.codename1.ui.util.Resources;
/**
 *
 * @author Your name here
 */

public class StateMachine extends StateMachineBase{
    public StateMachine(String resFile) {
        super(resFile);
        // do not modify, write code in initVars and initialize class members there,
        // the constructor might be invoked too late due to race conditions that might occur
    }

    /**
     * this method should be used to initialize variables instead of
     * the constructor/class scope to avoid race conditions
     */
    @Override
    protected void initVars(Resources res) {
    }
}

1 个答案:

答案 0 :(得分:0)

该课程的来源位于graphics section of the Codename One developer guide,粘贴在下方:

  

应用程序的中心是DrawingCanvas类,它扩展了Component。

public class DrawingCanvas extends Component {
    GeneralPath p = new GeneralPath();
    int strokeColor = 0x0000ff;
    int strokeWidth = 10;

    public void addPoint(float x, float y){
        // To be written
    }

    @Override
    protected void paintBackground(Graphics g) {
        super.paintBackground(g);
            Stroke stroke = new Stroke(
                strokeWidth,
                Stroke.CAP_BUTT,
                Stroke.JOIN_ROUND, 1f
            );
            g.setColor(strokeColor);

            // Draw the shape
            g.drawShape(p, stroke);

    }

    @Override
    public void pointerPressed(int x, int y) {
        addPoint(x-getParent().getAbsoluteX(), y-getParent().getAbsoluteY());
    }
}

请注意,这是基础骨架,稍后将在该章中添加更多代码...

相关问题