このページのコンテンツ一覧(5個位までにしてください。上が最新で下が最古です)
import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.swing.*; public class HW116 extends JApplet { private static final int[][] CONTEXT = { { 5, 900}, { 10, 800}, { 15, 700}, { 20, 600}, }; private static final int SCREEN_W = 600; private static final int SCREEN_H = 500; private static final String IMG_NAME = "img1.png"; private Game game; private int score; private JLabel screen = new JLabel(); private JLabel status = new JLabel(); private JButton startBtn = new JButton("スタート"); private JButton stopBtn = new JButton("ストップ"); private BufferedImage scrImg = new BufferedImage(SCREEN_W, SCREEN_H, BufferedImage.TYPE_INT_ARGB); private Graphics2D scrG = scrImg.createGraphics(); private Image image; private int imgW; private int imgH; public void init () { image = getImage(getCodeBase(), IMG_NAME); MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); while (mt.checkAll() == false) { try { mt.waitForAll(); } catch (InterruptedException ex) { // nop } } imgW = image.getWidth(null); imgH = image.getHeight(null); scrG.setBackground(new Color(230, 230, 240)); scrG.clearRect(0, 0, scrImg.getWidth(), scrImg.getHeight()); screen.setIcon(new ImageIcon(scrImg)); screen.setBorder(BorderFactory.createEtchedBorder()); screen.setHorizontalAlignment(SwingConstants.CENTER); screen.setVerticalAlignment(SwingConstants.CENTER); screen.setHorizontalTextPosition(SwingConstants.CENTER); screen.setFont(new Font("Dialog", 1, 20)); screen.addMouseListener(new MouseAdapter() { public void mousePressed (MouseEvent e) { if (game == null) { return; } if (game.getRect().contains(e.getPoint())) { status.setText("スコア:" + (++score) + "点"); game.interrupt(); } } }); status.setHorizontalAlignment(SwingConstants.CENTER); status.setPreferredSize(new Dimension(1000, 32)); status.setFont(new Font("Dialog", 1, 15)); startBtn.addActionListener(new ActionListener () { public void actionPerformed (ActionEvent e) { screen.setText(""); score = 0; status.setText("スコア:" + score + "点"); startBtn.setEnabled(false); stopBtn.setEnabled(true); game = new Game(); game.start(); } }); stopBtn.addActionListener(new ActionListener () { public void actionPerformed (ActionEvent e) { game.setStopFlag(); game = null; status.setText("中止しました"); startBtn.setEnabled(true); stopBtn.setEnabled(false); } }); stopBtn.setEnabled(false); JPanel bottom = new JPanel(new FlowLayout(FlowLayout.CENTER, 64, 4)); bottom.add(startBtn); bottom.add(stopBtn); add(screen, BorderLayout.CENTER); add(bottom, BorderLayout.SOUTH); add(status, BorderLayout.NORTH); } private class Game extends Thread { private Rectangle rect = new Rectangle(); private volatile boolean stopFlag; public void run () { for (int i = 0; i < CONTEXT.length; i++) { int count = CONTEXT[i][0]; int interval = CONTEXT[i][1]; for (int k = 0; k < count; k++) { if (stopFlag) { scrG.clearRect(0, 0, SCREEN_W, SCREEN_H); screen.repaint(); return; } int x = (int) (Math.random() * (SCREEN_W - imgW)); int y = (int) (Math.random() * (SCREEN_H - imgH)); setRect(x, y, imgW, imgH); scrG.drawImage(image, x, y, null); screen.repaint(); try { Thread.sleep(interval); } catch (InterruptedException e) { // nop } setRect(0, 0, 0, 0); scrG.clearRect(x, y, imgW, imgH); screen.repaint(); } } screen.setText("<html> ゲーム終了 <br> スコア" + score + "点 </html>"); status.setText(""); startBtn.setEnabled(true); stopBtn.setEnabled(false); } private synchronized void setRect (int x, int y, int w, int h) { rect.setBounds(x, y, w, h); } private synchronized Rectangle getRect () { return rect; } private void setStopFlag () { stopFlag = true; } } }
import java.io.*; import java.util.*; import java.util.Map.Entry; import javax.xml.parsers.*; import org.w3c.dom.*; public class HW114 { private static final Map keyToText = new HashMap() { { put("root_type", "ソースファイル"); put("class_type", "クラス名"); put("method_type", "メソッド名"); put("class_count", "クラスの数"); put("method_count", "メソッドの数"); put("field_count", "属性の数"); put("public", "publicメンバの数"); put("protected", "protectedメンバの数"); put("package", "packageメンバの数"); put("private", "privateメンバの数"); put("line_count", "ステートメントの数"); put("call_count", "メソッド呼出の回数"); } }; private static final Set modifySet = new TreeSet( Arrays.asList(new String[] { "public", "protected", "package", "private" })); public static void main (String[] args) { Document doc = null; try { if (args.length == 0) { throw new Exception(); } DocumentBuilder db = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); doc = db.parse(new File(args[0])); } catch (Exception ex) { System.out.println("XMLファイル読み込みエラー"); System.exit(0); } Map rootMap = new LinkedHashMap(); rootMap.put("type", "root_type"); rootMap.put("name", ""); Element root = doc.getDocumentElement(); inspect(root, rootMap); StringBuffer sb = new StringBuffer(); output(sb, rootMap, ""); try { File file = new File("output.txt"); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); bw.write(sb.toString()); bw.flush(); bw.close(); } catch (Exception ex) { System.out.println("書き込みエラー"); } } private static void inspect (Node elm, Map param) { NodeList list = elm.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) { continue; } if (node.getNodeName().equals("class")) { processClass(node, param); } else if (node.getNodeName().equals("method")) { processMember(node, param, true); } else if (node.getNodeName().equals("field")) { processMember(node, param, false); } else if (node.getNodeName().equals("block")) { processLine(node, param, true); } else if (node.getNodeName().equals("send")) { processLine(node, param, false); } else if (node.getNodeName().equals("modifier")) { processModifier(node, param); } else { inspect(node, param); } } } private static void processClass (Node node, Map map) { if (map.get("type").equals("root_type") == false) { return; } Integer itg = (Integer) map.get("class_count"); if (itg == null) { itg = new Integer(0); } map.put("class_count", new Integer(itg.intValue() + 1)); NamedNodeMap attrs = node.getAttributes(); Attr att = (Attr) attrs.getNamedItem("name"); String className = att.getValue(); Map newMap = new LinkedHashMap(); newMap.put("type", "class_type"); newMap.put("name", className); newMap.put("parent", map); map.put("child@" + className, newMap); inspect(node, newMap); } private static void processMember (Node node, Map map, boolean isMethod) { if (map.get("type").equals("class_type") == false) { return; } String countName = (isMethod) ? "method_count" : "field_count"; Integer itg = (Integer) map.get(countName); if (itg == null) { itg = new Integer(0); } map.put(countName, new Integer(itg.intValue() + 1)); if (isMethod) { NamedNodeMap attrs = node.getAttributes(); Attr att = (Attr) attrs.getNamedItem("name"); String metName = att.getValue(); Map newMap = new LinkedHashMap(); newMap.put("type", "method_type"); newMap.put("name", metName); newMap.put("parent", map); map.put("child@" + metName, newMap); inspect(node, newMap); } } private static void processLine (Node node, Map map, boolean isBlock) { if (map.get("type").equals("method_type") == false) { return; } String countName = (isBlock) ? "line_count" : "call_count"; Integer itg = (Integer) map.get(countName); if (itg == null) { itg = new Integer(0); } map.put(countName, new Integer(itg.intValue() + 1)); if (isBlock) { inspect(node, map); } } private static void processModifier (Node node, Map map) { if (map.get("type").equals("method_type") == false) { return; } NamedNodeMap attrs = node.getAttributes(); Attr att = (Attr) attrs.getNamedItem("name"); if (att == null || modifySet.contains(att.getValue()) == false) { return; } String visName = att.getValue(); Map parentMap = (Map) map.get("parent"); Integer itg = (Integer) parentMap.get(visName); if (itg == null) { itg = new Integer(0); } parentMap.put(visName, new Integer(itg.intValue() + 1)); } private static void output (StringBuffer sb, Map map, String tab) { List childList = new ArrayList(); String newTab = tab + " "; for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) { Entry entry = (Entry) it.next(); String name = (String) entry.getKey(); Object value = entry.getValue(); if (name.startsWith("child@")) { childList.add(value); continue; } if (name.equals("parent")) { continue; } Object text = keyToText.get(name); name = (text != null) ? (String) text : name; text = keyToText.get(value); value = (text != null) ? (String) text : value; if (name.equals("type")) { sb.append("\n" + tab + value + ":"); } else if (name.equals("name")) { sb.append(value + "\n"); } else { sb.append(newTab + name + ":" + value + "\n"); } } for (int i = 0; i < childList.size(); i++) { output(sb, (Map) childList.get(i), newTab); } } }
web.xmlファイル
<?xml version="1.0" encoding="Shift_JIS" ?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" > <welcome-file-list> <welcome-file> /WEB-INF/jsp/start.jsp </welcome-file> </welcome-file-list> <servlet> <servlet-name> Start </servlet-name> <jsp-file> /WEB-INF/jsp/start.jsp </jsp-file> </servlet> <servlet-mapping> <servlet-name> Start </servlet-name> <url-pattern> /start </url-pattern> </servlet-mapping> <servlet> <servlet-name> TableList </servlet-name> <jsp-file> /WEB-INF/jsp/tablelist.jsp </jsp-file> </servlet> <servlet-mapping> <servlet-name> TableList </servlet-name> <url-pattern> /tablelist </url-pattern> </servlet-mapping> <servlet> <servlet-name> Table </servlet-name> <jsp-file> /WEB-INF/jsp/table.jsp </jsp-file> </servlet> <servlet-mapping> <servlet-name> Table </servlet-name> <url-pattern> /table </url-pattern> </servlet-mapping> </web-app>
start.jspファイル
<%@page contentType="text/html; charset=Shift_JIS" pageEncoding="Shift_JIS" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <head> <meta http-equiv="content-type" content="text/html; charset="Shift_JIS" > </head> <body> <c:if test = "${datasrc == null}"> <sql:setDataSource url="jdbc:mysql://localhost:3306/homework" driver="com.mysql.jdbc.Driver" user="root" password="root" var="datasrc" scope="application" /> </c:if> <form action="tablelist" method="GET"> <table align="center" border="1"> <tr> <td colspan="2" align="center"> ユーザー認証 </td> </tr> <tr> <td> 名前 </td> <td> <input type="text" maxlength="10" name="username"> </td> </tr> <tr> <td> パスワード </td> <td> <input type="password" maxlength="10" name="password"> </td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="select"> </td> </tr> </table> </form> </body>
tablelist.jspファイル
<%@page contentType="text/html; charset=Shift_JIS" pageEncoding="Shift_JIS" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <head> <meta http-equiv="content-type" content="text/html; charset="Shift_JIS" > </head> <body> <sql:query var="rowset" dataSource="${datasrc}"> show tables; </sql:query> <center> テーブル一覧 </center> <br> <table align="center" border="1" cellpadding="4"> <c:forEach var="row" items="${rowset.rows}"> <tr> <td> ${row.Tables_in_homework} </td> <td> <form action="table" method="GET"> <input type="hidden" name="table" value="${row.Tables_in_homework}" /> <input type="submit" value="select" /> </form> </td> </tr> </c:forEach> </table> <br> <center> <form action="start" method="GET"> <input type="submit" value="back"> </form> </center> </body>
table.jspファイル
<%@page contentType="text/html; charset=Shift_JIS" pageEncoding="Shift_JIS" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> <head> <meta http-equiv="content-type" content="text/html; charset="Shift_JIS" > </head> <body> <sql:query var="rowset" dataSource="${datasrc}"> select * from ${param.table} ; </sql:query> <center> ${param.table} </center> <br> <table align="center" border="1" cellpadding="8"> <tr> <td align="center"> No </td> <td align="center"> Name </td> <td align="center"> Money </td> </tr> <c:forEach var="row" items="${rowset.rows}"> <tr> <td> ${row.No} </td> <td> ${row.Name} </td> <td> ${row.Money} </td> </tr> </c:forEach> </table> <br> <center> <form action="tablelist" method="GET"> <input type="submit" value="back"> </form> </center> </body>
// GraphDemo.java import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Set; /* *<applet code="GraphDemo.class" width="900" height="200">aaa</applet> */ public class GraphDemo extends Applet { public static final String COMPANY_A = "A社"; public static final String COMPANY_B = "B社"; public static final String COMPANY_C = "C社"; public static final String COMPANY_D = "D社"; public static final String TERM_NEW = "新人"; public static final String TERM_FRESH = "若手"; public static final String TERM_MID = "中堅"; public static final String TERM_VETERAN = "ベテラン"; public static final String SEX_MAN = "男"; public static final String SEX_WOMAN = "女"; private LinkedHashMap<Data, Integer> distribution = new LinkedHashMap<Data, Integer>(); public void init() { distribution.put( new Data(COMPANY_A, TERM_NEW, false), 95 ); distribution.put( new Data(COMPANY_A, TERM_NEW, true), 10 ); distribution.put( new Data(COMPANY_A, TERM_FRESH, false), 85 ); distribution.put( new Data(COMPANY_A, TERM_FRESH, true), 10 ); distribution.put( new Data(COMPANY_A, TERM_MID, false), 98 ); distribution.put( new Data(COMPANY_A, TERM_MID, true), 8 ); distribution.put( new Data(COMPANY_A, TERM_VETERAN, false), 77 ); distribution.put( new Data(COMPANY_A, TERM_VETERAN, true), 6 ); distribution.put( new Data(COMPANY_B, TERM_NEW, false), 56 ); distribution.put( new Data(COMPANY_B, TERM_NEW, true), 16 ); distribution.put( new Data(COMPANY_B, TERM_FRESH, false), 44 ); distribution.put( new Data(COMPANY_B, TERM_FRESH, true), 19 ); distribution.put( new Data(COMPANY_B, TERM_MID, false), 66 ); distribution.put( new Data(COMPANY_B, TERM_MID, true), 25 ); distribution.put( new Data(COMPANY_B, TERM_VETERAN, false), 86 ); distribution.put( new Data(COMPANY_B, TERM_VETERAN, true), 24 ); distribution.put( new Data(COMPANY_C, TERM_NEW, false), 28 ); distribution.put( new Data(COMPANY_C, TERM_NEW, true), 13 ); distribution.put( new Data(COMPANY_C, TERM_FRESH, false), 31 ); distribution.put( new Data(COMPANY_C, TERM_FRESH, true), 18 ); distribution.put( new Data(COMPANY_C, TERM_MID, false), 25 ); distribution.put( new Data(COMPANY_C, TERM_MID, true), 16 ); distribution.put( new Data(COMPANY_C, TERM_VETERAN, false), 0 ); distribution.put( new Data(COMPANY_C, TERM_VETERAN, true), 0 ); distribution.put( new Data(COMPANY_D, TERM_NEW, false), 105 ); distribution.put( new Data(COMPANY_D, TERM_NEW, true), 13 ); distribution.put( new Data(COMPANY_D, TERM_FRESH, false), 73 ); distribution.put( new Data(COMPANY_D, TERM_FRESH, true), 6 ); distribution.put( new Data(COMPANY_D, TERM_MID, false), 141 ); distribution.put( new Data(COMPANY_D, TERM_MID, true), 18 ); distribution.put( new Data(COMPANY_D, TERM_VETERAN, false), 0 ); distribution.put( new Data(COMPANY_D, TERM_VETERAN, true), 0 ); } public void paint( Graphics g ) { // 各社のx座標 int xA = 30, xB = 30, xC = 30, xD = 30; // 各社のy座標 int yA = 20, yB = 70, yC = 120, yD = 170; // タイトル部分に会社名を書く。 g.drawString( COMPANY_A, 10, yA+10 ); g.drawString( COMPANY_B, 10, yB+10 ); g.drawString( COMPANY_C, 10, yC+10 ); g.drawString( COMPANY_D, 10, yD+10 ); // HashMapのkeyを全部とってIteratorでまわす。 Set keySet = distribution.keySet(); Iterator iterator = keySet.iterator(); while( iterator.hasNext() ) { Data key = (Data)iterator.next(); // 棒グラフの開始地点座標 int y = 0; int x = 0; // 棒の長さは人数に比例(比例定数は2にしてみた。) int width = distribution.get(key) * 2; // keyから会社名を取得。 String keyCompany = key.getCompanyName(); // 会社が違えば、y座標が異なる。 // x座標は棒を書いた最右端になるようにする。 if( keyCompany.equals( COMPANY_A ) ) { y = yA; x = xA; xA += width; } else if( keyCompany.equals( COMPANY_B ) ) { y = yB; x = xB; xB += width; } else if( keyCompany.equals( COMPANY_C ) ) { y = yC; x = xC; xC += width; } else { y = yD; x = xD; xD += width; } // 性別が違えば、色が異なる。 String keySex = key.getSex(); if( keySex.equals( SEX_MAN ) ) g.setColor( Color.BLUE ); else g.setColor( Color.RED ); // 棒を書く。 g.drawRect( x, y, width, 10 ); // 黒字でキャリアを書いておく。 g.setColor( Color.BLACK ); String keyTerm = key.getTerm(); if(width != 0) { g.drawString( keyTerm, (x + width/2 -keyTerm.length()*5), y ); g.drawString( "" + (width/2), (x + width/2 -5), y+10 ); } } } }
// Data.java public class Data { private String companyName; private String term; private boolean isFemale; public Data( String companyName, String term, boolean isFemale ) { this.companyName = companyName; this.term = term; this.isFemale = isFemale; } public String getCompanyName() { return companyName; } public boolean isFemale() { return isFemale; } public String getTerm() { return term; } public String toString() { return this.companyName + this.term + getSex(); } public String getSex() { return this.isFemale ? GraphDemo.SEX_WOMAN : GraphDemo.SEX_MAN; } }
// HiraganaCounter.java import java.io.FileReader; import java.io.IOException; public class HiraganaCounter { public static void main(String[] args) { FileReader fr = null; try { fr = new FileReader(args[0]); int[] letter = new int[83];// ぁ〜ん までの83個を作成 for( int c; (c=fr.read()) != -1; ) { if (12353 <= c && c <= 12435 ) { letter[c - 12353]++; } } for( int i=0; i<letter.length; i++ ) { System.out.println( (char)(i+12353) + ":" + letter[i] ); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if( fr != null ) fr.close(); } catch( IOException ioe ) {} } } }