litchi 0 Light Poster

Hi :)

I am writing an application that converts text to braille for a varsity project. I got it working by using a library given to us by our professor, but I am trying to understand the code I am using, so I am re-writing the code and omitting any "unnecessary" code, i.e. condensing it and changing it to work from inside my file/application.

I have tried my best to use the code "extracted" from the library, but it won't draw anything. I have never used Graphics2D or BufferedImage, but I think how it works is: It draws on an image, inserts it into a label and adds it to offscreen image, and then swaps it with the onscreen image.

Could someone be so kind as to help me find out what is stopping my code from drawing?

ps. To see what it should look like, compile and run MainF.java With the rest of the files in the same directory, enter some text, click the button and Viola! :) **Warning** The code is very messy, but it is for testing purposes.

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.util.LinkedList;

public final class MainFrame_exp{

    private static MainFrame std = new MainFrame();
    public static final Color WHITE      = Color.WHITE;
    public static final Color BLACK      = Color.BLACK;

    private static JFrame frame;
    private static BufferedImage offscreenImage, onscreenImage;
    private static Graphics2D offscreen, onscreen;

    static { init(); }

    private static void init() 
    {
		frame = new JFrame();
		offscreenImage = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
		onscreenImage  = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
		offscreen = offscreenImage.createGraphics();
		onscreen  = onscreenImage.createGraphics();
		offscreen.setColor(WHITE);
		offscreen.fillRect(0, 0, 512, 512);
	      
		clear();

		// not sure what this does? O_o **start**
		RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
							  RenderingHints.VALUE_ANTIALIAS_ON);
		hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		offscreen.addRenderingHints(hints);
		// **stop**
		
		ImageIcon icon = new ImageIcon(onscreenImage);
		JLabel draw = new JLabel(icon);

		frame.setContentPane(draw);
		
		frame.setResizable(false);
		
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);      // closes only current window
		frame.setTitle("Standard Draw");
	
		frame.pack();
	
		frame.setVisible(true);
    }

    public static void clear() { clear(WHITE); }
    public static void clear(Color color) 
    {
	    offscreen.setColor(color);
	    offscreen.fillRect(0, 0, 512, 512);
	    offscreen.setColor(WHITE);
	    draw();
    }

    private static void draw() 
    {
        onscreen.drawImage(offscreenImage, 0, 0, null);
        frame.repaint();
    }

    public static void braille(int a, int b, int c, int d, int e, int f, int g, int h, int j, int k)
    {
//this part works, it can be ignored. **start**
	int[] arr = {a, b, c, d, e, f, g, h};
	double[][] arr2 = {
			{10, 1125},
			{10, 1105},
			{10, 1085},
			{30, 1125},
			{30, 1105},
    			{30, 1085},
    			{10, 1065},
    			{30, 1065},};
	for (int i = 0; i < 8; i++)
	{
		if (arr[i] == 1)
		  {
		      double xs = 512  * (arr2[i][0]+(j*50) + 530) / 2120;
		      double ys = 512 * (1191.75-arr2[i][1]-(k*100)) / 1248.5;
		      double ws = 4096 / Math.abs(2120);
		      double hs = 4096 / Math.abs(1248.5);
		      offscreen.fill(new Ellipse2D.Double(xs - ws/2, ys - hs/2, ws, hs));
		      draw();
		  }
		else if (arr[i] == 0)
		  {
		      double xs = 512  * (arr2[i][0]+(j*50) + 530) / (2120);
		      double ys = 512 * (1191.75-arr2[i][1]-(k*100)) / 1248.5;
		      double r = 0.0015;
		      offscreen.fill(new Ellipse2D.Double(xs - r/2, ys - r/2, r, r));
		      draw();
		  }
	}//**stop**

    }

    public static void main (String[] args){
      braille(1,0,0,0,0,0,0,0,1,2);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.