I am new to java grahics2D. I want to create it like where user can write as drawing or having keyboard buttons to draw the characters. I was looking over the internet and managed to do the paint section.
I started testing the drawstring with Arabic characters. Now how can I set the direction and make the characters connected...I only have 3 letters for testing.
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();
}
}
If you draw the characters one by one, Java2D won't know any better than to draw each character in the isolate form: ﺏ ﺏ ﺏ
If you want several characters to be rendered as one word, e.g. ببب, you have to draw the entire string at once.