package com.jotuntech.sketcher.client.command; import java.awt.AlphaComposite; import java.nio.ByteBuffer; import com.jotuntech.sketcher.client.Client; import com.jotuntech.sketcher.client.Command; import com.jotuntech.sketcher.client.Connection; import com.jotuntech.sketcher.common.BitmapLayer; import com.jotuntech.sketcher.common.User; import com.jotuntech.sketcher.common.filter.Filter; public class FilterCommand implements Command { private Filter filter; private int x, y, w, h; float a, b, c; public FilterCommand() { } public FilterCommand(Filter filter, float x, float y, float w, float h, float a, float b, float c) { this.filter = filter; this.x = (int) x; this.y = (int) y; this.w = (int) w; this.h = (int) h; this.a = a; this.b = b; this.c = c; } public int perform(Client client, User user) { if(user == null) { return Connection.SEND_NONE; } BitmapLayer pl = (BitmapLayer) user.getPhantomLayer(); filter.setSize(w, h); filter.setParameterA(a); user.getLayer().copyTo(pl, null, false, x, y, x, y, w, h); pl.setAlphaRule(AlphaComposite.SRC); pl.setOpacity(1); pl.applyFilter(filter, user.getLayer().getImageObserver(), x, y, w, h); return Connection.SEND_OTHERS; } public void decode(ByteBuffer in) { x = (int) in.getFloat(); y = (int) in.getFloat(); w = (int) in.getFloat(); h = (int) in.getFloat(); a = in.getFloat(); b = in.getFloat(); c = in.getFloat(); StringBuffer filterNameBuffer = new StringBuffer(); while(in.remaining() >= 2) { filterNameBuffer.append(in.getChar()); } String filterName = "com.jotuntech.sketcher.common.filter." + filterNameBuffer.toString(); try { filter = (Filter) Class.forName(filterName).newInstance(); } catch (Throwable t) { t.printStackTrace(); } } public void encode(ByteBuffer out) { out.putFloat(x); out.putFloat(y); out.putFloat(w); out.putFloat(h); out.putFloat(a); out.putFloat(b); out.putFloat(c); String filterName = filter.getClass().getSimpleName(); for(int i = 0; i < filterName.length(); i++) { out.putChar(filterName.charAt(i)); } } }