# Creating your own Widget class

{% hint style="info" %}
You can create your own widget by extending the Widget class
{% endhint %}

{% code lineNumbers="true" %}

```java
public class MyWidget extends Widget
{
   /**
     * Constructs a Widget object.
     *
     * @param client The Minecraft client instance
     */
    public MyWidget(MinecraftClient client, <Other Parameters>) {
        super(client);
    }

    @Override
    public WidgetBox getWidgetBox() {
        // x1,y1,x2,y2 are used to define the box around the widget
        // scale variable to use the value from user or set your own scale
        return new WidgetBox(x1,y1,x2,y2,scale);
    }

    @Override
    public void render(DrawContext drawContext) {

    }
}
```

{% endcode %}

#### You can render whatever you want within the `render()` method

{% hint style="success" %}
{% code overflow="wrap" fullWidth="true" %}

```java
getWidgetBox() is used to define a widgetBox which the player can use to move or interact with.
```

{% endcode %}
{% endhint %}

### ***You can use this to add a widget using:*** [](https://tanishisherewith.gitbook.io/archived-legacy-dynamichud/widgets "mention")***.***

#### If you want to save and load this widget then you can add the following:

{% code lineNumbers="true" %}

```java
private String value = "V";
private String key = "Key";
@Override
public void writeToTag(NbtCompound tag) {
    super.writeToTag(tag);
    tag.putString(key, value);
}

@Override
public void readFromTag(NbtCompound tag) {
    super.readFromTag(tag);
    value = tag.getString(key /* Or "Key"*/);
}
```

{% endcode %}

To save and load this Widget, you can implement the `WidgetLoading`  class in your main class:

{% code lineNumbers="true" %}

```java
public class MyMainClass implements ClientModInitializer,WidgetLoading {
   private DynamicUtil dynamicutil;
     @Override
    public void onInitializeClient() {
      dynamicutil=DynamicHUD.getDynamicUtil();
      DynamicHUD.setIWigdets(new MyClass());
      dynamicutil.getWidgetManager().setWidgetLoading(new MyMainClass());
   }
    @Override
    public Widget loadWidgetsFromTag(String className, NbtCompound widgetTag) {
        //SAMPLE CODE EXAMPLE :
        if (className.equals(MyWidget.class.getName())) {
            MyWidget widget = new MyWidget (MinecraftClient.getInstance(),<Other parameters>);
            widget.readFromTag(widgetTag);
            return widget;
        }
        return WidgetLoading.super.loadWidgetsFromTag(className, widgetTag);
    }
}
```

{% endcode %}

{% hint style="info" %}
Using this code, we get the className from the load file of the widgets and using that we determine if the widget is of MyWidget class. If it is then we create a new widget whose parameter values are changed by reading the widget save data ( i.e, xpercent, yPercent, enabled, etc.)
{% endhint %}

#### Now set the MyMainClass as widgetLoading so that it is registered during loading or saving:

```java
dynamicutil.getWidgetManager().setWidgetLoading(new MyMainClass());
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tanishisherewith.gitbook.io/archived-legacy-dynamichud/widgets/creating-your-own-widget-class.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
