processing - Having Object Oriented programming issues

后端 未结 2 758
予麋鹿
予麋鹿 2021-01-15 16:14

So basically, my aim it to create a program like the MS Paint software, where I can draw shapes with mouse dragging and change its colors. This is a very long script of mine

2条回答
  •  猫巷女王i
    2021-01-15 16:35

    It is hard to evaluate the correctness of your code without seeing the main method. Your 'main' class is set up in such a way that it appears the method mousePressed() is intended to poll the buttons and set the drawing mode based on their state.

    It is impossible for me to know if you are polling the buttons correctly, however, without viewing the main method.

    If you are looking to use an Object-Oriented approach, you are going to want to use the Observer pattern. In essence, your buttons will all have a reference to the 'main' object which has a buttonClicked(Button btn) method. When a button is clicked, it runs the 'main' object's buttonClicked(Button btn) method. The argument provided will be a reference to the button that is clicked so that the main can choose the appropriate mode to use. Demo code follows:

    In Main class:

    //Give the button a reference to the main object
    button1 = new button(this);
    
    //receive notifications from the button
    public buttonClicked(Button btn) {
        if(btn.equals(button1))
            mode = 1;
        if(...)
            ...
    }
    

提交回复
热议问题