21 Rectangle & Rectangle2D 1 ウィンドウに矩形を描く方法を紹介します のに Rectangle2D.Float(float x, float y, float w, float h) があります が Python では double と float との違いを意識する必要はありません void drawrect(int x, int y, int width, int height) は 2点 (x,y) および (x+width,y+height) で囲まれた矩形領域に 矩形の 外枠 を描きます 同様に void fillrect(int x, int y, int width, int height) は 同じ矩 形領域に 矩形の 内部 を描きます 関数 paint には パネル panel 内に図形を描く方法を記述します 描画属性 g.color に指定した色を使って drawrect は 外枠 を fillrect は 内部 を 描きます from java.awt.geom import Rectangle2D x, y, w, h = 10, 10, 90, 90 shape = Rectangle2D.Double(x+200, y, w, h) g.drawrect(x, y, w, h) g.fillrect(x+100, y, w, h) size=(310, 140), title="rectangle & Rectangle2D") Rectangle2D.Double(double x, double y, double w, double h) は 2 点 (x,y) および (x+w,y+h) で囲まれた矩形領域に 矩形 を生成します 同様のも ひよ子のきもち 2007/02/05
(x,y)(x+w,y+h) CanvasPanelJPanel paint x, y, w, h = 10, 10, 40, 40 shape = Rectangle2D.Double(x, y, w, h) CanvasPanel.paint(panel,g)CanvasPanel paintpanelpaint panel.paint=paint CanvasPanelJPanelpaint def setframe(e): shape.setframe(x+100, y, w-10, h-20) panel.repaint() button = JPanel(layout=GridLayout(1, 0)) button.add(jbutton("setframe", actionperformed=setframe)) size=(160, 110), title="rectangle & Rectangle2D", layout=borderlayout()) frame.add(panel, BorderLayout.CENTER) frame.add(button, BorderLayout.SOUTH) void setframe(double x, double y, double w, double h) TypeError: can't assign to this attribute in java instance: paint TypeError
23 Oval & Ellipse 1 ウィンドウに楕円を描く方法を紹介します に Ellipse2D.Float(float x, float y, float w, float h) がありますが Python では double と float との違いを意識する必要はありません void drawoval(int x, int y, int width, int height) は 2点 (x,y) および (x+width,y+height) で囲まれた矩形領域に 楕円の 外枠 を描きます 同様に void filloval(int x, int y, int width, int height) は 同じ矩 形領域に 楕円の 内部 を描きます 関数 paint には パネル panel 内に図形を描く方法を記述します 描画属性 g.color に指定した色を使って drawrect は 外枠 を fillrect は 内部 を 描きます from java.awt.geom import Ellipse2D x, y, w, h = 10, 10, 90, 90 shape = Ellipse2D.Double(x+200, y, w, h) g.drawoval(x, y, w, h) g.filloval(x+100, y, w, h) size=(310, 140), title="oval & Ellipse") Ellipse2D.Double(double x, double y, double w, double h) は 2点 (x,y) および (x+w,y+h) で囲まれた矩形領域に 楕円 を生成します 同様のもの ひよ子のきもち 2007/02/07
(x,y)(x+w,y+h) CanvasPanelJPanel paint x, y, w, h = 10, 10, 40, 40 shape = Ellipse2D.Double(x, y, w, h) CanvasPanel.paint(panel,g)CanvasPanel paintpanelpaint panel.paint=paint CanvasPanelJPanelpaint def setframe(e): shape.setframe(x+100, y, w-10, h-20) panel.repaint() button = JPanel(layout=GridLayout(1, 0)) button.add(jbutton("setframe", actionperformed=setframe)) size=(160, 110), title="oval & Ellipse", layout=borderlayout()) frame.add(panel, BorderLayout.CENTER) frame.add(button, BorderLayout.SOUTH) void setframe(double x, double y, double w, double h) TypeError: can't assign to this attribute in java instance: paint TypeError
25 Polygon 1 ウィンドウに多角形を描く方法を紹介します size=(310, 140), title="polygon") Polygon(int[] xpoints, int[] ypoints, int npoints) は 多角形を生成 します xpoints は各頂点の x 座標を ypoints は各頂点の y 座標を列挙したもの です npoints は 頂点の数です 多角形の頂点は x 座標および y 座標を分けて指 定します そのため xy 座標が1つのオブジェクトとして表現されているときには これらを分ける必要があります また 頂点の数は自動的に判別されないので これを 明示する必要があります void drawpolygon(polygon p) は 多角形 p の 外枠 を描きます void fillpolygon(polygon p) は 多角形 p の 内部 を描きます from java.awt import Polygon vertices = (10, 40), (100, 40), (25, 100), (55, 10), (85, 100) xpoints = [x for x, y in vertices] ypoints = [y for x, y in vertices] npoints = len(vertices) shape1 = Polygon(xpoints, ypoints, npoints) shape2 = Polygon( [x+100 for x in xpoints], ypoints, npoints) shape3 = Polygon( [x+200 for x in xpoints], ypoints, npoints) g.drawpolygon(shape1) g.fillpolygon(shape2) g.fill(shape3) stroke = BasicStroke( 3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND) g.fill(stroke.createstrokedshape(shape3)) 関数 paint には パネル panel 内に図形を描く方法を記述します まず drawpolygon を使って 多角形の外枠を黒 Color.black で描きます 次に fillpolygon を使って 多角形の内部を赤 Color.red で描きます さらに fill を使って 多角形の内部だけを黄 Color.yellow で描きます 最後に fill を使っ て 多角形の外枠 幅 3 画素 に相当する部分を青 Color.blue で描きます ひよ子のきもち 2007/02/09