Custom Drawing এবং Painting Techniques

AWT Graphics এবং Drawing - এডাব্লিউটি (AWT) - Java Technologies

378

AWT (Abstract Window Toolkit) এ Custom Drawing এবং Painting Techniques ব্যবহার করা খুবই সাধারণ, যেখানে আপনি কাস্টম গ্রাফিক্স তৈরি করতে পারেন, যেমন শেপ, লাইন, টেক্সট, বা ছবি। এই কাজের জন্য Graphics ক্লাসটি ব্যবহৃত হয়, যা বিভিন্ন গ্রাফিক্যাল অউটপুট (যেমন শেপ, টেক্সট, ছবি, ইত্যাদি) তৈরি করার জন্য প্রয়োজনীয় মেথড সরবরাহ করে।

AWT তে কাস্টম ড্রয়িং করার জন্য, আপনি সাধারণত paint() অথবা paintComponent() মেথডকে ওভাররাইড করবেন। এই মেথডে আপনি আপনার গ্রাফিক্স কনটেন্ট ড্রয়িং করবেন।

Custom Drawing Techniques:

১. paint() মেথড ব্যবহার করে কাস্টম ড্রয়িং

AWT তে কাস্টম ড্রয়িং করার জন্য সাধারণত Canvas বা Panel এর paint() মেথড ব্যবহার করা হয়। এই মেথডে Graphics অবজেক্ট পাস করা হয়, যার মাধ্যমে আপনি বিভিন্ন গ্রাফিক্যাল উপাদান ড্রয়িং করতে পারেন।

উদাহরণ:

import java.awt.*;

public class CustomDrawingExample {
    public static void main(String[] args) {
        Frame frame = new Frame("AWT Custom Drawing");

        // Create a Canvas to draw custom graphics
        Canvas canvas = new Canvas() {
            public void paint(Graphics g) {
                // Set color for drawing
                g.setColor(Color.BLUE);
                
                // Draw a line
                g.drawLine(50, 50, 200, 50);

                // Draw a rectangle
                g.setColor(Color.RED);
                g.fillRect(50, 100, 150, 100);

                // Draw an oval
                g.setColor(Color.GREEN);
                g.fillOval(50, 250, 150, 100);

                // Draw text
                g.setColor(Color.BLACK);
                g.drawString("AWT Custom Drawing", 50, 400);
            }
        };

        // Set Canvas size and add to the Frame
        canvas.setSize(400, 500);
        frame.add(canvas);

        // Frame settings
        frame.setSize(400, 500);
        frame.setVisible(true);

        // Close window action
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent we) {
                System.exit(0);
            }
        });
    }
}

ব্যাখ্যা:

  • এখানে Canvas ক্লাসের paint() মেথডটি ওভাররাইড করা হয়েছে। এই মেথডে আমরা Graphics অবজেক্ট ব্যবহার করে বিভিন্ন শেপ যেমন লাইন, আয়তক্ষেত্র, বৃত্ত এবং টেক্সট ড্রয়িং করেছি।
  • Graphics.setColor() মেথড ব্যবহার করে আমরা ড্রইংয়ের জন্য রঙ নির্ধারণ করেছি।

২. Double Buffering ব্যবহার করা

একটি সাধারণ সমস্যা হল গ্রাফিক্স ড্রয়িং করার সময় স্ক্রীনে ঝাপসা বা ফ্লিকারিং দেখা দেয়। এর জন্য Double Buffering ব্যবহার করা হয়, যেখানে গ্রাফিক্স প্রথমে একটি buffered image-এ আঁকা হয় এবং পরে একবারে স্ক্রীনে দেখা যায়।

AWT তে ডাবল বাফারিং করার জন্য আপনি BufferStrategy ব্যবহার করতে পারেন। তবে, সাধারণ কাস্টম ড্রয়িংয়ের ক্ষেত্রে ডাবল বাফারিং ম্যানুয়ালি করা যায়। নিচে একটি উদাহরণ দেওয়া হলো:

Double Buffering উদাহরণ:

import java.awt.*;
import java.awt.image.BufferStrategy;

public class DoubleBufferingExample extends Canvas {
    public void paint(Graphics g) {
        // Get the buffer strategy
        BufferStrategy strategy = getBufferStrategy();

        if (strategy == null) {
            createBufferStrategy(2); // Create a double-buffered strategy
            return;
        }

        // Get the Graphics object from the back buffer
        Graphics2D g2d = (Graphics2D) strategy.getDrawGraphics();

        // Set the background color
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, getWidth(), getHeight());

        // Custom drawing
        g2d.setColor(Color.BLUE);
        g2d.fillOval(50, 50, 150, 100);

        // Complete drawing
        g2d.dispose();

        // Show the back buffer on the screen
        strategy.show();
    }

    public static void main(String[] args) {
        Frame frame = new Frame("AWT Double Buffering Example");

        DoubleBufferingExample canvas = new DoubleBufferingExample();
        frame.add(canvas);

        frame.setSize(400, 400);
        frame.setVisible(true);

        // Close window action
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent we) {
                System.exit(0);
            }
        });

        canvas.createBufferStrategy(2); // Create double buffer strategy
    }
}

