9 2013 6 11 9.1 JFC Swing.................................... 9 1 9.2 (Graphics )................. 9 2 9.3.................................. 9 4 9.4 (Color ).............................. 9 6 9.5....................................... 9 8 9.1 JFC Swing Java Java (GUI) Java Foundation Classes (JFC) JFC Swing GUI 1 2 ( ) JFC Card Pile GameFrame GUI GamePanel ( ) JMenuBar ) GUI Swing JFC Swing GUI (Toolkit) javax.swing GUI JFrame JWindow JDailog 1 9 1
JComponent JPanel JMenuBar JToolBar JLabel JButton JCheckBox JRaidoButton JMenu JMenuItem. ( ) on/off 1. Swing 2 Swing GameFrame GamePanel Card Pile JFrame JPanel JComponent JComponent Deck Pile paintcomponent Swing GUI JComponent JComponent 1 protected void paintcomponent(java.awt.graphics g) JComponent ( ) 9.2 (Graphics ) GUI ( ) paintcomponent java.awt.graphics 3 2 JMenuBar JMenu JMenuItem 3 Graphics Graphics2D Graphics2D Graphics paintcomponent Graphics Swing AWT (Abstract Window Toolkit) 9 2
Graphics ( ) ( ) (graphics contexts) ( ) Graphics 2 ColorFont java.awt 4 Graphics Graphics create() void translate(int x, int y) void setcolor(color c) Color getcolor() void setfont(font f) Font getfont() ( ) (x, y) c f void drawstring(string s, int x, int y) void drawline(int x1, int y1, int x2, int y2) void drawrect(int x, int y, void drawroundrect(int x, int y, int w, int h, int aw, int ah) void drawoval(int x, int y, void drawarc(int x, int y, int w, int h, int s, int e) void drawpolyline(int[] x, int[] y, int n) (x, y) 5 s (x1, y2) (x2, y2) (x, y) w h (x, y) w h aw (x, y) w h (x, y) w h s e x 0 45 135 225 315 (x[0], y[0]) (x[1], y[1]) (x[n-1], y[n-1]) paintcomponent Graphics2D 4 Graphics Graphics2D Java Platform, Standard Edition 6 API (http://docs.oracle.com/javase/jp/6/api/) 5 (x, y) ( ) (x, y) 9 3
void drawpolygon(int[] x, int[] y, int n) void clearrect(int x, int y, void fillrect(int x, int y, void fillroundrect(int x, int y, int w, int h, int aw, int ah) void filloval(int x, int y, void fillarc(int x, int y, int w, int h, int s, int e) void fillpolygon(int[] x, int[] y, int n) (x[0], y[0]) (x[1], y[1]) (x[n-1], y[n-1]) (x, y) w h 6 drawrect drawroundrect drawoval drawarc ( ) drawpolygon 9.3 Card MyCard paintcomponent MyCard.java 1 import java.awt.*; 2 import jp.ac.ryukoku.math.graphics.*; 3 4 class MyCard extends Card { 5 static Color[] logocolors = { 6 new Color(150, 0, 150), // 7 new Color(0, 150, 0), // 8 new Color(0, 120, 200) // 9 }; 10 static int[][] logoxs = { 11 {13, 13, 26, 26}, {43, 43, 56, 56}, {27, 57, 73, 42} 12 }; 13 static int[][] logoys = { 14 {37, 87, 87, 50}, {17, 50, 50, 17}, {50, 87, 87, 50} 15 }; 16 17 MyCard() { super(); } 18 MyCard(Suit suit, Rank rank) { super(suit, rank); } 19 20 protected void paintcomponent(graphics g) { 21 if (isfacedup()) { 22 /* */ 23 super.paintcomponent(g); 24 return; 25 } 26 /* */ 27 g.setcolor(new Color(255, 255, 255)); // 28 g.fillroundrect(0, 0, 80, 120, 6, 6); // 29 g.setcolor(new Color(200, 200, 200)); // 30 g.fillroundrect(3, 3, 74, 114, 6, 6); // 31 for (int i = 0; i < logocolors.length; i++) { 6 JComponent void setbackground(color c) 9 4
32 /* 3 */ 33 g.setcolor(logocolors[i]); 34 g.fillpolygon(logoxs[i], logoys[i], logoxs[i].length); 35 } 36 g.setcolor(color.black); // 37 g.drawstring(" ", 17, 108); // 38 } 39 } MyCard paintcomponent super.paintcomponent(g); Card G901.java MyCard 2 MyCard paintcomponent 2 G901.java 1 import jp.ac.ryukoku.math.graphics.*; 2 3 class G901 { 4 public static void main(string[] args) { 5 GameFrame f = new GameFrame(); 6 f.add(new MyCard(Suit.HEARTS, Rank.KING), 400, 300); 7 f.add(new MyCard()); 8 } 9 } drawpolygon drawstring paintcomponent (Graphics ) x y ( ) translate create 9 5
9.4 (Color ) JFC java.awt.color MyCard new Color(150, 0, 150) Color.BLACK Color Color (immutable) Color Color(int r, int g, int b) Color(int r, int g, int b, int a) Color(float r, float g, float b) Color(float r, float g, float b, float a) static final Color BLACK static final Color DRAK GRAY static final Color GRAY static final Color LIGHT GRAY static final Color WHITE static final Color RED static final Color GREEN static final Color BLUE static final Color YELLOW static final Color CYAN static final Color MGENTA static final Color PINK static final Color ORANGE static Color gethsbcolor (float h, float s, float b) 3 ( ) 0 255 (r, g, b) 3 ( ) ( ) 0 255 (r, g, b a) 3 ( ) 0.0 1.0 (r, g, b) 3 ( ) ( ) 3 ( ) 0.0 1.0 (r, g, ba) 0.0 1.0 (h, s, b) Color brighter() Color darker() int getred() 0 255 9 6
int getgreen() int getblue() int getalpha() 0 255 0 255 ( ) 0 255 Color 3 RGB ( ) (r, g, b) Color 3 int 0 255 float 0.0 1.0 new Color(0, 0, 0) new Color(0.0f, 0.0f, 0.0f) new Color(255, 255, 0) new Color(1.0f, 1.0f, 0.0f) Color new Color(255, 255, 0) RGB ( ) ( ) 0.0 1.0 α ( ) = α + (1 α) 1.0 ( 0 ) 0.0 ( ) RGB 0 255 7 Color 1.0 (int 255 ) 4 MyCard.java 27 29 // g.setcolor(new Color(255, 255, 255, 100)); // g.setcolor(new Color(200, 200, 200, 100)); 7 1.0 9 7
9.5 1. MyCard.java MyCard2.java MyCard2 paintcomponent Graphics translate logoxs logoys 2. Card MyCard3 MyCard3.java MyCard3 paintcomponent 3 3. Deck protected Card createcard(suit s, Rank r) { return new Card(s, r); } Deck MyDeck createcard MyCard MyDeck MyDeck.java 2 MyDeck MyCard MyDeck() MyDeck(int n) n (n = 0, 1, 2) 9 8
MyDeck.java G902.java G902.java 1 import jp.ac.ryukoku.math.graphics.*; 2 3 class G902 { 4 public static void main(string[] args) { 5 GameFrame f = new GameFrame(); 6 Deck[] decks 7 = { new MyDeck(), new Deck(), new MyDeck(1) }; 8 int x = 200; 9 for (Deck d : decks) { 10 f.add(d, x, 120); 11 d.shuffle(); 12 x += 160; 13 } 14 x = 80; 15 for (int i = 0; i < 5; i++) { 16 for (Deck d : decks) { 17 Card c = d.pickup(); 18 c.moveto(x, 400); 19 if (i % 2 == 0) { 20 c.flip(); 21 } 22 x += 40; 23 } 24 } 25 } 26 } G902.java 9 9 9