السلام عليكم
كيف حالكم جميعاً وما أخبار العيد معكم
لي فترة أحاول أن أطور حزمة المحرك AAnimation
ولا يخفى عليكم أن هذا النوع من البرمجة
-أقصد برمجة الحزم packages لا البرمجة باستخدام حزمتي-
تحتاج إلى اختبار من قبل المستخدمين وأخذ التغذية الراجعة feedback من قبل المطورين
وبما أن الجميع يعتبرني مجرد مدعي
قررت أن أكون أنا أول المستخدمين لحزمتي
أترككم مع عارض الصور الذي قمت ببرمجته باستخدام الحزمة
ابدأ أولاً مع الفئة ImagePanel
هذه الفئة تمثل صورة موضوعة في panel
أفكر في أن أضيفها إلى حزمة أدواتي MyComponents في نسختها القادمة بإذن الله
لكني أحتاج إلى تطويرها بعض الشيء
package imageViewer;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
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;
public class ImagePanel extends JPanel {
private BufferedImage image;
private Image scalledImage;
private final URL resource;
private double currentAngle;
private boolean changeInRotationAngle;
public ImagePanel(String imageURI) {
//System.out.println(imageURI);
resource = getClass().getResource(imageURI);
}
public boolean isScalledImageCreated(int width, int height) {
return scalledImage != null && scalledImage.getHeight(null) == height
&& scalledImage.getWidth(null) == width;
}
public void createThumpnail(int width, int height)
throws MalformedURLException, IOException {
if (image == null)
loadImage();
if (isScalledImageCreated(width, height))
return;
scalledImage = image.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
// System.out.println("create thumpnail");
repaint();
// setBounds(0, 0, width, height);
// setBounds(0, 0, image.getWidth(), image.getHeight());
}
public void loadImage() throws MalformedURLException, IOException {
image = ImageIO.read(resource);
// System.out.println("load image");
}
public void setRotateAngle(double degree) {
double tempAngle = degree%360;
if(currentAngle==tempAngle)
return;
currentAngle=tempAngle;
repaint();
}
public void addToRotateAngle(double degree){
currentAngle=((degree%360)+currentAngle)%360;
repaint();
//System.out.println(currentAngle);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d=(Graphics2D)g;
// System.out.println("enter paint image");
if (scalledImage == null) {
return;
}
super.paintComponent(g);
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform)(origXform.clone());
//center of rotation is center of the panel
int xRot = this.getWidth()/2;
int yRot = this.getHeight()/2;
newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
g2d.setTransform(newXform);
//draw image centered in panel
int x = (getWidth() - image.getWidth(this))/2;
int y = (getHeight() - image.getHeight(this))/2;
g.drawImage(scalledImage, 0, 0, scalledImage.getWidth(null),
scalledImage.getHeight(null), null);
g2d.setTransform(origXform);
// System.out.println("paint image");
// g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(),null);
}
}مميزات الفئة السابقة
1- تمكنك من تصغير الصورة إلى الحجم الذي تريده بالضبط وتضعها في منتصف الحاوية panel
2- تمكنك من تدوير الصورة بأي زاوية تريد
3- تمكنك من تحميل الصورة في الذاكرة عند الحاجة فقط lazy initialization
الفئة ImageAnimation وهذه الفئة تقوم ببناء الواجهة AnimationListner
المسؤولة عن الأحداث في الحزمة AAnimation
package imageViewer;
import version8.AnimationEvent;
import version8.AnimationListEvent;
import version8.AnimationListner;
public class ImageAnimation implements AnimationListner {
private int count;
public void beforeAnimationList(
AnimationListEvent animationListEvent) {
//System.out.println("before animation lsit");
}
public void beforeAnimation(AnimationEvent animationEvent) {
// TODO Auto-generated method stub
}
public void afterAnimationList(
AnimationListEvent animationListEvent) {
// TODO Auto-generated method stub
}
public void afterAnimation(AnimationEvent animationEvent) {
if(!animationEvent.getAnimationObject().isAnimationObjectWakeOutOfAnimationEngineContatiner()){
count++;
}
else
count--;
if(count==0||count==animationEvent.getAnimationList().getLength()){
animationEvent.getAnimationObject().setList(null);
}
//System.out.println(count);
}
}الفئة الأخيرة وهي الفئة عارض الصور
package imageViewer;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import version8.AnimationEngine;
import version8.AnimationEvent;
import version8.AnimationList;
import version8.AnimationListEvent;
import version8.AnimationListner;
import version8.AnimationObject;
import version8.WhenAnimationObjectReachBoundsOfAnimationEngineContainer;
import version8.script.PointToPointAnimation;
public class ImageViewer extends JPanel implements ActionListener {
// private List<ImagePanel> imagePanels;
public static final int constatnt = 20;
private AnimationEngine animationEngine;
private List<AnimationObject> animationObjects = new ArrayList<AnimationObject>();
private JLayeredPane layerdPane = new JLayeredPane();
private int currentImageIndex = 0;
private JButton previousButton, nextButton;
private AnimationList forwordAnimationList2;
private AnimationList backwordAnimationList2;
public ImageViewer(int width, int height, boolean lazyIntialization,List<ImagePanel> imagePanels)
throws MalformedURLException,IOException {
intializeEnimationEngine();
boolean first = true;
int i = 0;
for (ImagePanel imagePanel : imagePanels) {
i++;
first = createAnimationObject(width, height, lazyIntialization,
first, i, imagePanel);
}
intialization(width,height);
}
public ImageViewer(int width, int height, boolean lazyIntialization,ImagePanel... imagePanels)
throws MalformedURLException,IOException {
intializeEnimationEngine();
boolean first = true;
int i = 0;
for (ImagePanel imagePanel : imagePanels) {
i++;
first = createAnimationObject(width, height, lazyIntialization,
first, i, imagePanel);
}
intialization(width,height);
}
private boolean createAnimationObject(int width, int height,boolean lazyIntialization, boolean first, int i,ImagePanel imagePanel)
throws MalformedURLException, IOException {
AnimationObject animationObject;
if (first) {
animationObject = new AnimationObject(new Point(constatnt, 0),imagePanel, null, false);
animationObject.setBounds(0, 0, width - constatnt * 2, height);
first = false;
}
else {
animationObject = new AnimationObject(new Point(width- constatnt, 0), imagePanel, null, false);
animationObject.setBounds(width - constatnt * 2, 0, width- constatnt * 2, height);
}
layerdPane.add(imagePanel, new Integer(i));
if (!lazyIntialization) {
imagePanel.createThumpnail(imagePanel.getWidth(), imagePanel.getHeight());
imagePanel.repaint();
}
//animationObject.setActionWhenAnimationObjectReachBoundsOfAnimationEngineContatin
er(WhenAnimationObjectReachBoundsOfAnimationEngineContainer.IGNORE_ALL_REJECTED
_ANIMATION);
animationObject.addAnimationListner(new ImageAnimation());
animationObjects.add(animationObject);
animationEngine.addAnimationObject(animationObject);
return first;
}
private void intializeEnimationEngine() {
animationEngine = new AnimationEngine(layerdPane);
animationEngine.setAutoActiviate(false);
new Thread(animationEngine).start();
}
private void intialization(int width,int height)throws MalformedURLException, IOException {
ImagePanel imagePanel = (ImagePanel) animationObjects.get(0).getComponent();
imagePanel.createThumpnail(width, height);
setLayout(null);
setBounds(0, 0, height, width);
intializePanel(width, height);
intializeAnimationList();
intializePreviousButton(height);
intializeNextButton(width, height);
}
private void intializeNextButton(int width, int height) {
nextButton = new JButton(">");
nextButton.setBounds(width - constatnt, 0, constatnt, height);
if (currentImageIndex == animationObjects.size() - 1) {
nextButton.setEnabled(false);
}
nextButton.addActionListener(this);
add(nextButton);
}
private void intializePreviousButton(int height) {
previousButton = new JButton("<");
previousButton.setBounds(0, 0, constatnt, height);
previousButton.setEnabled(false);
previousButton.addActionListener(this);
add(previousButton);
}
private void intializeAnimationList() {
forwordAnimationList2 = new PointToPointAnimation(-layerdPane.getWidth(), 0,0, 0, 1000).excute();
backwordAnimationList2 = new PointToPointAnimation(0, 0, -layerdPane.getWidth(), 0, 1000).excute();
}
private void intializePanel(int width, int height) {
layerdPane.setLayout(null);
layerdPane.setBounds(constatnt, 0, width - constatnt * 2, height);
add(layerdPane);
}
private void next() throws MalformedURLException, IOException {
System.out.println("hello method");
currentImageIndex++;
final ImagePanel imagePanel = (ImagePanel) animationObjects.get(currentImageIndex).getComponent();
if(!imagePanel.isScalledImageCreated(imagePanel.getWidth(), imagePanel.getHeight())){
new Thread(new Runnable() {
public void run() {
try {
imagePanel.createThumpnail(imagePanel.getWidth(), imagePanel.getHeight());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (currentImageIndex == animationObjects.size() - 1) {
nextButton.setEnabled(false);
}
else
nextButton.setEnabled(true);
previousButton.setEnabled(true);// any time method next calling then
animationObjects.get(currentImageIndex).setList(backwordAnimationList2);
animationEngine.activeEngine();
}
}).start();
System.out.println("hello if");
}
else{
//currentImageIndex++;
//imagePanel.createThumpnail(imagePanel.getWidth(), imagePanel.getHeight());
if (currentImageIndex == animationObjects.size() - 1) {
nextButton.setEnabled(false);
}
else
nextButton.setEnabled(true);
previousButton.setEnabled(true);// any time method next calling then
animationObjects.get(currentImageIndex).setList(backwordAnimationList2);
animationEngine.activeEngine();
System.out.println("hello else");
}
}
private void previous() throws MalformedURLException, IOException {
currentImageIndex--;
ImagePanel imagePanel = (ImagePanel) animationObjects.get(currentImageIndex).getComponent();
imagePanel.createThumpnail(imagePanel.getWidth(), imagePanel.getHeight());
if (currentImageIndex == 0) {
previousButton.setEnabled(false);
}
else
previousButton.setEnabled(true);
nextButton.setEnabled(true);// any time method previous calling then
animationObjects.get(currentImageIndex + 1).setList(forwordAnimationList2);
animationEngine.activeEngine();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == nextButton) {
try {
next();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
} else if (e.getSource() == previousButton) {
try {
previous();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Image Viewer Test");
frame.setLayout(null);
try {
ImagePanel comp1 = new ImagePanel("1.jpg");
ImagePanel comp2 = new ImagePanel("2.jpg");
ImagePanel comp3 = new ImagePanel("3.jpg");
ImagePanel comp4 = new ImagePanel("4.jpg");
ImagePanel comp5 = new ImagePanel("5.jpg");
ImagePanel comp6 = new ImagePanel("6.bmp");
ImagePanel comp7 = new ImagePanel("7.jpg");
ImagePanel comp8 = new ImagePanel("8.jpg");
ArrayList<ImagePanel> imagePanels = new ArrayList<ImagePanel>();
imagePanels.add(comp1);
imagePanels.add(comp2);
imagePanels.add(comp3);
imagePanels.add(comp4);
imagePanels.add(comp5);
imagePanels.add(comp6);
imagePanels.add(comp7);
imagePanels.add(comp8);
ImageViewer viewer = new ImageViewer(450, 450, true, imagePanels);
viewer.setBounds(17, 7, 450, 450);
frame.add(viewer);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}تحدثت عن موضوع JLayeredPane في لمحة الصراع الطبقي
لست جيداً في شرح ما أقوم به
لذا أي أسئلة سأكون جاهز إن شاء الله
شيء أخير قبل أن أختم لاحظت بطء في تحميل الصور ولا أعرف كيف أحل هذه المشكلة
هل لدى أحدكم حل لها؟
أظن أن للأمر علاقة بموضوع ClassLoader
تحياتي


