You are not logged in.

#1 2017-07-16 13:52:46

PReP
Member
From: Sweden
Registered: 2010-06-13
Posts: 349
Website

(Java - SWT) Clearing Canvas/Drawing space?)

Hello Arch Forum-Peoples.

After putting it on hold forever i recently decided to give Java programming a go,
so im at the very beginning of knowing what the argh im doing.

. Currently i am trying to use the SWT library (outside of eclipse tho, prefer staying in vim for now, and using javac to compile)

After a few days of trying to google-fu this, i think im either not understanding all the "answers" i find, or researching the wrong thing,
Ive also a strong feeling its due to my current lack of flow-logics (im guilty of underexposure to object oriented programming, and i thought goto was a cool idea for too long.. (shameful face),
Like i might not logic-get where the drawing happens and where it doesnt here.

. My goal is simply to find out how the grrr to clear either the drawing surface/canvas, or the space the image i put on screen previously uses, somehow,
  but i fail to grasp how to make that happen (mebbe im in the wrong place?)

So im just hoping to get a nudge in the right direction so i can get back to google/reading manuals/trying things; cus atm i feel im out of ideas =/

. Here is the code:

import java.util.*;
import java.io.*;
//import java.nio.file.*;
//import java.nio.charset.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.events.*;

public class guistuff {

	public static void main(String[] args){



		Display GameScreen = new Display();
		Shell GameShell = new Shell(GameScreen);

		GameShell.setText("Ellis Game");

		GameShell.setSize(1920, 1080);

		GameShell.setLayout(new FillLayout());


		// Creating Canvas Thingie
		//
		Canvas GameCanvas = new Canvas(GameShell, SWT.NONE);
		final Image EllisImage = new Image(GameScreen, "grfx/ellis.png");


		// Creating Paint Handler for the Canvas Thingie

		GameCanvas.addPaintListener(new PaintListener() {


			public void paintControl(PaintEvent e) {
							
				int WalkTimes = 0;

				// Loop for redrawing image to a "new" y location each iteration

				while (WalkTimes < 1000) {

    				e.gc.drawImage(EllisImage, WalkTimes, 0);
				
				// My Non-Functional Possibly Stupid attempts to clear/redraw/erase the canvas after every draw
								
				GameShell.redraw();
				GameShell.update();
				
				//

				WalkTimes++;

				};
		

  			};
		});


		GameShell.open ();

		while (!GameShell.isDisposed ()) {
			if (!GameScreen.readAndDispatch ()) GameScreen.sleep ();
		}

		GameScreen.dispose ();





	}

}	

Thanks in advance for anyone finding time to help me out a bit smile

Last edited by PReP (2017-07-16 14:08:11)


. Main: Intel Core i5 6600k @ 4.4 Ghz, 16 GB DDR4 XMP, Gefore GTX 970 (Gainward Phantom) - Arch Linux 64-Bit
. Server: Intel Core i5 2500k @ 3.9 Ghz, 8 GB DDR2-XMP RAM @ 1600 Mhz, Geforce GTX 570 (Gainward Phantom) - Arch Linux 64-Bit
. Body: Estrogen @ 90%, Testestorone @ 10% (Not scientific just out-of-my-guesstimate-brain)

Offline

#2 2017-07-16 14:45:39

JohnBobSmith
Member
From: Canada
Registered: 2014-11-29
Posts: 725

Re: (Java - SWT) Clearing Canvas/Drawing space?)

I know from experience that any sort of GUI/Game development, at least in C++, is *extremely* difficult. If you are new to Java I recommend sticking to the console initially. I would mess with the printing text to the screen, reading/writing/processing files, getting and validating user input, maybe try making a command line tic tac toe or similar. These are all tasks that will help you develop GUI's/games later on. Master the basic syntax/language ideas before divining in to anything too difficult. smile

One minor style gripe, you have a *lot* of extra vertical whitespace. I like to reserve the meaning of my vertical whitespace to mean new code block starts/ends here. Maybe im being picky. Who knows.


I have been diagnosed with Bipolar Disorder. Therefore, I apologize in advance if the quality/content of my posts decline sharply, at any point.

Offline

#3 2017-07-23 22:19:03

RapidTransit
Member
Registered: 2013-04-27
Posts: 10

Re: (Java - SWT) Clearing Canvas/Drawing space?)

I wouldn't start with any GUI programming, I don't know SWT (I don't know if your listener is called in a different thread), I've used JavaFX. But before you really really start.

1. Read Sun's Style Guide (It's old but it applies here)

a. Install Intellij and Gradle, start a new project with Gradle

or

b. Install Intellij, Gradle and start a new Spring Boot Project (Comes with an embedded Tomcat Container so you can try a bit of web programming) I find reading other peoples code and learning the Spring Framework will start you in a good direction.

Edit: I'll stick this in IntelliJ and give it a whirl

Edit #2, SWT is archaic leftovers of IBM:

package com.help;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.LockSupport;


public class GuiStuff extends Application {

    private static final ExecutorService EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        StackPane root = new StackPane();
        ImageView image = new ImageView("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png");
        image.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                // Run your operations in a separate thread
                EXECUTOR_SERVICE.submit(()->{
                    int i = 0;
                    while (i < 1000) {
                        
                        // When Ever you modify the scene you want to run it on the 'main' Thread
                        Platform.runLater(()-> /** I don't know What You Want to Do but Do it Here **/);
                        i++;
                        // Prevent the type loop from, constantly spinning to fast this is helpful sometimes
                        LockSupport.parkNanos(1);
                    }
                });
            }
        });
        root.getChildren().addAll(image);
        primaryStage.setScene(new Scene(root, 1200, 600));
        primaryStage.show();
    }
}

Last edited by RapidTransit (2017-07-23 23:28:25)

Offline

Board footer

Powered by FluxBB