11 Replies - 442 Views - Last Post: 18 May 2014 - 11:29 PM Rate Topic: -----

#1 jabbeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 13-January 14

JavaFX Graphics Context

Posted 16 May 2014 - 06:59 AM

Hello. I am doing a Timeline project and i have bumped into a small problem. I have a class that creates the timeline it self called : public void drawTimeline(GraphicsContext gc){

That graphicscontext is the one i send in to the method in my main method.
The problem is now, how can i call my drawEventPos(GraphicsContext gc) method in my Controllerclass for the timeline stage? Now i just want to test if a buttonpress can generate a new event on the timeline, because i have that method that handles the buttonAction in the controller class, i need to call the eventPos method if im not wrong, seems logic to me.
The graphicscontext i use for the timeline is the one i create in the main method, so i cant use that one in the controller class. :)/> Some tips please. I dont really understand either how to store graphiccontext as a refrence, so if you say i can do that, please tell me how :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: JavaFX Graphics Context

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: JavaFX Graphics Context

Posted 16 May 2014 - 07:19 PM

Nothing stopping you from creating a getter that will return the canvas to the controller from the view. In the controller class you can then call:

Canvas canvas = view.getCanvas();
GraphicsContext gc = canvas.getGraphicsContext2D();


This post has been edited by farrell2k: 16 May 2014 - 07:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 jabbeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 13-January 14

Re: JavaFX Graphics Context

Posted 17 May 2014 - 03:22 PM

View Postfarrell2k, on 16 May 2014 - 07:19 PM, said:

Nothing stopping you from creating a getter that will return the canvas to the controller from the view. In the controller class you can then call:

Canvas canvas = view.getCanvas();
GraphicsContext gc = canvas.getGraphicsContext2D();



