Results 1 to 3 of 3
  1. #1
    FunkyJustice is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Text to Colored Dots: what is the most efficient way to accomplish this?

    For the last few weeks I've been trying to program a nifty little idea I came up with: using a Scanner, I want to read a text file, create an array of all of the characters, and then print out an image that translates the letters into different colored dots. So far, I've been able to convert all of the chars to int values that have unique values between 1 and 255. However, I encountered a roadblock trying to display these dots on a JPanel and deleted the entire project in frustration. Going at this a second time, does anyone have any tips or ideas on how I could most efficiently go about this? The end result would be a JPanel that reads a text file, converts all of the chars to int values, and then displays dots with said int values for their colors to represent the text, one dot for each letter.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Text to Colored Dots: what is the most efficient way to accomplish this?

    It's hard for me to know what to suggest as I'm not really sure where you're stuck. As per usual, it is much easier to solve this by breaking down the program creation into individual small pieces and trying to solve each piece one at a time. Can you tell us more about what you've done and what doesn't work?

  3. #3
    FunkyJustice is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Text to Colored Dots: what is the most efficient way to accomplish this?

    So currently this is my code. I have succeeded in converting each token into an array of integers. Numbers, punctuation, etc. can be added later. Currently, I am trying to create a system of generating the coordinates of where the dots will be placed on a panel.

    Java Code:
    package texttodotv3;
    
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.util.Scanner;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     *
     * @author David
     */
    public class TexttoDotV3 {
    private Scanner scans;
    private String input;
    public TexttoDotV3()
    {
        input = "my name is david";
        scans = new Scanner(input);
    }
        public int[] Give()
        {
            Dot dot = new Dot();
            int[] dots = (dot.StringToDot(scans.next()));
            for(int x = 0; x < dots.length; x++)
            {
                System.out.println(dots[x]);
            }
            return dots;
        
        }
        public static void main(String[] args) {
    TexttoDotV3 printer = new TexttoDotV3();
    printer.Give();
    }
    
        public class Frame extends JFrame{
            private static final int FRAME_HEIGHT = 500;
            private static final int FRAME_WIDTH = 500;
            private Frame.Panel panel;
            
            public Frame(){
                panel = new Frame.Panel();
            }
        
        public class Panel extends JPanel{
            private final int PANEL_HEIGHT = 500;
            private final int PANEL_WIDTH = 500;
            private int X;
                
            public Panel()
            {
                setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
            }    
            
            public void PaintComponent(Graphics g)
            {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
            }
        }
        }
    }
    
    #
    
    public class Dot {
        public int[] StringToDot (String letters){
            int[]numbers = new int[letters.length()];
            for(int x = 0;x<letters.length();x++)
            {
                
                if("a".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 1;
                }
                if("b".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 2;
                }
                if("c".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 3;
                }
                if("d".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 4;
                }
                if("e".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 5;
                }
                if("f".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 6;
                }
                if("g".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 7;
                }
                if("h".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 8;
                }
                if("i".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 9;
                }
                if("j".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 10;
                }
                if("k".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 11;
                }
                if("l".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 12;
                }
                if("m".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 13;
                }
                if("n".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 14;
                }
                if("o".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 15;
                }
                if("p".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 16;
                }
                if("q".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 17;
                }
                if("r".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 18;
                }
                if("s".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 19;
                }
                if("t".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 20;
                }
                if("u".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 21;
                }
                if("v".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 22;
                }
                if("w".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 23;
                }
                if("x".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 24;
                }
                if("y".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 25;
                }
                if("z".equals(letters.substring(x, x+1)))
                {
                    numbers[x] = 26;
                }
            }
            return numbers;
                
                
                
                
            }
        }
    
    import java.awt.Color;
    import java.awt.Graphics2D;
    
    
    /**
     *
     * @author David
     */
    public class DotDisplay{
        private int letter;
        private int xStart;
        private int yStart;
        private Color[] colors = new Color[26];
        public DotDisplay(int x, int y, int l)
        {
            letter = l;
            xStart = x;
            yStart = y;
        }
        public void makeColors()
        {
            colors[1] = new Color(255,0,0);
            colors[2] = new Color(230,25,0);
            colors[3] = new Color(205,50,0);
            colors[4] = new Color(150,150,0);
            colors[5] = new Color(100,200,200);
            colors[6] = new Color(0,255,255);
            colors[7] = new Color(100,150,100);
            colors[8] = new Color(255,0,255);
            colors[9] = new Color(50,25,180);
            colors[10] = new Color(200,100,200);
            colors[11] = new Color(120,255,30);
            colors[12] = new Color(0,255,0);
            colors[13] = new Color(25,50,50);
            colors[14] = new Color(100,0,100);
            colors[15] = new Color(100,100,0);
            colors[16] = new Color(0,100,100);
            colors[17] = new Color(100,0,0);
            colors[18] = new Color(0,100,0);
            colors[19] = new Color(0,0,100);
            colors[20] = new Color(40,0,0);
            colors[21] = new Color(0,40,0);
            colors[22] = new Color(0,0,40);
            colors[23] = new Color(40,0,40);
            colors[24] = new Color(40,40,0);
            colors[25] = new Color(0,40,40);
            colors[26] = new Color(0,0,255);
        }
        public void draw(Graphics2D g2)
        {
            g2.setColor(colors[letter]);
            g2.fillOval(xStart, yStart, 10, 10);
        }
        
                
    }
    This is very unfinished, mind you, but it is correctly converting the letters to ints, and I am unsure how to complete the functions in a way that will print the dots on a JPanel left to right, starting at the top left of the panel, with a space between each token. The whole swing GUI section is not working at the moment, but that can be easily worked out once the dot printing function is complete. I'm finding it particularly hard to create a function that will account for finding when the next dot should be on a new line, and thus have a new Y value. I'm also unsure of how to proceed when, in the case of converting a large text, the panel is completely filled with dots, and some sort of scroll function will have to be put in place. Any suggestions on streamlining/completing this code would be greatly appreciated.

Similar Threads

  1. ASCII art, drawing an asterisks diamond within dots.
    By ajknight7498 in forum New To Java
    Replies: 3
    Last Post: 11-11-2011, 12:20 AM
  2. colored text with System.out.println
    By novice in forum New To Java
    Replies: 4
    Last Post: 07-08-2011, 10:48 PM
  3. Drawing dots.
    By hellolleh in forum NetBeans
    Replies: 0
    Last Post: 03-16-2010, 09:09 PM
  4. A more efficient way to read from a text file
    By matzahboy in forum New To Java
    Replies: 13
    Last Post: 10-14-2009, 12:35 PM
  5. Replies: 2
    Last Post: 08-10-2007, 01:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •