SoftwareEngineering

 

CSV

CSVを読み込む

CSVを書きこむ

XML

XMLを読み込む

絶対ロケーションパスを取得する

ファイル名から拡張子を除いたベース名を取得する

プロパティファイル

XML版

配列

String配列

歯抜けになっている中身を前詰する

public class RemoveBlankItems{
    
    public static void main(String args[]) throws Exception {
        String items[] = new String[30];
        
        for (int i = 0; i < items.length; i++) {
            items[i] = "";
        }
        
        items[5] = "5番目のデータ";
        items[10] = "10番目のデータ";
        items[15] = "15番目のデータ";
        items[20] = "20番目のデータ";
        items[25] = "25番目のデータ";
        
        System.out.println("---------------------------------------------------------------------------");
        System.out.println("-- 未処理");
        for (int i = 0; i < items.length; i++) {
            System.out.println("items[" + Integer.toString(i) + "]:[" + items[i] + "]");
        }
        
        removeBlankItems(items);
        
        System.out.println("---------------------------------------------------------------------------");
        System.out.println("-- 処理後");
        for (int i = 0; i < items.length; i++) {
            System.out.println("items[" + Integer.toString(i) + "]:[" + items[i] + "]");
        }
    }
    
    
    private static void removeBlankItems(String items[]) {
        String buffer[] = new String[items.length];
        
        // 配列の中身を退避
        for (int index = 0; index < items.length; index++) {
            buffer[index] = items[index];
        }
        
        // 前方に詰めたい配列の中身をクリア
        for (int index = 0; index < items.length; index++) {
            items[index] = "";
        }
        
        // 前方に詰める
        int itemIndex = 0;
        for (int bufferIndex = 0; bufferIndex < buffer.length; bufferIndex++) {
            if (buffer[bufferIndex] == null) continue;
            if (buffer[bufferIndex].equals("")) continue;
            
            items[itemIndex] = buffer[bufferIndex];
            itemIndex++;
        }
        
    }
    
}

画像

画像を回転させる

private Image rotate(Image sourceImage, int rotation) {
    int sourceWidth = sourceImage.getWidth(this);       // 幅(変換前の画像)
    int sourceHeight = sourceImage.getHeight(this);     // 高さ(変換後の画像)
    double angle = 0;                                   // 角度
    
    int targetWidth = sourceWidth;                      // 幅(変換後の画像)
    int targetHeight = sourceHeight;                    // 高さ(変換後の画像)
    
    double posX = 0;    // 中心点 X座標
    double posY = 0;    // 中心点 Y座標
    
    
    switch (rotation) {
        case 1: {
            // 右に90度回転する
            angle = Math.toRadians(90d);
            posX = (sourceWidth / 2) - ((sourceWidth - sourceHeight) / 2);
            posY = sourceHeight / 2;
            
            targetWidth = sourceHeight;
            targetHeight = sourceWidth;
            
            break;
        }
        
        case 2: {
            // 右に180度回転する
            angle = Math.toRadians(180d);
            posX = sourceWidth / 2;
            posY = sourceHeight / 2;
            
            targetWidth = sourceWidth;
            targetHeight = sourceHeight;
            
            break;
        }
        
        case 3: {
            // 右に270度回転する
            angle = Math.toRadians(270d);
            posX = sourceWidth / 2;
            posY = (sourceHeight / 2) + ((sourceWidth - sourceHeight) / 2);
            
            targetWidth = sourceHeight;
            targetHeight = sourceWidth;
            
            break;
        }
    }
    
    
    BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
    
    java.awt.geom.AffineTransform affin
        = java.awt.geom.AffineTransform.getRotateInstance(angle, posX, posY);
    
    Graphics2D g2 = (Graphics2D)targetImage.createGraphics();
    g2.drawImage(sourceImage, affin, null);
    
    return targetImage;
}

印刷処理(JDK 1.3)

ページの印刷方向に初期値を設定する方法

印刷ジョブのプロパティを変更するためのダイアログにページの印刷方向に初期値を設定する方法を以下に示します。 ダイアログで指定した印刷方向でページが印刷されます。

コード

import java.awt.*;
import java.awt.print.*;

public class DefaultOrientation implements Printable {
    private static Font fnt = new Font("Helvetica",Font.PLAIN,24);
    
    
    
    public static void main(String[] args) {
        PrinterJob job = PrinterJob.getPrinterJob();
        
        
        PageFormat defaultPage = job.defaultPage();
        defaultPage.setOrientation(PageFormat.LANDSCAPE);
        job.setPrintable(new DefaultOrientation(), defaultPage);    // Point-1
        
        
        if (job.printDialog()) {
            
            try {
                job.print();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
            
        }
        
    }
    
    
    
    public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
        if (5 <= pageIndex) return Printable.NO_SUCH_PAGE;
        
        g.setFont(fnt);
        g.setColor(Color.green);
        g.drawString("Page " + (pageIndex+1), 100, 100);
        
        return Printable.PAGE_EXISTS;
    }
    
}

実行結果

DefaultOrientation_0001.PNG

解説

注意事項/連絡事項

参考サイト


トップ   一覧 検索 最終更新   ヘルプ   最終更新のRSS