3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Processingで自動改行ソフトを作る。

Last updated at Posted at 2021-04-01

Processing_Auto-line-breaker

自動改行ソフトを作りました。
テキストボックス内ですべての処理を行っているため、ファイルに依存しません。(ファイル制御に挫折しただけ)
ezgif.com-gif-maker.gif

環境と経緯

OS Win10
開発環境 Processing3.5.4

ゴミパソと10年前のイラストレーターを片手に、手動改行で頑張っている新聞部に寄贈した物です。

初投稿のため見るに堪えない点が多いかと思いますが、温かい目で見守ってください。

コード

ALB_ver1.0
//ALB ver1.0
import javax.swing.*;
import java.awt.*;

JLayeredPane pane;
JTextField fbox1;
JTextField fbox2;
JTextArea abox1;

int textlong;

void setup() {
  size(200, 200);
  // pane
  Canvas canvas = (Canvas) surface.getNative();
  pane = (JLayeredPane) canvas.getParent().getParent();
  // filed
  fbox1 = new JTextField();
  fbox1.setBounds(10, 10, 50, 30);
  pane.add(fbox1);
  // area
  abox1 = new JTextArea();
  abox1.setLineWrap(true);
  abox1.setWrapStyleWord(true);
  JScrollPane scrollPane = new JScrollPane(abox1);
  scrollPane.setBounds(0, 50, width, height-50);
  pane.add(scrollPane);
  //rect
  fill(128);
  rect(70, 10, 120, 30);
}

void draw() {
}

void mousePressed() {
  if ((mouseX>70)&&(mouseX<70+120)&&(mouseY>10)&&(mouseY<10+30)) {
    abox1.setEditable(false);
    fbox1.setEditable(false);
    //set last
    abox1.setCaretPosition(abox1.getText().length());
    //delite newline
    for (int i=0; i<abox1.getSelectionEnd(); i++) {
      try {
        //println(abox1.getText(i, 1));
        if (abox1.getText(i, 1).equals("\n")) {
          //println("change");
          abox1.replaceRange("", i, i+1);
        }
      }
      catch(Exception e) {
        //println("bad");
      }
    }
    //try input fbox1
    try {
      //success
      //insert newline for right pos
      textlong=Integer.parseInt(fbox1.getText());
      for (int i=1, j=0; i<abox1.getSelectionEnd()+j; i++) {
        if (i==textlong) {
          abox1.insert("\n", i);
          j++;
        } else if (i==textlong*(j+1)+j) {
          abox1.insert("\n", i);
          j++;
        }
      }
    }
    catch(Exception e) {
      //fail
      //only delite newline
    }
    abox1.setEditable(true);
    fbox1.setEditable(true);
  }
}

解説

カーネル的なとこ
    //set last
    abox1.setCaretPosition(abox1.getText().length());
    //delite newline
    for (int i=0; i<abox1.getSelectionEnd(); i++) {
      try {
        //println(abox1.getText(i, 1));
        if (abox1.getText(i, 1).equals("\n")) {
          //println("change");
          abox1.replaceRange("", i, i+1);
        }
      }
      catch(Exception e) {
        //println("bad");
      }
    }
    //try input fbox1
    try {
      //success
      //insert newline for right pos
      textlong=Integer.parseInt(fbox1.getText());
      for (int i=1, j=0; i<abox1.getSelectionEnd()+j; i++) {
        if (i==textlong) {
          abox1.insert("\n", i);
          j++;
        } else if (i==textlong*(j+1)+j) {
          abox1.insert("\n", i);
          j++;
        }
      }
    }
    catch(Exception e) {
      //fail
      //only delite newline
    }

アルゴリズムは無理やり改行コードを全削除して、指定字数の場所に改行コードを入れていく仕組みです。

改行コードの挿入について

CCF_000098.jpg
例えば、5文字ごとに改行する場合。
最初の改行位置は5文字目
二回目の改行位置は11文字目
三回目の改行位置は17文字目
・・・
と、「指定字数※n=1」と「指定字数*(n-1)+n※n>=2」となっています。

おそらく、もっとスマートな方法があるはずなのですが小さい頭で考えた結果こうなりました。

参考
あえて言うほどではない 数値 ⇔ 文字列変換 Java編
Processing テキストボックスで日本語入力をする
Swingを使ってみよう

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?