How do I implement classes in Processing? [duplicate] - processing

This question already exists:
Closed 10 years ago.
Possible Duplicate:
Asteroids game in processing
Trying to integrate some classes for Asteroids game into Processing program. How to you implement classes into a processing program? I'm trying to use the classes the professor gave us to use but I'm getting a "The constructor refers to the missing type" error

Daniel Shiffman provides a great intro to object-oriented programming in the Processing IDE here.
Each custom Processing class is stored as a .pde file.
If you're trying to include classes encapsulated within .pde files, open the .pde in the folder that contains the setup() and draw() methods.
If you're trying to make a new class, click the arrow button at the upper-right and select New Tab to create a new class.

Related

Where should a game logic be within a JavaFx application?

Being new to Javafx and java, I'm having trouble picturing the design for a chess game.
So I have the start method like following in my JavaFX Application-extended class:
public void start(Stage primaryStage) throws Exception{
Scene scene = new Scene(createContent());
primaryStage.setTitle("ChessGame");
primaryStage.getIcons().add(new Image("file:images/icon.png"));
primaryStage.setScene(scene);
primaryStage.show();
}
protected Parent createContent(){
Pane root = new Pane();
root.setPrefSize(WIDTH*TILE_SIZE, HEIGHT*TILE_SIZE);
boardInitialize("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -");
root.getChildren().add(tileGroup);
root.getChildren().add(pieceGroup);
return root;
}
The problem is, I don't know where to put the game's logic inside my application. The game logic will handle player's turn, check for checks and checkmates, generate possible moves, etc (and later a very crude AI if possible). I have tried to jam it into the start method, but it doesn't work because the start method only runs once. Pygame with their gameloop makes much more sense than this and I can see how I would go with it. So my question is: where do I put the game logic in my application?
You may use the Model-View-Controller (+ Network if needed) architecture to design your system. JavaFX is really useful when working with the MVC.
Rules of Thumb:
1- Do not put your game logic inside the Model, Network and View(FXML) classes.
2- Use FXML as part of the design (it will bootstrap your work).
3- Try to achieve " Low coupling & High Coherence "
4- Some example from a fully-working MVC JavaFX project.
> /* WelcomeScreenController class (interacts with the FXML file)*/
> #FXML
> void doSignup(ActionEvent event) {
>
> user = username.getText();
> pass = password.getText();
>
> if(user != null && !user.isEmpty() && pass != null && !pass.isEmpty())
> if (checkBox.isSelected())
> GameEngine.game().getAccessManager().callSignupService(user,pass);
> else
> showNotification("You need to accept terms.");
> }
5- Try to handle button logic from the Controller class of the particular FXML file. http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm
6- Create a controller class named "GameEngine" to do all your computations about the game logic. Call GameEngine's methods from the button's handle action event method.
7- Try to use Object-Oriented Programming methodologies. (Polymorphism, Inheritance, Design Patterns, etc.)
I would stand upon the shoulders of others and use a library that already has some structure in it.
With that you can follow the model that build upon the experience of others and use their best practices and avoid the pitfalls they have experienced.
For JavaFX I am aware of the FXGL library, which comes with a nice list of examples of how to build a game in JavaFX.
On YouTube you can find several examples and tutorials for this library.
First of all, I would like to let you know that I have never written a Javafx application and I could be wrong.
The start method, as you correctly pointed out is the main entry point of a Javafx application, as you can see from the anatomy of a Javafx application described here. You can initialize your game there, but naturally, the moves will occur later than running the start. You will need to use event handling to handle the case when a move is being attempted by a player.
Once you are able to handle the move events, you will also be able to check whether a move is correct or not, or, to check the possible valid move options of a given player. I would like to suggest that you should have your own classes for the business logic and to avoid mixing business logic with event handling, even though the event handler will invoke business logic methods.

Nested Hierarchy Naming Rules & How To Keep Track Of Attached Scripts

I use the MVC programming model. I am running into a problem using Unity's hierarchy inspector. Say I have a Controller script: C_CardDrag. Now, because this drags a UI item thats nested several layers deep, it won't be readily apparenty that this C_CardDrag script is attached to an icon within in the card, and NOT the root object. You see what I mean in the image below (the blue highlight is where the C_Drag needs to be attached, NOT the top level object, otherwise the whole thing gets dragged when I only want the icon in the middle dragged).
I feel that it can be VERY confusing to design and name classes when they need to appear on nested objects.
Do you have any design practices I can follow to make script names clearer to where they are attached?
For example say I have a Tree object, and it has several brances. And lets say I have a C_Tree script, and a C_Branch script. How would you handle making it clear that C_Tree goes on a Tree, a TOP level object, where as C_Branch goes on a nested UI object?
Update:
Found a good resource here: Component based game engine design
Like any software that is being developed, you should only apply the appropriate Architectural and Design patterns that fit that project.
For example:
I was building a game that would be used for different devices. Although Unity lets you switch from Android to iOS seemingly easy. This game could be played completely differently on different devices, so I wen't with MVC.
Another game was built around Peer to Peer.
So overall you have a lot of options.
The way you can organize your files is by folders in the Project section of the Editor. I like to use something like this:
You will notice the Model is not in the Hierarchy. I don't use MonoBehaviours for models. In some cases, even the Controller could be non-MonoBehaviour code. That is why I prefer to have all the organization happen in the Project not the Hierarchy.

Talking Among GWT Panels using UIBinder Layout