ব্যাখ্যা:

  • এখানে ডাবল বাফারিং ব্যবহার করে কাস্টম ড্রইং করা হয়েছে। প্রথমে back buffer-এ আঁকা হচ্ছে এবং পরে একবারে স্ক্রীনে দেখানো হচ্ছে, যা স্ক্রীনের ঝাপসা দূর করে।
  • createBufferStrategy(2) দিয়ে ডাবল বাফারিং চালু করা হয়েছে।

৩. কাস্টম গ্রাফিক্স শেপস এবং টেক্সট

AWT তে গ্রাফিক্সের জন্য বিভিন্ন স্ট্যান্ডার্ড শেপ এবং টেক্সট ড্রয়িং মেথড রয়েছে:

  • drawLine(x1, y1, x2, y2): দুটি পয়েন্টের মধ্যে একটি রেখা আঁকা।
  • drawRect(x, y, width, height): একটি আয়তক্ষেত্র আঁকা (সীমারেখা সহ)।
  • fillRect(x, y, width, height): আয়তক্ষেত্র পূর্ণভাবে আঁকা।
  • drawOval(x, y, width, height): একটি বৃত্তের আউটলাইন আঁকা।
  • fillOval(x, y, width, height): বৃত্ত পূর্ণভাবে আঁকা।
  • drawString(string, x, y): একটি স্ট্রিং টেক্সট ড্রয়িং করা।

উদাহরণ:

import java.awt.*;

public class ShapeTextDrawing {
    public static void main(String[] args) {
        Frame frame = new Frame("Shape and Text Drawing");

        // Create a Canvas to draw custom graphics
        Canvas canvas = new Canvas() {
            public void paint(Graphics g) {
                // Draw some custom shapes and text
                g.setColor(Color.RED);
                g.drawRect(50, 50, 200, 100);  // Rectangle Outline
                g.fillRect(50, 200, 200, 100); // Filled Rectangle

                g.setColor(Color.GREEN);
                g.drawOval(50, 350, 200, 100);  // Oval Outline
                g.fillOval(50, 500, 200, 100);  // Filled Oval

                g.setColor(Color.BLACK);
                g.drawString("Custom Drawing in AWT", 50, 650); // Text Drawing
            }
        };

        // Set Canvas size and add to the Frame
        canvas.setSize(300, 700);
        frame.add(canvas);

        // Frame settings
        frame.setSize(400, 700);
        frame.setVisible(true);

        // Close window action
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent we) {
                System.exit(0);
            }
        });
    }
}

৪. Advanced Techniques (Images, Gradients, etc.)

AWT-তে images এবং gradients ড্রইং করা যায়। যেমন:

  • Images: drawImage() মেথড ব্যবহার করে ছবি আঁকা যেতে পারে।
  • Gradients: Graphics2D ব্যবহার করে গ্রেডিয়েন্ট পেইন্টিং করা যেতে পারে, যেমন linear gradient বা radial gradient
import java.awt.*;
import java.awt.event.*;

public class ImageAndGradientExample extends Canvas {
    public void paint(Graphics g) {
        // Use Graphics2D for advanced techniques
        Graphics2D g2d = (Graphics2D) g;

        // Draw Image (Make sure to load your image)
        Image img = Toolkit.getDefaultToolkit().getImage("path/to/image.jpg");
        g.drawImage(img, 50, 50, this);

        // Gradient Example
        GradientPaint gradient = new GradientPaint(50, 200, Color.RED, 200, 200, Color.YELLOW);
        g2d.setPaint(gradient);
        g2d.fillRect(50, 200, 200, 100);
    }

    public static void main(String[] args) {
        Frame frame = new Frame("AWT Image and Gradient Example");

        ImageAndGradientExample canvas = new ImageAndGradientExample();
        frame.add(canvas);

        frame.setSize(400, 400);
        frame.setVisible(true);

        // Close window action
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });
    }
}

ব্যাখ্যা:

  • এখানে একটি ইমেজ এবং gradient ব্যবহার করা হয়েছে Graphics2D এবং GradientPaint এর মাধ্যমে।

AWT তে কাস্টম ড্রয়িং করার জন্য Graphics ক্লাসের বিভিন্ন মেথড ব্যবহার করতে পারেন যেমন শেপ, টেক্সট, ছবি, এবং গ্রেডিয়েন্ট পেইন্টিং। এছাড়া ডাবল বাফারিং বা অ্যানিমেশন করতে চাইলে BufferStrategy ব্যবহার করা যায়। AWT তে স্টাইলিং এবং গ্রাফিক্সের ক্ষেত্রে কিছু সীমাবদ্ধতা থাকতে পারে, তবে আপনি সৃজনশীলভাবে বিভিন্ন গ্রাফিক্যাল এলিমেন্ট তৈরি করতে পারেন।

Content added By
Promotion

Are you sure to start over?

Loading...