Geometry 関数
NSBundle
NSDictionary
NSFileManager
NSMutableAttributedString
NSNumber
NSScanner
NSString
NSTimer
Graphic 関数
-グラフィック関係の関数
-四角を描く
-四角で塗りつぶす
-警告音をならす
NSApplication
NSBezierPath
NSCell
NSColor
NSCursor
NSCustomImageRep
NSDocument
NSDocumentController
NSDragging
NSEvent
NSFontManager
NSGraphicsContext
NSImage
NSMenu
NSOutlineView
NSPanel
NSPopUpButton
NSResponder
NSScrollView
NSString 追加
NSTableColumn
NSTableView
NSTextStorage
NSTextView
NSToolbar
NSView
NSWindow
NSWorkspace
その他
.nib ウィンドウ
Views パレット
クラス
インスタンス変数
メソッド
CFXML
Carbon Event
Carbon Graphics
Cocoa で日本語
メソッド
その他

- Application Kit-
Graphic 関数

Application Kit - Graphic 関数
グラフィック関係の関数
Keywords: NSGraphics

Application Kit はいろいろな関数を提供しているけど、ここではグラフィック関係のものをまとめてみた。宣言は、ヘッダファイル AppKit/NSGraphics.h の中にある。主な内容は、グラフィック関係の定数、グラフィック環境を調べる関数、線や四角を描画する関数、などだ。NSBeazierPath と機能がかぶるものもある。


Application Kit - Graphic 関数
四角を描く
Keywords: NSFrameRect, NSFrameRectWithWidth

View に四角を描くいちばん手っ取り早い方法は、NSFrameRect() を使うことだ。

Application Kit/NSGraphics.h
APPKIT_EXTERN void NSFrameRect(NSRect rect);
APPKIT_EXTERN void NSFrameRectWithWidth(
                NSRect rect, float frameWidth);

現在の線の太さで描きたいときは NSFrameRect()、線の太さを指定したいときは NSFrameRectWithWidth() だ。

GraphicFunctions/frameRectView.m (sample)
- (void)testNSFrameRect:(NSRect)frameRect
{
    NSRect rect = NSMakeRect(50, 50, 80, 80);
    
    // Call NSFrameRect
    NSFrameRect(rect);
}

これだけで OK だ。現在の Graphic Context の色と線の太さで描かれる。

線の太さを指定する NSFrameRectWithWidth の呼び出し方は、こんな感じになる。

GraphicFunctions/frameRectView.m (sample)
- (void)testNSFrameRectWithWidth:(NSRect)frameRect
{
    NSRect	rect = NSMakeRect(250, 10, 50, 50);
    
    // Call NSFrameRectWithWidth, width = 1.0
    NSFrameRectWithWidth(rect, 1.0);
    
    // Call NSFrameRectWithWidth, width = 1.5
    rect.origin.y += 80;
    NSFrameRectWithWidth(rect, 1.5);
    
    // Call NSFrameRectWithWidth, width = 5.0
    rect.origin.y += 80;
    NSFrameRectWithWidth(rect, 5.0);
}

NSFrameRect sample実行結果は、右の図のようになる。下から順に、線の太さ 1.0、1.5、5.0 だ。1.5 を指定したときは、アンチエイリアシングがかかることに注意。また 5.0 を見ると分かるけど、太い線を描くときは、指定した rect の内側に向かって太くなる。

■関連リンク:
四角を描く(NSBezierPath)

■サンプルダウンロード:
GraphicFunctions.tar.gz


Application Kit - Graphic 関数
四角で塗りつぶす
Keywords: NSRectFill

では、四角で塗りつぶす場合は?NSRectFill() を使う。

Application Kit/NSGraphics.h
APPKIT_EXTERN void NSRectFill(
                NSRect rect);
APPKIT_EXTERN void NSRectFillList(
                const NSRect *rects, 
                int count);
APPKIT_EXTERN void NSRectFillListWithGrays(
                const NSRect *rects, 
                const float *grays, 
                int num);
APPKIT_EXTERN void NSRectFillListWithColors(
                const NSRect *rects, 
                NSColor **colors, 
                int num);

これらを使うと、現在の Graphics Context の色で塗りつぶされるんだ。いちばん単純なのが NSRectFill()。引き数に指定した rect を塗りつぶす。NSRectFillList() は、複数の rect を一気に描ける。引き数に NSRect の配列と、その数を渡すことになる。NSRectFillListWithGrays() は、複数の rect を、グレーの色を指定して描く。

GraphicFunctions/fillRectView.m (sample)
- (void)testNSRectFillListWithGrays:(NSRect)frameRect
{
    NSRect rect = NSMakeRect(160, 10, 50, 20);
    int  rectCount = 10;
    NSRect rectList[rectCount];
    float grays[rectCount];
    int i;
    
    // Set rectList
    for(i = 0; i < rectCount; i++) {
        *(rectList + i) = rect;
        rect.origin.y += rect.size.height;
    }
    
    // Set grays
    for(i = 0; i < rectCount; i++) {
        grays[i] = 0.1 * i;
    }
    
    // Call NSRectFillListWithGrays
    NSRectFillListWithGrays(rectList, grays, rectCount);
}

グレーの色は、float の配列で指定する。値は 0.0 から、1.0 の間で指定する。グレーではなくて好きな色を指定するには、NSRectFillListWithColors() を使おう。

GraphicFunctions/fillRectView.m (sample)
- (void)testNSRectFillListWithColors:(NSRect)frameRect
{
    NSRect	rect = NSMakeRect(240, 10, 50, 10);
    int		rectCount = 20;
    NSRect	rectList[rectCount];
    NSColor*	colors[rectCount];
    int		i;
    
    // Set rectList
    for(i = 0; i < rectCount; i++) {
        *(rectList + i) = rect;
        rect.origin.y += rect.size.height;
    }
    
    // Set color
    for(i = 0; i < rectCount; i++) {
        colors[i] = 
            [NSColor colorWithDeviceRed:(
                            (rectCount - i) * 0.05) 
                        green:(i * 0.05) 
                        blue:0.0 
                        alpha:1.0];
    }
    
    // Call NSRectFillListWithColors
    NSRectFillListWithColors(rectList, colors, rectCount);
}

NSFillRect sample 実行結果は、右の図のようになる。

こういう四角を描く処理は、NSBeazierPath でも行える。だけど、こちらの方が手軽だし、状況に応じて使い分けよう。

■関連リンク:
四角を描く(NSBezierPath)

■サンプルダウンロード:
GraphicFunctions.tar.gz


Application Kit - Graphic 関数
警告音をならす
Keywords: NSBeep

警告音をならすには、NSBeep() を使う。

Application Kit/NSGraphics.h
APPKIT_EXTERN void NSBeep();

サウンド関係なのに、なぜか NSGraphics.h に含まれている。これを使うと、System Preferences で設定した警告音を鳴らすことができる。



[Home] [Download] [Archives] [BBS] [Cocoa Programming Tips 1001] [Core Foundation の秘密] [Safari Developer Center] [はじめてのブラウザのつくり方] [Sketch BP] [スクリーンセイバーを作ろう] [Objective-C 最適化] [Authorization API 完全理解] [Mac OS X Programming Books Review] [オブジェクト指向の言語比較論] [panther-dev]

mailto: mkino@xd5.so-net.ne.jp