gpt4 book ai didi

java - 带有阿拉伯字符的drawString-java

转载 作者:行者123 更新时间:2023-11-30 09:06:29 25 4
gpt4 key购买 nike

我是 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();
}

}

最佳答案

如果你一个一个地绘制字符,Java2D 不会比以隔离形式绘制每个字符更好:ﺏ ﺏ ﺏ

如果您希望将多个字符呈现为一个单词,例如ببب,你必须一次画出整个字符串。

关于java - 带有阿拉伯字符的drawString-java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24433512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com