Hm. So you mean in my main class i create a i.e getCanvas(){ method. So i can do this from different controllers not only just one? , but in the main method. i return what? getCanvas(){ return canvas; or Canvas canvas = new Canvas() ?
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1379
  • View blog
  • Posts: 3,039
  • Joined: 05-April 11

Re: JavaFX Graphics Context

Posted 17 May 2014 - 05:23 PM

I don't quite understand your question. Could you please try and explain it again?
I am having a hard time using my imagination when it comes to the structure of your classes
Was This Post Helpful? 0
  • +
  • -

#5 jabbeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 13-January 14

Re: JavaFX Graphics Context

Posted 18 May 2014 - 04:49 AM

View PostCasiOo, on 17 May 2014 - 05:23 PM, said:

I don't quite understand your question. Could you please try and explain it again?
I am having a hard time using my imagination when it comes to the structure of your classes


Sure. Okey, will provide these small code fragments.
My main timeline class:

Canvas canvas = getCanvas();;
Group root = new Group(par);

/*
* Draws the graphics on the canvas/timeline
*/
dc.drawTimeline(graphics, d.startDate(), d.endDate());		
e.drawNewEvent(graphics, 780, "SCHTARWARZ");


For the moment i call to draw a new event using the drawNewEvent().
In this class i have the GraphicsContext.

NewEventController class

public void handleCreateBtn(ActionEvent e){
    	
c.drawNewEvent(gc, 400, "JAKOB");
}


So what i want to do is to basically call my drawNewEvent() in that handleCreateButton.
But its not possible at the moment, because i dont have a graphicscontext in this perticual class.


So basically what i should do is in main class:
getCanvas(){
Canvas can = new Canvas();
return canvas;



Then in controller class:
GraphicsCOntext g = main.getCanvas();
......
......
......
e.drawNewEvent(g, 500,"Event");

Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1379
  • View blog
  • Posts: 3,039
  • Joined: 05-April 11

Re: JavaFX Graphics Context

Posted 18 May 2014 - 08:57 AM

Low coupling and high cohesion

I see no reason why the controller should even know about the GraphicsContext
In my head the controller should delegate this task out to the timeline view, which will know how to draw the event
My JavaFX experience is very low, so I hope I've got everything right
Java 8 is being used in the follow examples

The controller lets the view do all the drawing
Let the controller decide what should happen on button clicks. Nothing interesting happening at the moment :)
public class TimelineController {
	private ITimelineView timelineView;
	
	
	public TimelineController(ITimelineView timelineView, LocalDate startDate, LocalDate endDate) {
		this.timelineView = timelineView;
		initTimelineView(startDate, endDate);
	}
	
	private void initTimelineView(LocalDate startDate, LocalDate endDate) {
		timelineView.setCreateClickHandler(this::onCreateClick);
		timelineView.drawTimeline(startDate, endDate);
		addTimelineEvent(new TimelineEvent(780, "SCHTARWARZ"));
	}
	
	private void addTimelineEvent(TimelineEvent event) {
		timelineView.drawNewEvent(event);
	}
	
	private void onCreateClick(ActionEvent e) {
		addTimelineEvent(new TimelineEvent(400, "JAKOB"));
	}
}



The view knows about the canvas, and can therefore get the GraphicsContext
It makes sense that the view will be drawing the timeline
I will also encourage you to add a TimelineEvent model class. A TimelineEventView would maybe make sense too, but that is up to you
public class TimelineView implements ITimelineView {
	private Button createBtn;
	private Canvas canvas;
	
	//TODO Init view ...
	
	@Override
	public void drawTimeline(LocalDate startDate, LocalDate endDate) {
		//TODO
	}
	
	@Override
	public void setCreateClickHandler(EventHandler<ActionEvent> handler) {
		createBtn.setOnAction(handler);
	}
	
	@Override
	public void drawNewEvent(TimelineEvent event) {
		GraphicsContext gc = canvas.getGraphicsContext2D();
		drawTimelineEvent(gc, event);
	}
	
	private void drawTimelineEvent(GraphicsContext gc, TimelineEvent event) {
		//...
	}
}



Low coupling and high cohesion

The above example is a good start for a MVC or MVP application
Lookup those design patterns if you do not already know about them
Was This Post Helpful? 1
  • +
  • -

#7 jabbeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 13-January 14

Re: JavaFX Graphics Context

Posted 18 May 2014 - 09:50 AM

View PostCasiOo, on 18 May 2014 - 08:57 AM, said:

Low coupling and high cohesion

I see no reason why the controller should even know about the GraphicsContext
In my head the controller should delegate this task out to the timeline view, which will know how to draw the event
My JavaFX experience is very low, so I hope I've got everything right
Java 8 is being used in the follow examples

The controller lets the view do all the drawing
Let the controller decide what should happen on button clicks. Nothing interesting happening at the moment :)/>/>
public class TimelineController {
	private ITimelineView timelineView;
	
	
	public TimelineController(ITimelineView timelineView, LocalDate startDate, LocalDate endDate) {
		this.timelineView = timelineView;
		initTimelineView(startDate, endDate);
	}
	
	private void initTimelineView(LocalDate startDate, LocalDate endDate) {
		timelineView.setCreateClickHandler(this::onCreateClick);
		timelineView.drawTimeline(startDate, endDate);
		addTimelineEvent(new TimelineEvent(780, "SCHTARWARZ"));
	}
	
	private void addTimelineEvent(TimelineEvent event) {
		timelineView.drawNewEvent(event);
	}
	
	private void onCreateClick(ActionEvent e) {
		addTimelineEvent(new TimelineEvent(400, "JAKOB"));
	}
}



The view knows about the canvas, and can therefore get the GraphicsContext
It makes sense that the view will be drawing the timeline
I will also encourage you to add a TimelineEvent model class. A TimelineEventView would maybe make sense too, but that is up to you
public class TimelineView implements ITimelineView {
	private Button createBtn;
	private Canvas canvas;
	
	//TODO Init view ...
	
	@Override
	public void drawTimeline(LocalDate startDate, LocalDate endDate) {
		//TODO
	}
	
	@Override
	public void setCreateClickHandler(EventHandler<ActionEvent> handler) {
		createBtn.setOnAction(handler);
	}
	
	@Override
	public void drawNewEvent(TimelineEvent event) {
		GraphicsContext gc = canvas.getGraphicsContext2D();
		drawTimelineEvent(gc, event);
	}
	
	private void drawTimelineEvent(GraphicsContext gc, TimelineEvent event) {
		//...
	}
}



Low coupling and high cohesion

The above example is a good start for a MVC or MVP application
Lookup those design patterns if you do not already know about them


Hm yeah it looks like it could work really good. Unfortnuatly i dont create the application frames, buttons manually, so i have fxml files so im not 100% sure how this work. But i will try to understand it. I dont get whats ITimelineView is, also i have maybe it a bit messy right now. But you recommend i have a specific class that represent a timelineevent?
And the TimelineView is The main method, like scenes,stages an so on.
I for the moment have a specific class that does the drawing of the timeline. HM its going to take a while before i can figure out everything.. Thanks :D/> Il post when i have fixed it :)/>

I see now, so in drawNewEvent i send in the whole Event class as a in-param? I didnt know i could do that, im pretty new at java :D

This post has been edited by jabbeboy: 18 May 2014 - 09:57 AM

Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1379
  • View blog
  • Posts: 3,039
  • Joined: 05-April 11

Re: JavaFX Graphics Context

Posted 18 May 2014 - 10:11 AM

Quote

Unfortnuatly i dont create the application frames, buttons manually, so i have fxml files so im not 100% sure how this work.

You sure can still get a reference to your buttons even when they are declared in the fxml file
That would indeed be the preferred way :)

