Java AWT (Abstract Window Toolkit) ব্যবহার করে Custom Animation তৈরি করতে এবং Double Buffering ব্যবহার করার জন্য কিছু বিশেষ টেকনিক প্রয়োগ করা হয়, যা গ্রাফিক্সকে মসৃণ এবং কার্যকরীভাবে প্রদর্শন করতে সাহায্য করে। Double Buffering এমন একটি পদ্ধতি যা স্ক্রীনে অ্যানিমেশন বা গ্রাফিক্স আপডেট করার সময় Flicker বা ঝাপসা হওয়ার সমস্যা এড়ায়।
এখানে Custom Animation এবং Double Buffering এর উদাহরণ দেয়া হয়েছে:
1. Custom Animation with AWT
Java AWT-এ কাস্টম অ্যানিমেশন তৈরির জন্য সাধারণত paint() অথবা update() মেথড ব্যবহার করা হয়, যেখানে গ্রাফিক্স অবজেক্টগুলি আঁকা হয় এবং নিয়মিত আপডেট করা হয়।
import java.awt.*;
import java.awt.event.*;
public class CustomAnimationExample extends Frame {
// Variables for animation
private int x = 50; // Initial position
private int direction = 1; // Direction of movement: 1 means right, -1 means left
public CustomAnimationExample() {
setTitle("Custom Animation Example");
setSize(500, 500);
setVisible(true);
// Start animation
new Thread(new AnimationRunnable()).start();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillRect(x, 200, 50, 50); // Draw the animated object (a red square)
}
// Animation thread that moves the square
class AnimationRunnable implements Runnable {
@Override
public void run() {
while (true) {
// Update the position of the square
x += direction * 5;
// Reverse direction when the square hits the window boundaries
if (x > getWidth() - 50 || x < 0) {
direction = -direction;
}
// Repaint the frame to update the animation
repaint();
try {
Thread.sleep(50); // Control the speed of the animation
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new CustomAnimationExample();
}
}
ব্যাখ্যা:
- AnimationRunnable থ্রেডটি
xপজিশন আপডেট করে এবংrepaint()কল করে পরবর্তী ফ্রেমে অ্যানিমেশন রেন্ডার করার জন্য। paint()মেথডে অ্যানিমেটেড অবজেক্ট (একটি লাল বর্গ) আঁকা হয়। এর পজিশনxমানের ভিত্তিতে পরিবর্তিত হয়।
2. Double Buffering in AWT
Double Buffering ব্যবহার করে আপনি গ্রাফিক্সের সকল পরিবর্তন একটি ব্যাকগ্রাউন্ড ইমেজে প্রথমে আঁকেন এবং তারপর সেগুলোকে ফ্রন্ট স্ক্রীনে রেন্ডার করেন। এর ফলে স্ক্রীনে আঁকার সময় ঝাপসা বা ফ্লিকারিং হ্রাস পায় এবং অ্যাপ্লিকেশন অনেক বেশি মসৃণ এবং কার্যকরীভাবে চলতে থাকে।
AWT-এ Double Buffering স্বাভাবিকভাবে setDoubleBuffered(true) দিয়ে সক্রিয় করা হয়। তবে, আপনি কাস্টম paint() মেথডে এটি আরও কার্যকরীভাবে ব্যবহার করতে পারেন।
Double Buffering Implementation Example
import java.awt.*;
import java.awt.event.*;
public class DoubleBufferingExample extends Frame {
private int x = 50; // Position for the animated object
private int direction = 1; // Direction of movement: 1 means right, -1 means left
public DoubleBufferingExample() {
setTitle("Double Buffering Example");
setSize(500, 500);
setVisible(true);
// Start animation in a new thread
new Thread(new AnimationRunnable()).start();
}
@Override
public void paint(Graphics g) {
// Call the superclass paint method for double buffering
super.paint(g);
// Create a buffered image for off-screen drawing
Image offscreenImage = createImage(getWidth(), getHeight());
Graphics offscreenGraphics = offscreenImage.getGraphics();
// Perform drawing in the offscreen buffer
offscreenGraphics.setColor(Color.RED);
offscreenGraphics.fillRect(x, 200, 50, 50); // Draw animated object
// Now copy the off-screen image to the screen
g.drawImage(offscreenImage, 0, 0, this);
}
class AnimationRunnable implements Runnable {
@Override
public void run() {
while (true) {
// Update the position of the square
x += direction * 5;
// Reverse direction when the square hits the window boundaries
if (x > getWidth() - 50 || x < 0) {
direction = -direction;
}
// Repaint the frame to update the animation
repaint();
try {
Thread.sleep(50); // Control the speed of the animation
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new DoubleBufferingExample();
}
}
ব্যাখ্যা:
- BufferedImage ব্যবহার করে একটি অফ-স্ক্রীন ইমেজ তৈরি করা হয় যা গ্রাফিক্স আঁকার জন্য ব্যবহৃত হয়।
- paint() মেথডে:
- প্রথমে অফ-স্ক্রীনে আঁকা হয় এবং তারপর g.drawImage() এর মাধ্যমে স্ক্রীনে রেন্ডার করা হয়।
- DoubleBuffering-এর মাধ্যমে আঁকার সময় ফ্লিকারিং বা ঝাপসা দূর হয়, কারণ স্ক্রীনে আঁকার পরিবর্তে প্রথমে অফ-স্ক্রীনে আঁকা হয়।
Key Takeaways:
- Custom Animation:
Threadব্যবহার করে অবজেক্টের পজিশন আপডেট করুন এবংrepaint()কল করুন।paint()মেথডে গ্রাফিক্স আঁকার জন্য কাস্টম লজিক ব্যবহার করুন।
- Double Buffering:
- এপ্লিকেশনটি ফ্লিকারিং ছাড়া এবং মসৃণভাবে কাজ করবে যদি আপনি Double Buffering ব্যবহার করেন।
createImage()এবংdrawImage()ব্যবহার করে অফ-স্ক্রীন গ্রাফিক্স তৈরি করা এবং তারপর স্ক্রীনে রেন্ডার করা হয়।
এই পদ্ধতিগুলি আপনার AWT অ্যানিমেশন এবং গ্রাফিক্সের পারফরম্যান্স উন্নত করতে সাহায্য করবে, বিশেষ করে যখন অ্যানিমেশন বা দ্রুত পরিবর্তনশীল গ্রাফিক্সের সাথে কাজ করতে হবে।
Read more