Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TwiconCoder
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.border.Border;
import twitter4j.ProfileImage;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import com.topcoder.client.contestApplet.editors.Standard.EntryPoint;
import com.topcoder.client.contestApplet.editors.Standard.StandardEditor;
import com.topcoder.client.contestApplet.editors.Standard.StandardEditorPanel;
public class TwiconCoder extends EntryPoint {
String screenname = "nise_nabe";
BufferedImage img;
public TwiconCoder() {
Twitter t = TwitterFactory.getSingleton();
try {
ProfileImage image = t.getProfileImage(screenname, ProfileImage.BIGGER);
img = ImageIO.read(new URL(image.getURL().toString()));
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public JPanel getEditorPanel() {
StandardEditorPanel panel = (StandardEditorPanel) super.getEditorPanel();
JScrollPane scroll = (JScrollPane) panel.getComponent(0);
scroll.setViewportBorder(new CentredBackgroundBorder(img));
JViewport viewport = (JViewport) scroll.getComponent(0);
viewport.setOpaque(false);
JPanel vpanel = (JPanel) viewport.getComponent(0);
vpanel.setOpaque(false);
StandardEditor editor = (StandardEditor) vpanel.getComponent(0);
editor.setOpaque(false);
return panel;
}
private class CentredBackgroundBorder implements Border {
private final BufferedImage image;
public CentredBackgroundBorder(BufferedImage image) {
this.image = image;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
x += (width - image.getWidth()) / 2;
y += (height - image.getHeight()) / 2;
((Graphics2D) g).drawRenderedImage(image, AffineTransform.getTranslateInstance(x, y));
}
@Override
public Insets getBorderInsets(Component c) {
return new Insets(0, 0, 0, 0);
}
@Override
public boolean isBorderOpaque() {
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment