ImageProxyTest.java
package test.patterns.virtualproxy;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
public class ImageProxyTest {
public static void main(String[] args) {
ImageProxyTest test = new ImageProxyTest();
try {
test.start();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
public void start() throws MalformedURLException {
//This has a BorderLayout manager's control
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Icon icon = WebIconFactory.getIcon("http://politeia.org.ro/wp-content/uploads/2014/09/ponta-china.jpg");
ImageComponentWrapper wrapper = new ImageComponentWrapper(icon);
//ImageComponent wrapper = new ImageComponent(icon);
frame.getContentPane().add(wrapper);
frame.setSize(1000, 700);
frame.setVisible(true);
}
}
WebIconFactory.java
package test.patterns.virtualproxy;import javax.swing.*;
import java.awt.*;
import java.net.*;
public class WebIconFactory {
public static Icon getIcon(String url) throws MalformedURLException {
return new ImageProxy(new URL(url));
}
}
ImageProxy.java
package test.patterns.virtualproxy;import javax.swing.*;
import java.awt.*;
import java.net.*;
public class ImageProxy implements Icon {
ImageIcon imageIcon;
URL imageURL;
Thread retrievalThread;
boolean retriving = false;
public ImageProxy(URL url) {
imageURL = url;
}
public int getIconWidth() {
if (imageIcon != null) {
return imageIcon.getIconWidth();
} else {
return 800;
}
}
public int getIconHeight() {
if (imageIcon != null) {
return imageIcon.getIconHeight();
} else {
return 600;
}
}
public void paintIcon(final Component c, Graphics g, int x, int y) {
if (imageIcon != null) {
imageIcon.paintIcon(c, g, x, y);
} else {
g.drawString("Loading Image from web ... ", x + 300, y + 200);
if (!retriving) {
retriving = true;
retrievalThread = new Thread(new Runnable() {
public void run(){
try {
//simulate a long time to get the image
java.lang.Thread.sleep(2500);
imageIcon = new ImageIcon(imageURL, "CD Cover");
c.repaint();
} catch(Exception ex) {
ex.printStackTrace();
}
}
});
retrievalThread.start();
}
}
}
}
ImageComponentWrapper.java
package test.patterns.virtualproxy;import java.awt.*;
import javax.swing.*;
public class ImageComponentWrapper extends JComponent {
private Icon icon;
public ImageComponentWrapper(Icon icon) {
this.icon = icon;
}
public void setIcon(Icon icon) {
this.icon = icon;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = icon.getIconWidth();
int h = icon.getIconHeight();
int x = (800 - w)/2;
int y = (600 - h)/2;
icon.paintIcon(this, g, x, y);
}
}
Icon.java
package test.patterns.virtualproxy;import javax.swing.*;
import java.awt.*;
public interface Icon {
public int getIconWidth();
public int getIconHeight();
public void paintIcon(final Component c, Graphics g, int x, int y);
}
Steps
- >javac -d class *.java
- >cd class
- \class>java test.patterns.virtualproxy.ImageProxyTest
Niciun comentariu:
Trimiteți un comentariu