drawString with arabic characters-java

时间:2014-06-26 14:43:03

标签: java swing arabic drawstring

我是java grahics2D的新手。我想创建它,就像用户可以写为绘图或用键盘按钮绘制字符。我正在浏览互联网并设法完成油漆部分。

我开始用阿拉伯字符测试抽绳。现在我如何设置方向并使字符连接......我只有3个字母用于测试。


package pa;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class paint {
    public static JFrame frame;
    public static boolean erase = false;
    public static boolean isChar = false;

    public static void main(String[] args) {

        frame = new JFrame("Paint It");

        frame.setDefaultCloseOperation(0);
        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                int result = JOptionPane.showConfirmDialog(frame,
                        "Exit the application?");
                if (result == JOptionPane.OK_OPTION) {
                    System.exit(0);
                }
            }

        });

        Container content = frame.getContentPane();

        content.setLayout(new BorderLayout());

        final PadDraw drawPad = new PadDraw();

        content.add(drawPad, BorderLayout.CENTER);

        JPanel panel = new JPanel();

        panel.setPreferredSize(new Dimension(32, 68));
        panel.setMinimumSize(new Dimension(32, 68));
        panel.setMaximumSize(new Dimension(32, 68));

        JButton clearButton = new JButton("Clear");

        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                drawPad.clear();
            }
        });

        JButton eraseButton = new JButton("Erase");
        eraseButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {

                drawPad.setErase(true);
            }
        });

        JButton alef = new JButton("\u0623");
        alef.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                drawPad.setDraw(true);
                drawPad.setErase(false);
                drawPad.drawChar("\u0623");

            }

        });

        JButton baa = new JButton("\u0628");
        baa.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                drawPad.setDraw(true);
                drawPad.setErase(false);
                drawPad.drawChar("\u0628");

            }

        });

        JButton taa = new JButton("\u062a");
        taa.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                drawPad.setDraw(true);
                drawPad.setErase(false);
                drawPad.drawChar("\u062a");

            }

        });

        JButton redButton = new JButton("Draw");

        redButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                drawPad.setDraw(false);
                drawPad.setErase(false);

                drawPad.black();
            }

        });

        panel.add(redButton);
        panel.add(clearButton);
        panel.add(eraseButton);
        panel.add(alef);
        panel.add(baa);
        panel.add(taa);

        content.add(panel, BorderLayout.WEST);

        frame.setSize(300, 300);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }
}

class PadDraw extends JComponent {
    Image image;
    private int xTxt, yTxt, xTemp, yTemp;
    private int xOff = 0;
    private boolean erase = false;
    private boolean isChar = false;

    public void setErase(boolean flag) {
        this.erase = flag;
    }

    public void setDraw(boolean drawType) {
        this.isChar = drawType;
    }

    Graphics2D graphics2D;

    int currentX, currentY, oldX, oldY;

    public PadDraw() {

        if (!isChar) {

            setDoubleBuffered(false);
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    oldX = e.getX();
                    oldY = e.getY();

                }
            });

            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    if (!erase) {
                        // graphics2D.setStroke(new BasicStroke(1));

                        graphics2D.setPaint(Color.black);
                        currentX = e.getX();
                        currentY = e.getY();
                        if (graphics2D != null)
                            graphics2D.drawLine(oldX, oldY, currentX, currentY);
                        repaint();
                        oldX = currentX;
                        oldY = currentY;
                    }
                    currentX = e.getX();
                    currentY = e.getY();
                    if (graphics2D != null)
                        graphics2D.setPaint(Color.WHITE);
                    // graphics2D.setStroke(new BasicStroke(10));
                    graphics2D.drawLine(oldX, oldY, currentX, currentY);
                    repaint();

                }

            });

        }
    }

    public void paintComponent(Graphics g) {

        if (image == null) {
            image = createImage(getSize().width, getSize().height);
            graphics2D = (Graphics2D) image.getGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            clear();

        }
        g.drawImage(image, 0, 0, null);
    }

    public void clear() {
        graphics2D.setPaint(Color.white);
        graphics2D.fillRect(0, 0, getSize().width, getSize().height);
        graphics2D.setPaint(Color.black);

        repaint();
    }

    public void drawChar(String x) {
        Font font = new Font("Serif", Font.PLAIN, 12);
        graphics2D.setFont(font);

        java.awt.font.FontRenderContext frc = graphics2D.getFontRenderContext();
        java.awt.font.TextLayout layout = new java.awt.font.TextLayout(x, font,
                frc);
        layout.draw(graphics2D, 20 + xOff, 55);

        repaint();
        xOff += 10;
        System.out.println(xOff);

    }

    // this is the red paint
    public void black() {
        graphics2D.setPaint(Color.black);
        repaint();
    }

}

1 个答案:

答案 0 :(得分:2)

如果你逐个绘制字符,Java2D将不会比在隔离窗体中绘制每个字符更好:ببب

如果您想将多个字符呈现为一个字,例如ببب,你必须立刻绘制整个字符串。