首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FLTK fl_line和fl_point不工作

FLTK fl_line和fl_point不工作
EN

Stack Overflow用户
提问于 2015-12-26 21:07:11
回答 1查看 641关注 0票数 2

我想根据计算结果在窗口上放置一个像素。计算是可行的,但是我想要生成的图形的任何部分都不会出现。编译器和链接器都没有发现错误,我也不知道为什么没有像素放在窗口上。因为我不知道问题出在哪里而没有缩小范围,所以我发布了整个代码:

代码语言:javascript
运行
复制
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Button.H>
#include <Fl/fl_Input.H>
#include <Fl/fl_Output.H>
#include <Physics.h>
#include <string.h>
#include <stdlib.h>
#include <Windows.h>

using namespace std;

#define WINSIZE 700
#define TIMESTEP 0.02f

class Graph : public Fl_Widget
{
public:
    Graph(int X, int Y, int W, int H) : Fl_Widget(X, Y, W, H) {}

    void draw(void) 
    {
        fl_color(FL_RED);
        fl_point(pos_x, (500 - pos_y));
    }

    int pos_x, pos_y;
};
//------------------------------------------------------------------------    

class WindowReq : public Fl_Window
{
public:
    WindowReq(int W, int H, const char* Title);

    Fl_Input*   InVel;
    Fl_Input*   InDeg;
    Fl_Output*  OutT;
    Fl_Output*  OutX;
    Fl_Output*  OutY;
    Fl_Button*  Calc;
    Graph*      graph;

private:
    static void calc_cb(Fl_Widget* o, void* v);
    inline void copyValueToX(char* c);
    inline void copyValueToY(char* c);
    inline void copyValueToT(char* c);
};

WindowReq::WindowReq(int W, int H, const char* Title) : Fl_Window(W, H, Title)
{
    begin();
        Calc = new Fl_Button((WINSIZE - 150), 50, 100, 30, "Calculate path");
        Calc->callback(&WindowReq::calc_cb, this);

        InVel = new Fl_Input(70, 50, 100, 30, "Velocity:");

        InDeg = new Fl_Input(((WINSIZE / 3) + 50), 50, 100, 30, "Angle:");

        OutX = new Fl_Output(70, 100, 100, 30, "Distance:");

        OutY = new Fl_Output(((WINSIZE / 3) + 50), 100, 100, 30, "Height:");

        OutT = new Fl_Output((WINSIZE - 150), 100, 100, 30, "Time:");

        graph = new Graph(0, 150, 700, 500);

        redraw();
    end();

    show();
}

void WindowReq::calc_cb(Fl_Widget* o, void* v)
{
    Projectile Stone;
    WindowReq* win = (WindowReq*)v;
    Coordinates2D Position = { 0,500 }, Buffer = Position;
    double velocity;
    double Degree;
    double Time;
    char Stuff[1000];

    velocity = atof(win->InVel->value());
    Degree = atof(win->InDeg->value());

    Stone.initialize(velocity, Degree, TIMESTEP);

    for (int i = 0; i < 700; i++)
    {
        fl_point(i, 400);
    }

    do
    {
        Position = Stone.getpos();
        Time = Stone.gettime();
        sprintf(Stuff, "%.2f", Time);
        win->copyValueToT(Stuff);
        sprintf(Stuff, "%d", Position.x);
        win->copyValueToX(Stuff);
        sprintf(Stuff, "%d", Position.y);
        win->copyValueToY(Stuff);
        Stone.simulatestep();
        win->graph->pos_x = Position.x;
        win->graph->pos_y = Position.y;
        win->graph->draw();
        Fl::check();
        Sleep(100);
    }while(Position.y >= 0);
}

void WindowReq::copyValueToX(char c[1000])
{
    OutX->value(c);
}

void WindowReq::copyValueToY(char c[1000])
{
    OutY->value(c);
}

void WindowReq::copyValueToT(char c[1000])
{
    OutT->value(c);
}

//============================================================
int main()
{
    WindowReq win(WINSIZE, WINSIZE, "Balistics Simulator");
    Fl::run();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-26 23:45:44

这个问题很可能与在WindowReq构造函数体中调用的回调函数有关。

代码语言:javascript
运行
复制
Calc->callback(&WindowReq::calc_cb, this);

尝试将其更改为:

代码语言:javascript
运行
复制
Calc->callback(calc_cb, this);

或者在动作函数calc_cb()中,在行中:

代码语言:javascript
运行
复制
win->graph->draw();

在上面的行之后,尝试添加:

代码语言:javascript
运行
复制
redraw();

并替换显式铸造:

代码语言:javascript
运行
复制
 WindowReq* win = (WindowReq*)v;

通过以下方式:

代码语言:javascript
运行
复制
 WindowReq* win = reinterpret_cast<WindowReq*>(v);

或者在函数draw()的定义中,在类Graph中。这些是与不显示fl_point()相关的位置,表示pos_xpos_y

附带说明:

您的代码中有一些C样式的行应该被它们的C++替代物替换,例如:

代码语言:javascript
运行
复制
 copyValueToX(char c[1000]) 

可以将其替换为std::string c并变为:

代码语言:javascript
运行
复制
 copyValueToX(string c) 

同样:

代码语言:javascript
运行
复制
 sprintf(Stuff, "%d", Position.x);

可能成为:

代码语言:javascript
运行
复制
std::cout << Position.x << std::endl;

最后,在成员函数中添加一些注释,以澄清它们中的语句和表达式。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34475248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档