#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    How do I fill graphic objects with letters from a text file


    I have a text file that has 5 letters vertically that spells
    H
    E
    L
    L
    O

    I want to place a letter into each graphic object. Running
    the code uploaded here, you get 5 ovals with

    b b b b b

    I want five ovals with:

    H E L L O

    with the letters retrieved from the text file.

    Any ideas?

    import java.awt.*;
    import javax.swing.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import javax.swing.JFrame;



    public class Ovals extends JPanel{

    public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.setBackground(Color.WHITE);
    Graphics2D g2d = (Graphics2D)g;

    int spacer=10;
    int screenRes = Toolkit.getDefaultToolkit().getScreenResolution();
    int fontSize = (int)Math.round(14.0 * screenRes / 72.0);

    Font font = new Font("Arial", Font.BOLD, fontSize);

    g2d.setFont(font);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


    for (int i=1; i < 6; i++) {

    g.setColor(Color.LIGHT_GRAY);
    g.fillOval(spacer, 3, 20, 30);

    g.setColor(Color.BLACK);
    g.drawOval(spacer, 3, 20 , 30);

    g.setColor(Color.BLACK);
    g2d.drawString(main.st, spacer + 5, 25);
    spacer = spacer+30;

    }}}




    public static String st ="b";

    public static void main(String[] args) {
    JFrame f = new JFrame("Ovals");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Ovals ov = new Ovals();
    f.add(ov);
    f.setSize(400,250);
    f.setVisible(true);

    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Put the letters in a String array and index into that array in the drawString() method.

IMN logo majestic logo threadwatch logo seochat tools logo