com.mp.gw2api.data.GW2APIColor.java Source code

Java tutorial

Introduction

Here is the source code for com.mp.gw2api.data.GW2APIColor.java

Source

/*
 * Copyright (C) 2016 Hero
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.mp.gw2api.data;

import com.mp.gw2api.base.GW2APIID;
import com.mp.gw2api.base.IGW2APIData;
import com.mp.gw2api.base.RGBColor;
import com.mp.gw2api.base.GW2Material;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author Mitchell Pell
 */
public class GW2APIColor extends GW2APIID implements IGW2APIData {

    public RGBColor baseRGB; //base_rgb
    public GW2Material cloth; //cloth
    public GW2Material leather; //leather
    public GW2Material metal; //metal
    public long item; //item
    public String[] categories; //categories

    @Override
    public void LoadFromJsonText(String text) {
        JSONParser parser = new JSONParser();
        JSONObject json = null;
        JSONObject jsonb = null;
        JSONArray ja = null;
        JSONArray jb = null;

        try {
            //Parse json array
            json = (JSONObject) parser.parse(text);

            //-id
            id = (long) json.get("id");
            //-name
            name = (String) json.get("name");

            //-cloth
            cloth = new GW2Material();
            jsonb = (JSONObject) json.get("cloth");
            if (jsonb != null) {
                cloth.brightness = (long) jsonb.get("brightness");
                try {
                    cloth.contrast = (double) (Long.parseLong(jsonb.get("contrast").toString()));
                } catch (NumberFormatException ex) {
                    try {
                        cloth.contrast = (Double.parseDouble(jsonb.get("contrast").toString()));
                    } catch (NumberFormatException ey) {
                        cloth.contrast = 0.f;
                    }

                }
                cloth.hue = (long) jsonb.get("hue");
                cloth.saturation = (double) jsonb.get("saturation");
                cloth.lightness = (double) jsonb.get("lightness");
                ja = (JSONArray) jsonb.get("rgb");
                if (ja != null)
                    if (ja.size() == 3) {
                        cloth.color = new RGBColor();
                        cloth.color.r = (byte) ((long) ja.get(0));
                        cloth.color.g = (byte) ((long) ja.get(1));
                        cloth.color.b = (byte) ((long) ja.get(2));
                    }
            }
            //-leather
            leather = new GW2Material();
            jsonb = (JSONObject) json.get("leather");
            if (jsonb != null) {
                leather.brightness = (long) jsonb.get("brightness");
                try {
                    cloth.contrast = (double) (Long.parseLong(jsonb.get("contrast").toString()));
                } catch (NumberFormatException ex) {
                    try {
                        cloth.contrast = (Double.parseDouble(jsonb.get("contrast").toString()));
                    } catch (NumberFormatException ey) {
                        cloth.contrast = 0.f;
                    }

                }
                leather.hue = (long) jsonb.get("hue");
                leather.saturation = (double) jsonb.get("saturation");
                leather.lightness = (double) jsonb.get("lightness");
                ja = (JSONArray) jsonb.get("rgb");
                if (ja != null)
                    if (ja.size() == 3) {
                        leather.color = new RGBColor();
                        leather.color.r = (byte) ((long) ja.get(0));
                        leather.color.g = (byte) ((long) ja.get(1));
                        leather.color.b = (byte) ((long) ja.get(2));
                    }
            } //-metal
            metal = new GW2Material();
            jsonb = (JSONObject) json.get("metal");
            if (jsonb != null) {
                metal.brightness = (long) jsonb.get("brightness");
                try {
                    metal.contrast = (double) (Long.parseLong(jsonb.get("contrast").toString()));
                } catch (NumberFormatException ex) {
                    try {
                        metal.contrast = (Double.parseDouble(jsonb.get("contrast").toString()));
                    } catch (NumberFormatException ey) {
                        metal.contrast = 0.f;
                    }

                }
                metal.hue = (long) jsonb.get("hue");
                metal.saturation = (double) jsonb.get("saturation");
                metal.lightness = (double) jsonb.get("lightness");
                ja = (JSONArray) jsonb.get("rgb");
                if (ja != null)
                    if (ja.size() == 3) {
                        metal.color = new RGBColor();
                        metal.color.r = (byte) ((long) ja.get(0));
                        metal.color.g = (byte) ((long) ja.get(1));
                        metal.color.b = (byte) ((long) ja.get(2));
                    }
            }

            //-item
            Object obj = json.get("item");
            if (obj != null)
                item = (long) obj;
            else
                item = -1;
            //-catagories []
            ja = (JSONArray) json.get("categories");
            if (ja != null)
                if (ja.size() > 0) {
                    categories = new String[ja.size()];
                    for (int i = 0; i < ja.size(); i++)
                        categories[i] = (String) ja.get(i);
                }

            int finallydone = 22;
        } catch (ParseException ex) {
            Logger.getLogger(com.mp.gw2api.lists.GW2APIMapList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}