New to GWT here...
I'm using the UIBinder approach to layout an app, somewhat in the style of the GWT Mail sample. The app starts with a DockLayoutPanel added to RootLayoutPanel within the onModuleLoad() method. The DockLayoutPanel has a static North and a static South, using a custom center widget defined like:
public class BigLayoutWidget extends ResizeComposite {
...
}
This custom widget is laid out using BigLayoutWidget.ui.xml, which in turn consists of a TabLayoutPanel (3 tabs), the first of which contains a SplitLayoutPanel divided into WEST (Shortcuts.ui.xml) and CENTER (Workpanel.ui.xml). Shortcuts, in turn, consists of a StackLayoutPanel with 3 stacks, each defined in its own ui.xml file.
I want click events within one of Shortcuts' individual stacks to change the contents of Workpanel, but so far I've only been able to manipulate widgets within the same class. Using the simplest case, I can't get a button click w/in Shortcuts to clear the contents of Workpanel or make WorkPanel non-visible.
A few questions...
Is ResizeComposite the right type of class to extend for this? I'm following the approach from the Mail example for TopPanel, MailList, etc, so maybe not?
How can I make these clicks manipulate the contents of panels in which they do NOT reside?
Are listeners no longer recommended for handling events? I thought I saw somewhere during compilation that ClickHandlers are used these days, and the click listener "subscription" approach is being deprecated (I'm mostly using #UiHandler annotations)
Is there an easy way to get a handle to specific elements in my app/page? (Applying the "ID" field in the UI.XML file generates a deprecation warning). I'm looking for something like a document.getElementById() that get me a handle to specific elements. If that exists, how do I set the handle/ID on the element, and how can I then call that element by name/id?
Note that I have the layout itself pretty well nailed; it's the interaction from one ui.xml modularized panel to the next that I can't quite get.
Thanks in advance.
If you don't have a use for resizing events than just use Composite
What you want is what the GWT devs called message bus (implemented as HandlerManager). You can get a nice explanation in the widely discussed (for example, on the GWT Google Group, just search for 'mvp') presentation by Ray Ryan from Google I/O 2009 which can be found here. Basically, you "broadcast" an event on that message bus and then a Widget listening for that event gets the message and does its stuff.
Yep, *Handlers are the current way of handling events - the usage is basically the same so migration shouldn't be a problem (docs). They changed it so that they could introduce custom fields in the future, without breaking existing code.
If you've set an id for any DOM element (for Widgets I use someWidget.getElement().setId(id), usually in combination with DOM.createUniqueId()) you can get it via GWT.get(String id). You'll get then a RootPanel which you'll have to cast to the right Widget class - as you can see it can get a little 'hackish' (what if you change the type of the Widget by that id? Exceptions, or worse), so I'd recommend sticking with MVP (see the first point) and communicating via the message bus. Remember however, that sometimes it's also good to aggregate - not everything has to be handled via the message bus :)
Bottom line is I'd recommend embracing MVP (and History) as soon as possible - it makes GWT development much easier and less messy :) (I know from experience, that with time the code starts to look like a nightmare, if you don't divide it into presentation, view, etc.)

Photoshop style GUI design

In a tool such as Photoshop, there is a selection of tools you can click on (e.g. pen, brush etc). Once you click on a tool, you can use that tool to paint, erase etc depending on what tool is selected.
I'm wondering how that would be best implemented in an OO design. I could only think of having a GUIManager that always knows which tool is selected, then when the Canvas (drawing area) detects a mouse click or mouse drag it asks GUIManager which tool is selected and applies that tool's behavior.
Does anyone describe a possible solution in class level detail (or in any detail if you don't have time).
Take a look at the State Pattern which has exactly your example.
However, it's not actually a GUI question but more to an object-oriented approach to how to model this problem. The GUI doesn't really play into this.
I'm not sure OO principles really play into this except in the sense that all graphic elements are probably derived from common base classes in your GUI of choice.
The amount of complexity in an application that needs a hierarchy of screen menus à la photoshop would presumably dwarf the difference between composition vs inheritance vs functional-style vs whatever-you-want.
Although, should you be motivated to write the program twice using different styles, it might be interesting to see the results. The ranking of various programming styles is an open question without real answers at this point, although we think that OO-styles are important in a way we haven't yet completely quantified.
One idea for this would be to have a tool class. Just sketching this in C++-like pseudocode:
class Tool
{
public:
// Keep a pointer to the "document", i.e. some representation of the
// image you are editing
Tool(Document *pDoc);
// Process mouse events -- this need to be overridden to execute
// the appropriate behaviour depending on the concrete type of tool
virtual void OnMouseEvent(const MouseEvent &e) = 0;
// and so on
};
You then inherit from this abstract tool class to provide the concrete tools your application needs. When a certain tool is selected, you instantiate a corresponding tool object and remember it somewhere. The window forwards mouse events to the currently active tool object, which then does the appropriate thing for the tool that was selected.
This is an implementation of the State pattern that Johannes refers to.

DLGTEMPLATE to CWnd-derived control

Is it possible to take a DLGTEMPLATE and use it as a CWnd-derived control for placing in any other CWnd?
I have a dialog template that I want to use on one of my CDockablePanes
Your question isn't clear, please rephrase. If you're asking if it's possible to instantiate a control from a DLGTEMPLATE and put that instantiation on another dialog or as the child of another control, then yes, it is. It's a major PITA though, read through the Old New Thing blog (the series on dialog templates) if you want to know the details. If you just want to get it to work, use the CDlgTempl class from one of the MSDN samples; start here: http://support.microsoft.com/kb/155257. It won't make CWnd-derived controls but they're not what you want if you're asking what I think you are; you'll get a CDialog-derived object (which is itself derived from CWnd obviously, so in that sense it does give you what you ask).

Resources