Quote

I dont get whats ITimelineView is

Oh simply an interface that I didn't include ^^ The interface is supposed to declare the methods having the @Override annotation above them

Quote

But you recommend i have a specific class that represent a timelineevent?

Yes, I highly recommend it!
Was This Post Helpful? 0
  • +
  • -

#9 jabbeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 13-January 14

Re: JavaFX Graphics Context

Posted 18 May 2014 - 10:41 AM

View PostCasiOo, on 18 May 2014 - 10:11 AM, said:

Quote

Unfortnuatly i dont create the application frames, buttons manually, so i have fxml files so im not 100% sure how this work.

You sure can still get a reference to your buttons even when they are declared in the fxml file
That would indeed be the preferred way :)/>

Quote

I dont get whats ITimelineView is

Oh simply an interface that I didn't include ^^/> The interface is supposed to declare the methods having the @Override annotation above them

Quote

But you recommend i have a specific class that represent a timelineevent?

Yes, I highly recommend it!


Okey. Sound reasonable and good.
The drawing of the timeline itself should also be in a seperate class that represents the timeline?
Was This Post Helpful? 0
  • +
  • -

#10 jabbeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 13-January 14

Re: JavaFX Graphics Context

Posted 18 May 2014 - 11:11 AM

IM sorry my stupidiness and newbieness. But i dont understand, to draw the initial timeline i call what method? drawTimeline in TImelineView has start, end dates as input, but no GraphicsContext? How am i supposed to paint on what?
How do i do with the rest of the stuff, like stage, scene? My main no looks like this, yeah shitty i know. mostly for testing purpose only.

public class TestTimeline extends Application{

	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		DrawTimeline dc = new DrawTimeline();
		TimelineEvent e = new TimelineEvent();
		DateClass d = new DateClass();

		Canvas canvas = dc.getCanvas();;
		Parent par = FXMLLoader.load(getClass().getResource("TimelineMainView.fxml"));
		Group root = new Group(par);
		GraphicsContext graphics = canvas.getGraphicsContext2D();
		
		stage.setTitle("Bluerift Timeline");
		stage.setWidth(1000);
		stage.setHeight(550);
		stage.setResizable(false);
		canvas.setWidth(1000);
		canvas.setHeight(450);

		/*
		 * Draws the graphics on the canvas/timeline
		 */
		dc.drawTimeline(graphics, d.startDate(), d.endDate());
		e.drawNewEvent(graphics, 600, "Jakob");
		
		
		root.getChildren().add(canvas);
		
		Scene scene = new Scene(root);
		scene.getStylesheets().
		add(this.getClass().getResource("JMetroLightTheme.css").toExternalForm());
		stage.setScene(scene);
		stage.show();
	}	

Was This Post Helpful? 0
  • +
  • -

#11 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: JavaFX Graphics Context

Posted 18 May 2014 - 05:04 PM

This prefixing interfaces with "I" nonsense is heresy. :)

Why would you write a TimeLineView class, which suggests to me that it is a UI widget, but give it no way to draw itself? Having another class named DrawTimeLine is strange. Does that draw a time line from the TimeLineEvent class? At this point, you may want to scrap your current project and do some research on MVC in Java. I think you jumped in without your arm floats and are sinking...
Was This Post Helpful? 0
  • +
  • -

#12 jabbeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 13-January 14

Re: JavaFX Graphics Context

Posted 18 May 2014 - 11:29 PM

View Postfarrell2k, on 18 May 2014 - 05:04 PM, said:

This prefixing interfaces with "I" nonsense is heresy. :)/>/>

Why would you write a TimeLineView class, which suggests to me that it is a UI widget, but give it no way to draw itself? Having another class named DrawTimeLine is strange. Does that draw a time line from the TimeLineEvent class? At this point, you may want to scrap your current project and do some research on MVC in Java. I think you jumped in without your arm floats and are sinking...


If you mean the TimelineView class that CasiOo suggested, i dont know. Thats the part i dont understand. I can't draw the timeline, theres no way to draw it, to draw it i need to have a graphicscontext. The drawTimeline class i had just so i could draw the timeline from another class, i dont know how i could do it in another way. Yes im inexperienced, but seemed pretty logic to me. But yes i have read a bit about MVC-pattern so i understand the principle. From this page:
http://www.tutorials...mvc_pattern.htm

I get that i should have a Demo class that calls the View class, but then theres the Controller class. I have done the application window and button in scenebuilder, which gives me a fxml file and a controller class. This controller class and the controller classes for every created windows is not the same? As i understand it's not.
So it's difficoult when i dont have exactly like you maybe think.
Then mainproblem now is i cant draw timeline. Also i dont understand whats "TimelineEvent event" is that im supposed to send in drawTimelineEvent.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1