- [NeoPaint.java]
※吉永さんへ:まだ皮しかできていません。
①IO周り・イベントの実装はできていません
②BufferedImageオブジェクトを内部に持たせる(メンバ変数)?
→読み込み・保存・undo/redo?
③JPanelを継承したDrawArea.classのインスタンスを格納するメンバ変数も用意しようと思います。
④必要なプロパティの取得/設定用getter/setter??。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NeoPaint extends JFrame {
class MenuFileAL implements ActionListener{
public void actionPerformed(ActionEvent ev){
if(ev.getSource() == createMenuItem){
System.out.println("新規作成を選択");
}else if(ev.getSource() == openMenuItem){
System.out.println("開くを選択");
}else if(ev.getSource() == saveAsMenuItem){
System.out.println("名前を付けて保存を選択");
}else if(ev.getSource() == closeMenuItem){
System.out.println("閉じるを選択");
}
}
}
class MenuEditAL implements ActionListener{
public void actionPerformed(ActionEvent ev){
if(ev.getSource() == undoMenuItem){
System.out.println("元に戻すを選択");
}else if(ev.getSource() == redoMenuItem){
System.out.println("やり直しを選択");
}
}
}
class RadioThickAL implements ActionListener{
public void actionPerformed(ActionEvent ev){
if(ev.getSource() == thickStrokeRadioButton){
System.out.println("太さ:太を選択");
strokeness =Strokeness.THICK;
}else if(ev.getSource() == middleStrokeRadioButton){
System.out.println("太さ:中を選択");
strokeness = Strokeness.MIDDLE;
}else if(ev.getSource() == thinStrokeRadioButton){
System.out.println("太さ:細を選択");
strokeness = Strokeness.THIN;
}
}
}
class RadioLineAL implements ActionListener{
public void actionPerformed(ActionEvent ev){
if(ev.getSource() == freehandDrawModeRadioButton){
System.out.println("フリーハンドを選択");
drawMode = DrawMode.FREEHAND;
}else if(ev.getSource() == twoPointDrawModeRadioButton){
System.out.println("2点指定を選択");
drawMode = DrawMode.TWOPOINT;
}
}
public void focusLost(FocusEvent ev){
}
}
class TextColorAL implements ActionListener{
public void actionPerformed(ActionEvent ev){
if(ev.getSource() == redColorTextField){
System.out.println("赤色数値を選択");
setColorSampleLabelColor();
}else if(ev.getSource() == greenColorTextField){
System.out.println("緑色数値を選択");
setColorSampleLabelColor();
}else if(ev.getSource() == blueColorTextField){
System.out.println("青色数値を選択");
setColorSampleLabelColor();
}
}
}
enum DrawMode{
// 列挙定数
FREEHAND("FREEHAND", 0),
TWOPOINT("TWOPOINT", 1);
private String drawModeName;
private int drawModeValue;
private DrawMode(String drawModeName, int drawModeValue){
this.drawModeName = drawModeName;
this.drawModeValue = drawModeValue;
}
// メソッド
@Override
public String toString(){
return this.drawModeName;
}
public int toDrawModeValue(){
return this.drawModeValue;
}
}
enum Strokeness{
// 列挙定数
THICK("THICK", 30),
MIDDLE("MIDDLE", 20),
THIN("THIN", 10);
private String strokenessName;
private int strokenessValue;
private Strokeness(String strokenessName, int strokenessValue){
this.strokenessName = strokenessName;
this.strokenessValue = strokenessValue;
}
// メソッド
@Override
public String toString(){
return this.strokenessName;
}
public int toStrokenessValue(){
return this.strokenessValue;
}
}
// 画像パネル
//private DrawArea drawArea;
// 太さ
private Strokeness strokeness;
// 色
private Color lineColor;
// 線描画法
private DrawMode drawMode;
// メイン
public static void main(String[] args){
(new NeoPaint()).setVisible(true);
}
// メニューバー
private JMenuBar menuBar;
// ファイルメニューアイテムリスナ
private MenuFileAL fileMenuItemListener;
// ファイルメニュー
private JMenu fileMenu;
// 新規作成メニューアイテム
private JMenuItem createMenuItem;
// 開くメニューアイテム
private JMenuItem openMenuItem;
// 名前を付けて保存メニューアイテム
private JMenuItem saveAsMenuItem;
// 閉じるメニューアイテム
private JMenuItem closeMenuItem;
// 編集メニューアイテムリスナー
private MenuEditAL editMenuItemListener;
// 編集メニュー
private JMenu editMenu;
// 元に戻すメニューアイテム
private JMenuItem undoMenuItem;
// やり直しメニューアイテム
private JMenuItem redoMenuItem;
// グリッドパネル
private JPanel gridPanel;
// ストロークリスナ
private RadioThickAL strokeListener;
// ストロークパネル
private JPanel strokePanel;
// ストロークラジオボタングループ
private ButtonGroup strokeButtonGroup;
// 太い--ラジオボタン
private JRadioButton thickStrokeRadioButton;
// 中--ラジオボタン
private JRadioButton middleStrokeRadioButton;
// 細い--ラジオボタン
private JRadioButton thinStrokeRadioButton;
// カラーリスナー
private TextColorAL colorListener;
// カラーパネル
private JPanel colorPanel;
// 赤テキストフィールド
private JTextField redColorTextField;
// 緑テキストフィールド
private JTextField greenColorTextField;
// 青テキストフィールド
private JTextField blueColorTextField;
//色見本
private JLabel colorSampleLabel;
// ドローモードリスナ
private RadioLineAL drawModeListener;
// ドローモードパネル
private JPanel drawModePanel;
// ドローモードラジオボタングループ
private ButtonGroup drawModeButtonGroup;
// フリーハンド--ラジオボタン
private JRadioButton freehandDrawModeRadioButton;
// 2点指定--ラジオボタン
private JRadioButton twoPointDrawModeRadioButton;
public NeoPaint(){
super("ネオペイント");
gridPanel = new JPanel(new GridLayout(2,2));
setSize(800,500);
setLocation(100,30);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuBar = new JMenuBar();
fileMenuItemListener = new MenuFileAL();
fileMenu = new JMenu("ファイル");
createMenuItem = new JMenuItem("新規作成");
createMenuItem.addActionListener(fileMenuItemListener);
fileMenu.add(createMenuItem);
openMenuItem = new JMenuItem("開く");
openMenuItem.addActionListener(fileMenuItemListener);
fileMenu.add(openMenuItem);
saveAsMenuItem = new JMenuItem("名前を付けて保存");
saveAsMenuItem.addActionListener(fileMenuItemListener);
fileMenu.add(saveAsMenuItem);
closeMenuItem = new JMenuItem("閉じる");
closeMenuItem.addActionListener(fileMenuItemListener);
fileMenu.add(closeMenuItem);
menuBar.add(fileMenu);
editMenuItemListener = new MenuEditAL();
editMenu = new JMenu("編集");
undoMenuItem = new JMenuItem("元に戻す");
undoMenuItem.addActionListener(editMenuItemListener);
editMenu.add(undoMenuItem);
redoMenuItem = new JMenuItem("やり直し");
redoMenuItem.addActionListener(editMenuItemListener);
editMenu.add(redoMenuItem);
menuBar.add(editMenu);
add(menuBar, BorderLayout.NORTH);
strokeListener = new RadioThickAL();
strokePanel = new JPanel(new FlowLayout());
strokePanel.add(new JLabel("太さ:"));
strokeButtonGroup = new ButtonGroup();
thickStrokeRadioButton = new JRadioButton("太");
thickStrokeRadioButton.addActionListener(strokeListener);
strokeButtonGroup.add(thickStrokeRadioButton);
strokePanel.add(thickStrokeRadioButton);
middleStrokeRadioButton = new JRadioButton("中", true);
middleStrokeRadioButton.addActionListener(strokeListener);
strokeButtonGroup.add(middleStrokeRadioButton);
strokePanel.add(middleStrokeRadioButton);
thinStrokeRadioButton = new JRadioButton("細");
thinStrokeRadioButton.addActionListener(strokeListener);
strokeButtonGroup.add(thinStrokeRadioButton);
strokePanel.add(thinStrokeRadioButton);
gridPanel.add(strokePanel);
colorListener = new TextColorAL();
colorPanel = new JPanel(new FlowLayout());
Dimension dim = new Dimension(40, 25);
colorPanel.add(new JLabel("色"));
colorPanel.add(new JLabel("R:"));
redColorTextField = new JTextField("255");
redColorTextField.addActionListener(colorListener);
redColorTextField.setPreferredSize(dim);
colorPanel.add(redColorTextField);
colorPanel.add(new JLabel("G:"));
greenColorTextField = new JTextField("0");
greenColorTextField.addActionListener(colorListener);
greenColorTextField.setPreferredSize(dim);
colorPanel.add(greenColorTextField);
colorPanel.add(new JLabel("B:"));
blueColorTextField = new JTextField("0");
blueColorTextField.addActionListener(colorListener);
blueColorTextField.setPreferredSize(dim);
colorPanel.add(blueColorTextField);
gridPanel.add(colorPanel);
drawModeListener = new RadioLineAL();
drawModePanel = new JPanel(new FlowLayout());
drawModePanel.add(new JLabel("線描画法:"));
drawModeButtonGroup = new ButtonGroup();
freehandDrawModeRadioButton = new JRadioButton("フリーハンド", true);
freehandDrawModeRadioButton.addActionListener(drawModeListener);
drawModeButtonGroup.add(freehandDrawModeRadioButton);
drawModePanel.add(freehandDrawModeRadioButton);
twoPointDrawModeRadioButton = new JRadioButton("2点指定");
twoPointDrawModeRadioButton.addActionListener(drawModeListener);
drawModeButtonGroup.add(twoPointDrawModeRadioButton);
drawModePanel.add(twoPointDrawModeRadioButton);
gridPanel.add(drawModePanel);
colorSampleLabel = new JLabel();
gridPanel.add(colorSampleLabel);
setColorSampleLabelColor();
colorSampleLabel.setOpaque(true);
menuBar.add(gridPanel);
}
private void setColorSampleLabelColor(){
if(!isRegularColorNumber(redColorTextField)
|| !isRegularColorNumber(greenColorTextField)
|| !isRegularColorNumber(blueColorTextField)){
colorSampleLabel.setForeground(new Color(255, 255, 255));
colorSampleLabel.setText("RGBは各々000~255の値を指定してください");
redColorTextField.setText("0");
greenColorTextField.setText("0");
blueColorTextField.setText("0");
}else{
colorSampleLabel.setText("");
}
int redNumber = Integer.parseInt(redColorTextField.getText());
int greenNumber = Integer.parseInt(greenColorTextField.getText());
int blueNumber = Integer.parseInt(blueColorTextField.getText());
this.lineColor = new Color(redNumber, greenNumber, blueNumber);
// redColorTextField.setText(String.format("%1$03d", redNumber));
// greenColorTextField.setText(String.format("%1$03d",greenNumber));
// blueColorTextField.setText(String.format("%1$03d",blueNumber));
colorSampleLabel.setBackground(this.lineColor);
}
private boolean isRegularColorNumber(JTextField jt){
String text = jt.getText();
if(text.length() > 3)return false;
return text.matches("^([0-9]{1,2}|[01][0-9][0-9]|2[0-4][0-9]|25[0-5])$");
}
public Strokeness getStrokeness(){
return this.strokeness;
}
public Color getLineColor(){
return this.lineColor;
}
public DrawMode getDrawMode(){
return this.drawMode;
}
}
- [20141213宿題]
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.regex.PatternSyntaxException;
import java.util.regex.*;
enum DigitStatus {
fractionOnlyNG, OK, NG
}
public class Calc {
static String currentData = "";
static String currentOperator = "+";
static BigDecimal currentResult = new BigDecimal(0);
static boolean isNotEndOfWhile = true;
static BufferedReader br = null;
/*定数*/
static final int MAX_DIGIT = 15;
static final String START_COMMENT = "計算機アプリを開始します。";
static final String NUMBER_INPUT_COMMENT = "数字を入力してください:";
static final String NUMBER_Operator_Q_INPUT_COMMENT = "数字・演算子・qを入力してください。";
static final String NUMBER_Operator_Q_INPUT_COMMENT_FOR_DIVIDE = "0以外の数字・演算子・qを入力してください。";
static final String SITEIKETASU_COMMENT = "最大桁数";
static final String OVER_COMMENT = "をオーバーしています。";
static final String SAIDO_INPUT_COMMENT = "もう1度入力してください。";
static final String END_COMMENT = "計算機アプリを終了します。";
static final String RESET_COMMENT = "値をリセットしました。";
static final String Operator_CHANGE_COMMENT = "演算子を変更します。";
static final String ZERO_DIVIDE_COMMENT = "0で除算しようとしました。";
public static void main (String[] args) throws IOException, NumberFormatException {
try {
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(START_COMMENT);
System.out.print(NUMBER_INPUT_COMMENT);
currentData = br.readLine();
currentData = currentData.trim();
while (isNotEndOfWhile) {
makeValidation();
}
} catch (IOException iEx) {
System.out.println(iEx.toString());
} catch (NumberFormatException nEx) {
System.out.println(nEx.toString());
} catch (ArithmeticException aEx) {
System.out.println(aEx.toString());
} catch (PatternSyntaxException pEx) {
System.out.println(pEx.toString());
} finally {
if (br != null) {
br.close();
}
}
}
private static void makeValidation()
throws NumberFormatException, IOException,
ArithmeticException, PatternSyntaxException {
if (currentData == null || currentData.isEmpty()) {
System.out.println(NUMBER_Operator_Q_INPUT_COMMENT);
readLineInputData();
} else if (currentData.matches("^[qQ]$")) {
System.out.print(END_COMMENT);
isNotEndOfWhile = false;
return;
} else if (currentData.matches("^\\=$")) {
System.out.println("= " + currentResult.toPlainString());
currentResult = new BigDecimal(0.0);//値のリセット
currentOperator = "+";
readLineInputData();
} else if (currentData.matches("^[\\+\\-]?\\d+(\\.\\d+)?$")) {
calculate();
readLineInputData();
} else if (currentData.matches("^[\\+\\-\\*/]$")){
currentOperator = currentData;
readLineInputData();
} else {
System.out.println(NUMBER_Operator_Q_INPUT_COMMENT);
readLineInputData();
}
}
private static void readLineInputData() throws IOException{
System.out.print(currentResult.toPlainString() + " " + currentOperator + " ");
currentData = br.readLine();
currentData = currentData.trim();
}
private static DigitStatus isDigitAvialable (String examData) {
BigDecimal bigDecimalExamData = new BigDecimal(examData);
String strBDExamData = bigDecimalExamData.toPlainString().replaceFirst("[\\+\\-]", "");
int totalLength = strBDExamData.length();
int integerLength = strBDExamData.indexOf(".") == -1 ? totalLength : strBDExamData.indexOf(".");
int fractionLength = strBDExamData.indexOf(".") == -1 ? 0 : totalLength - integerLength - 1;
if (integerLength <= MAX_DIGIT && fractionLength <= MAX_DIGIT) {
return DigitStatus.OK;
} else {
System.out.println(SITEIKETASU_COMMENT + "(" + MAX_DIGIT + ")" + OVER_COMMENT);
System.out.println(SAIDO_INPUT_COMMENT);
return DigitStatus.NG;
}
}
private static DigitStatus isfractionDigitOnlyAvialable (String examData) {
BigDecimal bigDecimalExamData = new BigDecimal(examData);
String strBDExamData = bigDecimalExamData.toPlainString().replaceFirst("[\\+\\-]", "");
int totalLength = strBDExamData.length();
int integerLength = 0;
int fractionLength = 0;
integerLength = strBDExamData.indexOf(".") == -1 ? totalLength : strBDExamData.indexOf(".");
fractionLength = strBDExamData.indexOf(".") == -1 ? 0 : totalLength - integerLength - 1;
if (integerLength <= MAX_DIGIT && fractionLength > MAX_DIGIT) {
return DigitStatus.fractionOnlyNG;
} else {
return DigitStatus.OK;
}
}
private static void calculate ()
throws NumberFormatException, IOException, ArithmeticException{
BigDecimal temp_currentResult = currentResult;
BigDecimal bigDecimalCurrentData = new BigDecimal(currentData);
if (isDigitAvialable(currentData) == DigitStatus.NG){
return;
}
switch (currentOperator.charAt(0)){
case '+':
temp_currentResult = currentResult.add(bigDecimalCurrentData);
break;
case '-':
temp_currentResult = currentResult.subtract(bigDecimalCurrentData);
break;
case '*':
temp_currentResult = currentResult.multiply(bigDecimalCurrentData);
if (isfractionDigitOnlyAvialable(temp_currentResult.toPlainString()) == DigitStatus.fractionOnlyNG) {
System.out.println("実計算結果:" + temp_currentResult.toPlainString());
System.out.println("四捨五入を実行して指定桁(" + MAX_DIGIT + ")にします:");
temp_currentResult = currentResult.multiply(bigDecimalCurrentData);
temp_currentResult = temp_currentResult.setScale(MAX_DIGIT, BigDecimal.ROUND_HALF_UP);
}
break;
case '/':
Double doubleCurrentData = Double.parseDouble(currentData);
if (doubleCurrentData == 0) {
System.out.println(NUMBER_Operator_Q_INPUT_COMMENT_FOR_DIVIDE);
return;
} else {
try{
temp_currentResult = currentResult.divide(bigDecimalCurrentData);
if (isfractionDigitOnlyAvialable(temp_currentResult.toPlainString()) == DigitStatus.fractionOnlyNG) {
System.out.println("実計算結果:" + temp_currentResult.toPlainString());
System.out.println("四捨五入を実行して指定桁(" + MAX_DIGIT + ")にします:");
temp_currentResult = currentResult.divide(bigDecimalCurrentData,
MAX_DIGIT ,
BigDecimal.ROUND_HALF_UP);
}
} catch(ArithmeticException aEx) {
temp_currentResult = currentResult.divide(bigDecimalCurrentData,
MAX_DIGIT ,
BigDecimal.ROUND_HALF_UP);
}
}
break;
default:
throw new IOException();
}
if (isDigitAvialable(temp_currentResult.toPlainString()) == DigitStatus.OK) {
currentResult = temp_currentResult;
}
}
}
- [宿題20141216]先生のお手本を参考に、20141221パフォーマンス修正。
まだprimeSet有効活用できず(primeSetオブジェクトの保存読込未着手・ループ変数の選択改良未着手)。
2014122primeSet.addの位置修正??
import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.*;
public class HW20141216 {
static String strCurrentLong = "";
static long startLong = 0;
static long currentLong = 0;
static String OPRATOR_PLUS = "*";
static TreeSet<Long> primeSet = new TreeSet<Long>();
static boolean isToBeChecked = false;
static String stringPrimeFact = "";
static BufferedReader br = null;
/*定数*/
static final int MAX_DIGIT = 15;
static final String START_COMMENT = "整数を入力してください。";
static final String WARN_COMMENT = "2以上の整数を入力してください。";
public static void main (String[] args) throws IOException, NumberFormatException {
primeSet.add(new Long (2));
try {
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(START_COMMENT);
readLineAndLongConvert();
while (!(currentLong >=2)) {
System.out.println(WARN_COMMENT);
readLineAndLongConvert();
}
startLong = currentLong;
long workLong = 2;
makePrimeFactorization (workLong, currentLong);
} catch (IOException iEx) {
System.out.println(iEx.toString());
} catch (NumberFormatException nEx) {
System.out.println(nEx.toString());
} finally {
if (br != null) {
br.close();
}
}
}
private static void makePrimeFactorization (long workLong, long currentLong) {
while (currentLong !=1) {
if(isToBeChecked(workLong) && currentLong % workLong == 0) {
currentLong /= workLong;
stringPrimeFact += workLong + OPRATOR_PLUS;
} else {
workLong = workLong > 2 ? workLong + 2 : workLong + 1;
}
}
if (stringPrimeFact.length() > 0) {
stringPrimeFact = stringPrimeFact.substring(0, stringPrimeFact.length() - 1);
System.out.println(startLong + " = " + stringPrimeFact);
}
}
private static void readLineAndLongConvert() throws IOException, NumberFormatException{
strCurrentLong = br.readLine();
currentLong = Long.parseLong(strCurrentLong);
}
private static boolean isToBeChecked (long workLong) {
if(primeSet.contains(new Long(workLong))){
return true;
}
for (long j :primeSet) {
if(workLong % j == 0) {
return false;
}else if (workLong % j != 0 && (double)j >= (double)Math.sqrt(workLong)){
primeSet.add(new Long(workLong));
return true;
}
}
primeSet.add(new Long(workLong));
return true;
}
}
HW20150106 †
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class HW20150106 extends Applet implements MouseListener,MouseMotionListener {
ArrayList<Point> points = new ArrayList<Point>();
Point currentPoint;
public void init(){
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
g.setColor(Color.red);
Point prevPoint;
if(points.isEmpty()){
return;
} else {
prevPoint = points.get(0);
}
for (Point point : points) {
g.drawLine(point.x, point.y, prevPoint.x, prevPoint.y);
prevPoint = point;
}
}
// マウスリスナー
public void mousePressed(MouseEvent ev){
System.out.println("mouse pressed");
}
public void mouseReleased(MouseEvent ev){
System.out.println("mouse released");
}
public void mouseClicked(MouseEvent ev){
currentPoint = new Point(ev.getX(), ev.getY());
points.add(currentPoint);
repaint();
System.out.println("mouse clicked");
}
public void mouseEntered(MouseEvent ev){
System.out.println("mouse entered");
setBackground(Color.white);
}
public void mouseExited(MouseEvent ev){
System.out.println("mouse exited");
setBackground(Color.black);
}
public void mouseDragged(MouseEvent ev){
if(currentPoint == null){
currentPoint = new Point(ev.getX(), ev.getY());
}
int deltaX = ev.getX() - currentPoint.x;
int deltaY = ev.getY() - currentPoint.y;
for (Point point : points) {
point.translate(deltaX, deltaY);
}
repaint();
}
public void mouseMoved(MouseEvent ev){
}
}