Color change by area in swt?

感情迁移 提交于 2021-01-29 20:01:33

问题


sorry I use a translator first, so please understand

enter image description here

What I want is that there is a swt program like that and I want to make a program that moves once a second and finishes up to 30m

I implemented it until it moved, but I want to change the color of the background as much as it moved, but I don't know what to do.

I want to separate the moved part and the remaining part like this

Can you tell me how to change the color of the pasted area like this?

Examples are good, explanations are good in code, or hints are good.

my code

 import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.io.File;
import java.util.Timer;


import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;

public class MainWindow implements Runnable {

    /**
     * Launch the application.
     * 
     * @param args
     */

    public void init() {
        Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setSize(720, 500);
        shell.setText("SWT Application");
        shell.setLayout(new FormLayout());
        
        Composite composite = new Composite(shell, SWT.NONE);
        FormData fd_composite = new FormData();
        fd_composite.bottom = new FormAttachment(0, 106);
        fd_composite.right = new FormAttachment(0, 694);
        fd_composite.top = new FormAttachment(0, 10);
        fd_composite.left = new FormAttachment(0, 41);
        composite.setLayoutData(fd_composite);

        org.eclipse.swt.graphics.Color blue = display.getSystemColor(SWT.COLOR_BLUE);
        org.eclipse.swt.graphics.Color green = display.getSystemColor(SWT.COLOR_GREEN);
        Composite realTimeComposite = new Composite(shell, SWT.NONE);
        FormData fd_realTimeComposite = new FormData(GridData.FILL,GridData.FILL);
        fd_realTimeComposite.right = new FormAttachment(composite, 0, SWT.RIGHT);
        fd_realTimeComposite.top = new FormAttachment(0, 121);
        fd_realTimeComposite.left = new FormAttachment(0, 41);
        
        realTimeComposite.setLayoutData(fd_realTimeComposite);
        realTimeComposite.setBackground(blue);
        
        realTimeComposite.setLayout(new org.eclipse.swt.layout.GridLayout(1, false));
        
        new Label(realTimeComposite, SWT.NONE);
        new Label(realTimeComposite, SWT.NONE);
        
        
        Label lblNewLabel = new Label(realTimeComposite, SWT.NONE);
        lblNewLabel.setBounds(10, 64, 142, 156); 
        lblNewLabel.setImage(new Image(display, "C:\\test.png"));
        
        final Runnable run = new Runnable(){
            private int counter = 0;
            @Override
            public void run(){
                
                    lblNewLabel.setLocation(lblNewLabel.getLocation().x+1, lblNewLabel.getLocation().y);
                    Point tempsize = realTimeComposite.getSize();
                    
                    Integer axisX = realTimeComposite.getSize().x - lblNewLabel.getLocation().x ;
                    System.out.print(axisX);
    
                    display.timerExec(1000, this);
                    //TODO : realTimeComposite 를 분할 하여야 함
                    //realTimeComposite.setBackground(green); };
                }
            };

        display.timerExec(1000, run);
        

        Label degree0 = new Label(shell, SWT.NONE);
        FormData fd_degree0 = new FormData();
        fd_degree0.right = new FormAttachment(0, 35);
        fd_degree0.top = new FormAttachment(0, 122);
        fd_degree0.left = new FormAttachment(0);
        degree0.setLayoutData(fd_degree0);
        degree0.setText("  0   -");

        Label degree360 = new Label(shell, SWT.NONE);
        fd_realTimeComposite.bottom = new FormAttachment(degree360, 0, SWT.BOTTOM);
        FormData fd_degree360 = new FormData();
        fd_degree360.right = new FormAttachment(0, 35);
        fd_degree360.top = new FormAttachment(0, 402);
        fd_degree360.left = new FormAttachment(0);
        degree360.setLayoutData(fd_degree360);
        degree360.setText("360 -");

        Label startDistance = new Label(shell, SWT.NONE);
        FormData fd_startDistance = new FormData();
        fd_startDistance.right = new FormAttachment(0, 130);
        fd_startDistance.top = new FormAttachment(0, 423);
        fd_startDistance.left = new FormAttachment(0, 43);
        startDistance.setLayoutData(fd_startDistance);
        startDistance.setText("0 m");

        Label previousDistance = new Label(shell, SWT.NONE);
        FormData fd_previousDistance = new FormData();
        fd_previousDistance.top = new FormAttachment(startDistance, 0, SWT.TOP);
        fd_previousDistance.right = new FormAttachment(composite, 0, SWT.RIGHT);
        fd_previousDistance.left = new FormAttachment(0, 659);
        previousDistance.setLayoutData(fd_previousDistance);
        previousDistance.setText("30m");
        
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }



    public static void main(String[] args) {
        MainWindow a = new MainWindow();
        a.init();
        
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        Display.getDefault().timerExec(1000, this);
    }
}

来源:https://stackoverflow.com/questions/65193186/color-change-by-area-in-swt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!