From ea8b6a7c586c217050f5ff2b119b9caaaf781b08 Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Sat, 14 May 2022 23:44:46 +0800 Subject: [PATCH 01/21] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Zhong <1648522715@qq.com> --- src/java2022spring/Calculator.java | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/java2022spring/Calculator.java diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000..f2b3859 --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,44 @@ +package java2022spring; +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +public class Calculator extends JFrame implements ActionListener +{ + private String[] KEYS={"7","8","9","*","4","5","6","-", + "1","2","3","+","0","e","π","÷","c","%",".","=","(",")","sqr","x*x"}; + private JButton keys[]=new JButton[KEYS.length]; + private JTextField resultText = new JTextField("0.0"); + public Calculator() + { + super("计算器"); + this.setLayout(null); + resultText.setBounds(20, 5, 250, 40); + resultText.setHorizontalAlignment(JTextField.RIGHT); + resultText.setEditable(false); + this.add(resultText); + int x=20,y=55; + for (int i=0;i Date: Sun, 15 May 2022 23:32:22 +0800 Subject: [PATCH 02/21] =?UTF-8?q?=E9=87=8D=E6=96=B0=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E6=A0=87=E5=87=86=E8=AE=A1=E7=AE=97=E5=99=A8=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E8=AE=A1=E7=AE=97=E5=99=A8=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20Signed-off-by:=20Zhong=20<1648522715@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index f2b3859..472db4e 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -4,13 +4,13 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Calculator extends JFrame implements ActionListener { - private String[] KEYS={"7","8","9","*","4","5","6","-", - "1","2","3","+","0","e","π","÷","c","%",".","=","(",")","sqr","x*x"}; + private String[] KEYS={"1/x","x*x","sqr","X","%","0","C","÷","7","8","9","*","4","5","6","-", + "1","2","3","+","(",")",".","="}; private JButton keys[]=new JButton[KEYS.length]; private JTextField resultText = new JTextField("0.0"); public Calculator() { - super("计算器"); + super("标准计算器"); this.setLayout(null); resultText.setBounds(20, 5, 250, 40); resultText.setHorizontalAlignment(JTextField.RIGHT); @@ -22,13 +22,22 @@ public class Calculator extends JFrame implements ActionListener keys[i] = new JButton(); keys[i].setText(KEYS[i]); keys[i].setBounds(x, y, 60, 40); + if(x<215) + { + x+=65; + } + else + { + x = 20; + y+=45; + } this.add(keys[i]); } for (int i = 0; i Date: Mon, 16 May 2022 21:51:24 +0800 Subject: [PATCH 03/21] =?UTF-8?q?=E4=BB=8A=E5=A4=A9=E7=BC=96=E5=86=99?= =?UTF-8?q?=E4=BA=86=E8=BF=90=E7=AE=97=E7=AE=97=E6=B3=95=E6=96=B9=E9=9D=A2?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=EF=BC=8C=E6=95=B0=E5=AD=97=E5=B0=9A?= =?UTF-8?q?=E6=9C=AA=E5=AE=9E=E7=8E=B0=EF=BC=8C=E6=9A=82=E6=97=B6=E8=BF=98?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=AE=9E=E7=8E=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Id3a24d2131ec9b65f6979edcd8fc4731ff49d889 Signed-off-by: Zhong <1648522715@qq.com> --- src/java2022spring/Calculator.java | 56 +++--------------------------- 1 file changed, 4 insertions(+), 52 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 472db4e..5d361c5 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,53 +1,5 @@ package java2022spring; -import javax.swing.*; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -public class Calculator extends JFrame implements ActionListener -{ - private String[] KEYS={"1/x","x*x","sqr","X","%","0","C","÷","7","8","9","*","4","5","6","-", - "1","2","3","+","(",")",".","="}; - private JButton keys[]=new JButton[KEYS.length]; - private JTextField resultText = new JTextField("0.0"); - public Calculator() - { - super("标准计算器"); - this.setLayout(null); - resultText.setBounds(20, 5, 250, 40); - resultText.setHorizontalAlignment(JTextField.RIGHT); - resultText.setEditable(false); - this.add(resultText); - int x=20,y=55; - for (int i=0;i Date: Mon, 16 May 2022 22:20:14 +0800 Subject: [PATCH 04/21] =?UTF-8?q?=E7=BC=96=E5=86=99=E4=BA=86=E8=BF=90?= =?UTF-8?q?=E7=AE=97=E7=AE=97=E6=B3=95=EF=BC=8C=E4=BD=86=E6=98=AF=E8=BF=98?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=AE=9E=E7=8E=B0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 100 ++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 3 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 5d361c5..d9890da 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,5 +1,99 @@ package java2022spring; +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +public class Calculator extends JFrame implements ActionListener//本计算器是基于Swing组建的图形用户界面,采用JFrame框架作为主窗口 +{ + private String[] KEYS={"1/x","x*x","sqr","X","%","0","C","÷","7","8","9","*","4","5","6","-", + "1","2","3","+","(",")",".","="}; + private JButton keys[]=new JButton[KEYS.length]; + private JTextField resultText = new JTextField("0.0");//结果显示文本框显示内容 + private String b="";//b用于存放计算式 + public Calculator() + { + super("标准计算器"); + this.setLayout(null);//设置布局 + resultText.setBounds(20, 5, 250, 40); + resultText.setHorizontalAlignment(JTextField.RIGHT);//设置输出结果文本框的水平对齐方式 + resultText.setEditable(false);//设置文本框不可以被修改 + this.add(resultText); + int x=20,y=55; + for (int i=0;i Date: Tue, 17 May 2022 20:11:01 +0800 Subject: [PATCH 05/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=B3=A8=E9=87=8A=EF=BC=8C=E6=89=8D=E5=86=99=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E7=99=BE=E5=A4=9A=E8=A1=8C=E5=B0=B1=E6=9C=89=E7=82=B9?= =?UTF-8?q?=E8=AE=B0=E4=B8=8D=E4=BD=8F=E4=BA=86=EF=BC=8C=E7=9B=AE=E5=89=8D?= =?UTF-8?q?=E6=89=93=E7=AE=97=E5=85=88=E6=8A=8A=E5=8A=9F=E8=83=BD=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=E5=BC=84=E5=87=BA=E6=9D=A5=EF=BC=8C=E5=86=8D=E6=85=A2?= =?UTF-8?q?=E6=85=A2=E5=AE=8C=E5=96=84=E5=86=85=E9=83=A8=E7=BB=93=E6=9E=84?= =?UTF-8?q?=C2=B7=E3=80=82=E7=AE=80=E5=8D=95=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E5=9B=9B=E4=B8=AA=E7=AE=97=E6=B3=95=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E6=89=BE=E5=A5=BD=E4=BA=86=EF=BC=8C=E7=8E=B0=E5=9C=A8?= =?UTF-8?q?=E6=AD=A3=E5=9C=A8=E5=AD=A6=E4=B9=A0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 36 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index d9890da..bdc8e0b 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -2,7 +2,9 @@ package java2022spring; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -public class Calculator extends JFrame implements ActionListener//本计算器是基于Swing组建的图形用户界面,采用JFrame框架作为主窗口 +//构造方法代码 +public class Calculator extends JFrame implements ActionListener/*本计算器是基于Swing组建的图形用户界面,采用JFrame框架作为主窗口 +用到的算法包括1.中缀表达式转后缀表达式,2.后缀表达式,3.平方运算,4.开方运算。 { private String[] KEYS={"1/x","x*x","sqr","X","%","0","C","÷","7","8","9","*","4","5","6","-", "1","2","3","+","(",")",".","="}; @@ -13,19 +15,20 @@ public class Calculator extends JFrame implements ActionListener// { super("标准计算器"); this.setLayout(null);//设置布局 - resultText.setBounds(20, 5, 250, 40); + resultText.setBounds(20, 5, 255, 40);//20,5是距离屏幕左右的像素,255和40是窗口的宽和高 resultText.setHorizontalAlignment(JTextField.RIGHT);//设置输出结果文本框的水平对齐方式 resultText.setEditable(false);//设置文本框不可以被修改 - this.add(resultText); + this.add(resultText);//新建文本框 int x=20,y=55; - for (int i=0;i Date: Thu, 19 May 2022 23:29:42 +0800 Subject: [PATCH 06/21] =?UTF-8?q?=E7=90=A2=E7=A3=A8=E4=BA=86=E4=B8=A4?= =?UTF-8?q?=E5=A4=A9=E7=9A=84=E5=90=8E=E7=BC=80=E7=AE=97=E6=B3=95=E5=92=8C?= =?UTF-8?q?=E4=B8=AD=E7=BC=80=E8=BD=AC=E5=90=8E=E7=BC=80=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E4=B9=9F=E6=B2=A1=E6=9C=89=E4=BB=80=E4=B9=88=E8=BF=9B=E5=B1=95?= =?UTF-8?q?=EF=BC=8C=E8=BF=98=E6=98=AF=E6=9C=89=E7=82=B9=E9=9A=BE=E5=BA=A6?= =?UTF-8?q?=E7=9A=84=E3=80=82=E4=BB=8A=E5=A4=A9=E8=AE=BE=E7=BD=AE=E4=BA=86?= =?UTF-8?q?=E6=B8=85=E9=99=A4=E9=94=AE=E5=92=8C=E6=B8=85=E9=99=A4=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E8=BE=93=E5=85=A5=E7=AC=A6=E7=9A=84X=E9=94=AE?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E6=98=AF=E5=A5=BD=E5=83=8F=E7=82=B9=E5=87=BB?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E7=9A=84=E6=97=B6=E5=80=99=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BA=86=EF=BC=8C=E6=98=8E=E5=A4=A9=E7=BB=A7?= =?UTF-8?q?=E7=BB=AD=E8=B0=83=E4=B8=80=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 61 ++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index bdc8e0b..2fce483 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -4,7 +4,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //构造方法代码 public class Calculator extends JFrame implements ActionListener/*本计算器是基于Swing组建的图形用户界面,采用JFrame框架作为主窗口 -用到的算法包括1.中缀表达式转后缀表达式,2.后缀表达式,3.平方运算,4.开方运算。 +用到的算法包括1.中缀表达式转后缀表达式,2.后缀表达式,3.平方运算,4.开方y运算*/ { private String[] KEYS={"1/x","x*x","sqr","X","%","0","C","÷","7","8","9","*","4","5","6","-", "1","2","3","+","(",")",".","="}; @@ -25,6 +25,7 @@ public class Calculator extends JFrame implements ActionListener/* keys[i] = new JButton(); keys[i].setText(KEYS[i]); keys[i].setBounds(x, y, 60, 40); + if(x<215)/*此处循环桥段参考了CSDN的代码,因为能力有限,写了好几次都是只能输出一个按钮 又或者是按钮无法对齐*/ { @@ -42,30 +43,54 @@ public class Calculator extends JFrame implements ActionListener/* keys[i].addActionListener(this); } - this.setResizable(true);//设置窗口可以调整大小 + this.setResizable(false);//设置窗口不可以调整大小 this.setBounds(500, 200, 300, 400); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true);//设置窗口是可见的 } - //处理事件 public void actionPerformed(ActionEvent e) //处理事件,ActionListener接口只有这一个方法 { - String label = e.getActionCommand(); + String label = e.getActionCommand(); //命令 + if (label=="="||label=="C"||label=="X")//处理特殊字符=、C和X + { + if(label=="=")//赋值等号 + { + String s=yunsuan1(this.b);//算法 + String result=yunsuan2(s); + this.b=result+null;//计算式直接等于result + resultText.setText(this.b);//setText选中一串字符串this.b + } + else if(label=="C")//清除所有内容 + { + this.b=null;//清空内容 + resultText.setText("0.0");//setText选中一串字符串,直接等于0.0 + } + else//点击一次X按钮删除一个输入字符 + { + AbstractButton result = null; + String resultText=result.getText(); + if(resultText.length()>0) { + result.setText(resultText.substring(0,resultText.length()-1)); + } + } + } + //算法3开方运算 if (label=="sqr")//算法3:开方运算,调用Math.sqrt()函数实现 { String n=yunsuan3(this.b); - resultText.setText(n);//setText选中一串字符串n - this.b=n;//计算式直接等于开方的答案 + resultText.setText(n);//setText选中字符串n + this.b=n;//计算式等于开方的答案 } + //算法4平方运算 else if(label=="x*x")//算法4:平方运算,调用Math.pow()函数实现 { String m=yunsuan4(this.b); - resultText.setText(m);//setText选中一串字符串m - this.b=m;//计算式直接等于平方的答案 + resultText.setText(m);//setText选中字符串m + this.b=m;//计算式等于平方的答案 } else @@ -74,15 +99,20 @@ public class Calculator extends JFrame implements ActionListener/* resultText.setText(this.b); } } - private String Result(String[] s) { - // TODO 自动生成的方法存根 + public String yunsuan1(String str) //算法1: + { + return null; + } + public String yunsuan2(String str) //算法2: + { return null; } - + public String yunsuan3(String str)//算法3:开方运算调用Math.sqrt()函数实现 { - String result=""; - double a=Double.parseDouble(str),b=0; + String result; + double a=Double.parseDouble(str); + double b=0; b=Math.sqrt(a);//调用Math.sqrt()函数实现 result=String.valueOf(b);//将b转换成字符串 return result; @@ -90,8 +120,9 @@ public class Calculator extends JFrame implements ActionListener/* public String yunsuan4(String str)//算法4:平方运算调用Math.pow()函数实现 { - String result=""; - double a=Double.parseDouble(str),b=0; + String result; + double a=Double.parseDouble(str); + double b=0; b=Math.pow(a, 2);//调用Math.pow()函数实现 result=String.valueOf(b);//将b转换成字符串 return result; -- Gitee From fa684f428f926cb986da57b055795180a2ef0dd9 Mon Sep 17 00:00:00 2001 From: ZhongZhongjun <10945993+zhongzhongjun@user.noreply.gitee.com> Date: Thu, 19 May 2022 15:37:49 +0000 Subject: [PATCH 07/21] add LICENSE. --- LICENSE | 661 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 661 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..80a61fa --- /dev/null +++ b/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. -- Gitee From 4d0891a5b9d4ab6479ff54d0242a933bf3b0db2d Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Fri, 20 May 2022 23:52:49 +0800 Subject: [PATCH 08/21] =?UTF-8?q?=E9=87=8D=E6=96=B0=E7=BC=96=E5=86=99?= =?UTF-8?q?=E4=BA=86=E7=95=8C=E9=9D=A2=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E6=98=AF=E7=AE=97=E6=B3=95=E9=83=A8=E5=88=86?= =?UTF-8?q?=E8=BF=98=E6=98=AF=E9=9A=BE=E4=BB=A5=C2=B7=E7=AA=81=E7=A0=B4?= =?UTF-8?q?=EF=BC=8C=E6=9C=9F=E5=BE=85=E6=98=8E=E5=A4=A9......?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 2fce483..618d261 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,5 +1,7 @@ package java2022spring; import javax.swing.*; + +import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //构造方法代码 @@ -25,7 +27,7 @@ public class Calculator extends JFrame implements ActionListener/* keys[i] = new JButton(); keys[i].setText(KEYS[i]); keys[i].setBounds(x, y, 60, 40); - + keys[i].setBackground(Color.ORANGE);//设置按钮的背景颜色为橙色 if(x<215)/*此处循环桥段参考了CSDN的代码,因为能力有限,写了好几次都是只能输出一个按钮 又或者是按钮无法对齐*/ { @@ -47,6 +49,7 @@ public class Calculator extends JFrame implements ActionListener/* this.setBounds(500, 200, 300, 400); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true);//设置窗口是可见的 + validate(); } //处理事件 @@ -59,15 +62,15 @@ public class Calculator extends JFrame implements ActionListener/* { String s=yunsuan1(this.b);//算法 String result=yunsuan2(s); - this.b=result+null;//计算式直接等于result + this.b=result+"";//计算式直接等于result resultText.setText(this.b);//setText选中一串字符串this.b } else if(label=="C")//清除所有内容 { - this.b=null;//清空内容 + this.b="";//清空内容 resultText.setText("0.0");//setText选中一串字符串,直接等于0.0 } - else//点击一次X按钮删除一个输入字符 + else//点击一次X按钮删除一个输入数据 { AbstractButton result = null; String resultText=result.getText(); @@ -76,7 +79,6 @@ public class Calculator extends JFrame implements ActionListener/* } } } - //算法3开方运算 if (label=="sqr")//算法3:开方运算,调用Math.sqrt()函数实现 { @@ -118,7 +120,7 @@ public class Calculator extends JFrame implements ActionListener/* return result; } - public String yunsuan4(String str)//算法4:平方运算调用Math.pow()函数实现 + public String yunsuan4(String str)//算法4:平方运算调用Math.pow()函数实现 { String result; double a=Double.parseDouble(str); @@ -128,7 +130,7 @@ public class Calculator extends JFrame implements ActionListener/* return result; } - public static void main(String arg[]) + public static void main(String arg[]) { Calculator b=new Calculator(); } -- Gitee From 78d027425ab703c122c69f282cccc50d348d307e Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Fri, 20 May 2022 23:56:06 +0800 Subject: [PATCH 09/21] =?UTF-8?q?=E9=87=8D=E6=96=B0=E7=BC=96=E5=86=99?= =?UTF-8?q?=E4=BA=86=E7=95=8C=E9=9D=A2=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E4=BD=86=E6=98=AF=E7=AE=97=E6=B3=95=E9=83=A8=E5=88=86?= =?UTF-8?q?=E8=BF=98=E6=98=AF=E9=9A=BE=E4=BB=A5=C2=B7=E7=AA=81=E7=A0=B4...?= =?UTF-8?q?...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 146 +++++++++++++++-------------- 1 file changed, 77 insertions(+), 69 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 618d261..989c68a 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,21 +1,36 @@ package java2022spring; import javax.swing.*; - import java.awt.Color; +import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -//构造方法代码 -public class Calculator extends JFrame implements ActionListener/*本计算器是基于Swing组建的图形用户界面,采用JFrame框架作为主窗口 -用到的算法包括1.中缀表达式转后缀表达式,2.后缀表达式,3.平方运算,4.开方y运算*/ -{ - private String[] KEYS={"1/x","x*x","sqr","X","%","0","C","÷","7","8","9","*","4","5","6","-", +@SuppressWarnings("serial") +class Calculator extends JFrame implements ActionListener/*本计算器是基于Swing组建的图形用户界面,采用JFrame框架作为主窗口 +用到的算法包括1.中缀表达式转后缀表达式,2.后缀表达式,3.平方运算,4.开方运算*/ +{ JTextArea text=new JTextArea(); + JPanel panel; //按钮面板 + String b="";//b用于存放计算式 + JTextField resultText = new JTextField("0.0");//结果显示文本框显示内容 + String[] KEYS={"1/x","x*x","sqr","X","%","0","C","÷","7","8","9","*","4","5","6","-", "1","2","3","+","(",")",".","="}; - private JButton keys[]=new JButton[KEYS.length]; - private JTextField resultText = new JTextField("0.0");//结果显示文本框显示内容 - private String b="";//b用于存放计算式 - public Calculator() - { - super("标准计算器"); + JButton keys[]=new JButton[KEYS.length]; + + public Calculator() { + super(); + init(); + this.setTitle("标准计算器"); + this.setVisible(true); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + validate(); + } + public void init() { + setTitle("标准计算器"); + this.setResizable(false);//设置窗口不可以调整大小 + this.setBounds(500, 200, 300, 400); + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + this.setVisible(true);//设置窗口是可见 + this.setFont(new Font("Calibre", Font.PLAIN, 60)); + //设置字体、样式、大小 this.setLayout(null);//设置布局 resultText.setBounds(20, 5, 255, 40);//20,5是距离屏幕左右的像素,255和40是窗口的宽和高 resultText.setHorizontalAlignment(JTextField.RIGHT);//设置输出结果文本框的水平对齐方式 @@ -44,63 +59,57 @@ public class Calculator extends JFrame implements ActionListener/* { keys[i].addActionListener(this); } - - this.setResizable(false);//设置窗口不可以调整大小 - this.setBounds(500, 200, 300, 400); - this.setDefaultCloseOperation(EXIT_ON_CLOSE); - this.setVisible(true);//设置窗口是可见的 - validate(); } //处理事件 + @SuppressWarnings("unused") public void actionPerformed(ActionEvent e) //处理事件,ActionListener接口只有这一个方法 { - String label = e.getActionCommand(); //命令 - if (label=="="||label=="C"||label=="X")//处理特殊字符=、C和X - { - if(label=="=")//赋值等号 - { - String s=yunsuan1(this.b);//算法 - String result=yunsuan2(s); - this.b=result+"";//计算式直接等于result - resultText.setText(this.b);//setText选中一串字符串this.b - } - else if(label=="C")//清除所有内容 - { - this.b="";//清空内容 - resultText.setText("0.0");//setText选中一串字符串,直接等于0.0 - } - else//点击一次X按钮删除一个输入数据 - { - AbstractButton result = null; - String resultText=result.getText(); - if(resultText.length()>0) { - result.setText(resultText.substring(0,resultText.length()-1)); - } - } - } - //算法3开方运算 - if (label=="sqr")//算法3:开方运算,调用Math.sqrt()函数实现 - { - String n=yunsuan3(this.b); - resultText.setText(n);//setText选中字符串n - this.b=n;//计算式等于开方的答案 - } - - //算法4平方运算 - else if(label=="x*x")//算法4:平方运算,调用Math.pow()函数实现 - { - String m=yunsuan4(this.b); - resultText.setText(m);//setText选中字符串m - this.b=m;//计算式等于平方的答案 - } + String label = e.getActionCommand();//命令 + if(label.equals("=")) + { + String s=yunsuan1(this.b);//算法 + String result=yunsuan2(s); + this.b=result+"";//计算式直接等于result + resultText.setText(this.b);//setText选中一串字符串this.b + } + else if(label.equals("C")) { + this.b="";//清空内容 + resultText.setText("0.0");//setText选中一串字符串,直接等于0.0 + } + else if(label.equals("X")){ + AbstractButton result = null; + @SuppressWarnings("null") + String resultText=result.getText(); + if(resultText.length()>0) { + result.setText(resultText.substring(0,resultText.length()-1)); + } + } + + //算法3开方运算 + else if (label.equals("sqr"))//算法3:开方运算,调用Math.sqrt()函数实现 + { + String n=yunsuan3(this.b); + resultText.setText(n);//setText选中字符串n + this.b=n;//计算式等于开方的答案 + } + + //算法4平方运算 + else if(label.equals("x*x"))//算法4:平方运算,调用Math.pow()函数实现 + { + String m=yunsuan4(this.b); + resultText.setText(m);//setText选中字符串m + this.b=m;//计算式等于平方的答案 + } - else - { - this.b=this.b+label;//遇到数字字符,直接加入后缀表达式 - resultText.setText(this.b); - } - } + else + { + this.b=this.b+label;//遇到数字字符,直接加入后缀表达式 + resultText.setText(this.b); + } + } + + public String yunsuan1(String str) //算法1: { return null; @@ -120,7 +129,7 @@ public class Calculator extends JFrame implements ActionListener/* return result; } - public String yunsuan4(String str)//算法4:平方运算调用Math.pow()函数实现 + public String yunsuan4(String str)//算法4:平方运算调用Math.pow()函数实现 { String result; double a=Double.parseDouble(str); @@ -129,10 +138,9 @@ public class Calculator extends JFrame implements ActionListener/* result=String.valueOf(b);//将b转换成字符串 return result; } - - public static void main(String arg[]) + + public static void main(String arg[]) { - Calculator b=new Calculator(); + Calculator a = new Calculator(); } - } - \ No newline at end of file +} \ No newline at end of file -- Gitee From b977c0b6fca0c0aa5dd33a8fab6b8e1133f045ba Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Tue, 24 May 2022 23:09:04 +0800 Subject: [PATCH 10/21] . --- src/java2022spring/Test.java | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 src/java2022spring/Test.java diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java deleted file mode 100644 index 24deb29..0000000 --- a/src/java2022spring/Test.java +++ /dev/null @@ -1,7 +0,0 @@ -package java2022spring; - -public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} -- Gitee From 350377d2da7df422246b17c8e2d0023c8e958c26 Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Tue, 24 May 2022 23:13:40 +0800 Subject: [PATCH 11/21] =?UTF-8?q?=E5=8E=9F=E6=9D=A5=E5=89=8D=E5=87=A0?= =?UTF-8?q?=E5=A4=A9=E4=B8=80=E7=9B=B4push=E5=A4=B1=E8=B4=A5=E4=BA=86?= =?UTF-8?q?=EF=BC=8C=E8=80=81=E5=B8=88=E6=88=91=E7=9C=9F=E7=9A=84=E6=9C=89?= =?UTF-8?q?=E5=9C=A8=E5=81=9A=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 浠婂ぉ鎶婂姛鑳介儴鍒嗛愭笎瀹屽杽浜嗭紝澶毦鐨勭畻娉曞涓嶄細锛岃嚜宸卞幓瀛︿簡涓浜涚畝鍗曚竴鐐圭殑绠楁硶锛屼絾鏄竴浜涘彲浠ヨ繍琛岋紝涓浜涗笉鑳借繍琛岋紝鎴戝凡缁忓偦鐪间簡锛屾槑澶╁啀缁х画淇敼浠g爜鍚 --- src/java2022spring/Calculator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 989c68a..2d678bc 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -141,6 +141,6 @@ class Calculator extends JFrame implements ActionListener/* public static void main(String arg[]) { - Calculator a = new Calculator(); + Calculator c = new Calculator(); } } \ No newline at end of file -- Gitee From e12b57f49ce8a750aa84eab31d03d61e90cbcf03 Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Wed, 25 May 2022 23:09:22 +0800 Subject: [PATCH 12/21] =?UTF-8?q?=E7=BB=88=E4=BA=8E=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E7=9A=84=E7=AE=97=E6=B3=95=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E5=AD=A6=E7=9A=84=E5=A4=AA=E9=9A=BE=E4=BA=86?= =?UTF-8?q?=EF=BC=8C=E5=AD=A6=E4=B8=8D=E4=BC=9A=EF=BC=8C=E7=8E=B0=E5=9C=A8?= =?UTF-8?q?=E6=AD=A3=E5=9C=A8=E9=87=8D=E6=96=B0=E6=91=B8=E7=B4=A2=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=EF=BC=8C=E5=9F=BA=E6=9C=AC=E6=A1=86=E6=9E=B6=E5=B7=B2?= =?UTF-8?q?=E6=9C=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 130 ++++++++++++++++------------- 1 file changed, 74 insertions(+), 56 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index 2d678bc..b8ff541 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -1,19 +1,24 @@ package java2022spring; import javax.swing.*; + import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @SuppressWarnings("serial") -class Calculator extends JFrame implements ActionListener/*本计算器是基于Swing组建的图形用户界面,采用JFrame框架作为主窗口 -用到的算法包括1.中缀表达式转后缀表达式,2.后缀表达式,3.平方运算,4.开方运算*/ -{ JTextArea text=new JTextArea(); - JPanel panel; //按钮面板 - String b="";//b用于存放计算式 - JTextField resultText = new JTextField("0.0");//结果显示文本框显示内容 - String[] KEYS={"1/x","x*x","sqr","X","%","0","C","÷","7","8","9","*","4","5","6","-", +public class Calculator extends JFrame implements ActionListener{//本计算器是基于Swing组建的图形用户界面,采用JFrame框架作为主窗口 + public JTextArea text=new JTextArea();//用户是否按的是整个表达式的第一个数字或者是运算符后的第一个数字 + public boolean firstDigit = true;//用户是否按的是整个表达式的第一个数字或者是运算符后的第一个数字 + public double resultNum = 0;//计算的中间结果 + public String operator = "="; // 当前运算的运算符 + public boolean operateValidFlag = true;// 操作是否合法 + + JPanel panel; //按钮面板 + String b="";//b用于存放计算式 + public JTextField resultText = new JTextField("0");//结果显示文本框显示内容 + String[] KEYS={"x*x","sqr","C","CE","%","0","1/x","÷","7","8","9","*","4","5","6","-", "1","2","3","+","(",")",".","="}; - JButton keys[]=new JButton[KEYS.length]; + JButton keys[]=new JButton[KEYS.length]; public Calculator() { super(); @@ -24,17 +29,15 @@ class Calculator extends JFrame implements ActionListener/* validate(); } public void init() { - setTitle("标准计算器"); this.setResizable(false);//设置窗口不可以调整大小 this.setBounds(500, 200, 300, 400); - this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true);//设置窗口是可见 - this.setFont(new Font("Calibre", Font.PLAIN, 60)); - //设置字体、样式、大小 + this.setFont(new Font("Calibre", Font.PLAIN, 60)); //设置字体、样式、大小 this.setLayout(null);//设置布局 resultText.setBounds(20, 5, 255, 40);//20,5是距离屏幕左右的像素,255和40是窗口的宽和高 resultText.setHorizontalAlignment(JTextField.RIGHT);//设置输出结果文本框的水平对齐方式 resultText.setEditable(false);//设置文本框不可以被修改 + resultText.setBackground(Color.WHITE);//设置文本框背景为白色 this.add(resultText);//新建文本框 int x=20,y=55; for (int i=0;i0) { - result.setText(resultText.substring(0,resultText.length()-1)); + if(label.equals("C")) { // 用户按了"C"键 + handleC();//setText选中一串字符串,直接等于0 } - } - - //算法3开方运算 + + else if(label.equals("CE")) //清除当前输入内容的一个数据 + { + handleCE(); + } + else if (label.equals("sqr"))//算法3:开方运算,调用Math.sqrt()函数实现 { - String n=yunsuan3(this.b); + String n=yunsuan1(this.b); // 用户按了"sqr"键 resultText.setText(n);//setText选中字符串n this.b=n;//计算式等于开方的答案 } - //算法4平方运算 else if(label.equals("x*x"))//算法4:平方运算,调用Math.pow()函数实现 { - String m=yunsuan4(this.b); + String m=yunsuan2(this.b); // 用户按了"x*x"键 resultText.setText(m);//setText选中字符串m this.b=m;//计算式等于平方的答案 } - else + else if("0123456789.".indexOf(label) >= 0) { - this.b=this.b+label;//遇到数字字符,直接加入后缀表达式 - resultText.setText(this.b); + handleNumber(); // 用户按了数字键或者小数点键 } - } - - - public String yunsuan1(String str) //算法1: - { - return null; - } - public String yunsuan2(String str) //算法2: - { - return null; + else { + handleOperator(); // 用户按了运算符键 } - - public String yunsuan3(String str)//算法3:开方运算调用Math.sqrt()函数实现 + } + + public String yunsuan1(String str)//算法1:开方运算调用Math.sqrt()函数实现 { String result; double a=Double.parseDouble(str); @@ -129,7 +111,7 @@ class Calculator extends JFrame implements ActionListener/* return result; } - public String yunsuan4(String str)//算法4:平方运算调用Math.pow()函数实现 + public String yunsuan2(String str)//算法2:平方运算调用Math.pow()函数实现 { String result; double a=Double.parseDouble(str); @@ -138,9 +120,45 @@ class Calculator extends JFrame implements ActionListener/* result=String.valueOf(b);//将b转换成字符串 return result; } + + public void handleNumber() { - public static void main(String arg[]) - { - Calculator c = new Calculator(); } + + public void handleCE() { + String text = resultText.getText(); // 用户按了"CE"键 + int i =text.length(); + if(i>0) { + text =text.substring(0, i-1); //将文本最后一个字符去掉 + if(i==0) {//如果文本框没有内容,则初始化计算器的各种值 + resultText.setText("0"); + } + } + else { + resultText.setText(text);//显示新的文本 + } + } + + public void handleC() { + + } + + public void handleOperator() { + + } + + +public double getNumberFromText() { + + try { + } + catch (NumberFormatException e) { + } + return 0; +} +public static void main(String[] args) { + Calculator c = new Calculator(); + c.setVisible(true); + c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); +} } \ No newline at end of file -- Gitee From c46386ea7036de41366ea7eb0da729f8953ebff8 Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Thu, 26 May 2022 22:46:03 +0800 Subject: [PATCH 13/21] =?UTF-8?q?=E5=86=99=E4=BA=86=E8=BF=90=E7=AE=97?= =?UTF-8?q?=E7=AC=A6=E9=83=A8=E5=88=86=E7=9A=84=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E7=95=A5=E6=9C=89=E5=8F=82=E8=80=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Calculator.java | 105 +++++++++++++++++------------ 1 file changed, 62 insertions(+), 43 deletions(-) diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index b8ff541..36f112e 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -14,7 +14,7 @@ public class Calculator extends JFrame implements ActionListener{// public boolean operateValidFlag = true;// 操作是否合法 JPanel panel; //按钮面板 - String b="";//b用于存放计算式 + public JTextField resultText = new JTextField("0");//结果显示文本框显示内容 String[] KEYS={"x*x","sqr","C","CE","%","0","1/x","÷","7","8","9","*","4","5","6","-", "1","2","3","+","(",")",".","="}; @@ -77,50 +77,16 @@ public class Calculator extends JFrame implements ActionListener{// { handleCE(); } - - else if (label.equals("sqr"))//算法3:开方运算,调用Math.sqrt()函数实现 - { - String n=yunsuan1(this.b); // 用户按了"sqr"键 - resultText.setText(n);//setText选中字符串n - this.b=n;//计算式等于开方的答案 - } - - else if(label.equals("x*x"))//算法4:平方运算,调用Math.pow()函数实现 - { - String m=yunsuan2(this.b); // 用户按了"x*x"键 - resultText.setText(m);//setText选中字符串m - this.b=m;//计算式等于平方的答案 - } else if("0123456789.".indexOf(label) >= 0) { handleNumber(); // 用户按了数字键或者小数点键 } else { - handleOperator(); // 用户按了运算符键 + handleOperator(label); // 用户按了运算符键 } } - - public String yunsuan1(String str)//算法1:开方运算调用Math.sqrt()函数实现 - { - String result; - double a=Double.parseDouble(str); - double b=0; - b=Math.sqrt(a);//调用Math.sqrt()函数实现 - result=String.valueOf(b);//将b转换成字符串 - return result; - } - - public String yunsuan2(String str)//算法2:平方运算调用Math.pow()函数实现 - { - String result; - double a=Double.parseDouble(str); - double b=0; - b=Math.pow(a, 2);//调用Math.pow()函数实现 - result=String.valueOf(b);//将b转换成字符串 - return result; - } - + public void handleNumber() { } @@ -140,21 +106,74 @@ public class Calculator extends JFrame implements ActionListener{// } public void handleC() { - + resultText.setText("0"); + firstDigit=true; + operator="="; } - public void handleOperator() { - - } + public void handleOperator(String key) { + if(operator.equals("/")) { + // 除法运算 + // 用户按了"/"键 + if(getNumberFromText() == 0.0) {// 如果当前结果文本框中的值等于 + operateValidFlag = false;//操作不合法 + resultText.setText("除数不能为零"); + } + else + { + resultNum /= getNumberFromText(); + } + } + else if (operator.equals("1/x")) + { + // 倒数运算 + // 用户按了"1/x"键 + if (resultNum == 0.0){ + operateValidFlag = false;// 操作不合法 + resultText.setText("零没有倒数"); + } + else { + resultNum = 1 / resultNum; + } + } + else if (operator.equals("+")) { + // 加法运算 + // 用户按了"+"键 + resultNum += getNumberFromText(); + } + else if (operator.equals("-")) { + // 减法运算 + // 用户按了"-"键 + resultNum -= getNumberFromText(); + } + else if (operator.equals("*")) { + // 乘法运算 + // 用户按了"*"键 + resultNum *= getNumberFromText(); + } + else if (operator.equals("%")) { + // 百分号运算,除以100 + // 用户按了"%"键 + resultNum = resultNum / 100; + } + else if (operator.equals("=")) { + // 赋值运算 + // 用户按了"="键 + resultNum = getNumberFromText(); + } + operator = key; + firstDigit = true; + } public double getNumberFromText() { - + double result = 0; + try { } catch (NumberFormatException e) { } - return 0; + return result; } public static void main(String[] args) { Calculator c = new Calculator(); -- Gitee From dd9990b5765a051416cc7759b4eec704d13b52ab Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Sat, 28 May 2022 17:35:15 +0800 Subject: [PATCH 14/21] =?UTF-8?q?=E8=80=81=E5=B8=88=EF=BC=8C=E5=8E=9F?= =?UTF-8?q?=E8=B0=85=E6=88=91=E7=AA=81=E7=84=B6=E6=9B=B4=E6=94=B9=E9=A2=98?= =?UTF-8?q?=E7=9B=AE=EF=BC=8C=E8=AE=A1=E7=AE=97=E5=99=A8=E4=BF=AE=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=94=B9=E5=A5=BD=E5=87=A0=E5=A4=A9=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B6=8A=E6=94=B9=E8=B6=8A=E4=B9=B1=EF=BC=8C?= =?UTF-8?q?=E6=88=91=E5=B7=B2=E7=BB=8F=E8=A6=81=E5=82=BB=E4=BA=86=EF=BC=8C?= =?UTF-8?q?=E6=8C=BA=E5=96=9C=E6=AC=A2=E7=8E=A9=E4=BA=94=E5=AD=90=E6=A3=8B?= =?UTF-8?q?=E7=9A=84=EF=BC=8C=E4=BB=8A=E5=A4=A9=E5=9C=A8B=E7=AB=99?= =?UTF-8?q?=E4=B8=8A=E5=AD=A6=E4=BA=86=E4=B8=80=E4=B8=8B=EF=BC=8C=E6=88=91?= =?UTF-8?q?=E8=A7=89=E5=BE=97=E6=88=91=E5=86=99=E4=BA=94=E5=AD=90=E6=A3=8B?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=A5=BD=E4=B8=80=E7=82=B9=EF=BC=8C=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A8=E6=88=91=E7=9C=9F=E7=9A=84=E5=86=99=E4=B8=8D?= =?UTF-8?q?=E4=B8=8B=E5=8E=BB=E4=BA=86=EF=BC=8C=E7=AE=97=E6=B3=95=E5=A4=AA?= =?UTF-8?q?=E5=A4=9A=E4=BA=86=EF=BC=8C=E8=84=91=E5=AD=90=E8=BD=AC=E4=B8=8D?= =?UTF-8?q?=E8=BF=87=E6=9D=A5=EF=BC=8C=E5=91=A8=E6=9C=AB=E8=BF=99=E4=B8=A4?= =?UTF-8?q?=E5=A4=A9=E6=88=91=E9=83=BD=E4=BC=9A=E5=9C=A8=E5=86=99=E4=BA=94?= =?UTF-8?q?=E5=AD=90=E6=A3=8B=EF=BC=8C=E4=BA=89=E5=8F=96=E6=8C=89=E6=97=B6?= =?UTF-8?q?=E4=BA=A4=E4=BD=9C=E4=B8=9A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Wuziqi.java | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/java2022spring/Wuziqi.java diff --git a/src/java2022spring/Wuziqi.java b/src/java2022spring/Wuziqi.java new file mode 100644 index 0000000..2d722ab --- /dev/null +++ b/src/java2022spring/Wuziqi.java @@ -0,0 +1,58 @@ +package java2022spring; +import javax.swing.*; + +import java.awt.*; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +//窗体设置 +public class Wuziqi { + public static void main(String[] args) { + @SuppressWarnings("unused") + Main1 m = new Main1(); + } +} + +class Main1 extends JFrame implements MouseListener { + int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 获取屏幕宽度 + int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 获取屏幕高度 + + public Main1() { + this.setTitle("五子棋"); // 标题 + this.setSize(800, 600); // 窗口大小 + this.setLocation((width - 800) / 2, (height - 600) / 2); // 设置窗口默认位置以屏幕居中 + this.setResizable(false); // 窗口是否可以改变大小=否 + this.setDefaultCloseOperation(Main1.EXIT_ON_CLOSE);// 窗口关闭方式为关闭窗口同时结束程序 + this.setVisible(true); // 窗口是否显示=是 + } + + @Override + public void mouseClicked(MouseEvent e) { + // TODO 自动生成的方法存根 + } + + @Override + public void mousePressed(MouseEvent e) { + // TODO 自动生成的方法存根 + } + + @Override + public void mouseReleased(MouseEvent e) { + // TODO 自动生成的方法存根 + } + + @Override + public void mouseEntered(MouseEvent e) { + // TODO 自动生成的方法存根 + } + + @Override + public void mouseExited(MouseEvent e) { + // TODO 自动生成的方法存根 + } +} + + + + + + -- Gitee From 23a83671ec04775c72384d50dc516968c9bfa8f6 Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Sun, 29 May 2022 23:35:23 +0800 Subject: [PATCH 15/21] =?UTF-8?q?=E6=8E=A5=E8=BF=91=E4=B8=A4=E5=A4=A9?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=E5=9C=A8B=E7=AB=99=E4=B8=8A=E9=9D=A2?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E4=BA=94=E5=AD=90=E6=A3=8B=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=9F=A5=E8=AF=86=E3=80=81=E5=B8=83=E5=B1=80=EF=BC=8C=E4=BB=8A?= =?UTF-8?q?=E5=A4=A9=E7=BB=88=E4=BA=8E=E6=8A=8A=E6=A3=8B=E7=9B=98=E5=B8=83?= =?UTF-8?q?=E5=B1=80=E8=AE=BE=E7=BD=AE=E5=86=99=E5=A5=BD=EF=BC=8C=E5=A4=A7?= =?UTF-8?q?=E8=87=B4=E6=A1=86=E6=9E=B6=E4=B9=9F=E5=86=99=E5=87=BA=E6=9D=A5?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=E4=B8=B4=E6=97=B6=E6=8D=A2=E9=A2=98=E7=9B=AE?= =?UTF-8?q?=E4=B9=9F=E6=B2=A1=E6=9C=89=E9=82=A3=E4=B9=88=E7=9D=80=E6=80=A5?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=E4=BD=86=E6=98=AF=E7=9C=9F=E7=9A=84=E7=9C=9F?= =?UTF-8?q?=E7=9A=84=E5=A5=BD=E9=9A=BE=EF=BC=8C=E7=BB=88=E4=BA=8E=E7=90=86?= =?UTF-8?q?=E8=A7=A3=E7=A8=8B=E5=BA=8F=E5=91=98=E8=AE=A8=E5=8E=8Cbug?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=E4=BD=86=E6=84=BF=E7=A8=8B=E5=BA=8F=E5=86=8D?= =?UTF-8?q?=E6=97=A0bug=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9C=9F=E7=9A=84=E5=A4=AA=E9=9A=BE=E4=BA=86=EF=BC=8C=E5=85=B3?= =?UTF-8?q?=E9=94=AE=E6=98=AF=E6=97=A0=E4=BB=8E=E4=B8=8B=E6=89=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Text.java | 59 ++++++++++ src/java2022spring/Wuziqi.java | 205 ++++++++++++++++++++++++++------- 2 files changed, 225 insertions(+), 39 deletions(-) create mode 100644 src/java2022spring/Text.java diff --git a/src/java2022spring/Text.java b/src/java2022spring/Text.java new file mode 100644 index 0000000..1dd37dd --- /dev/null +++ b/src/java2022spring/Text.java @@ -0,0 +1,59 @@ +package java2022spring; +import javax.swing.*; + +import java.awt.*; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +//窗体设置 +public class Text { + public static void main(String[] args) { + @SuppressWarnings("unused") + Main1 m = new Main1(); + } +} + +@SuppressWarnings("serial") +class Main1 extends JFrame implements MouseListener { + int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 获取屏幕宽度 + int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 获取屏幕高度 + + public Main1() { + this.setTitle("五子棋"); // 标题 + this.setSize(800, 600); // 窗口大小 + this.setLocation((width - 800) / 2, (height - 600) / 2); // 设置窗口默认位置以屏幕居中 + this.setResizable(false); // 窗口是否可以改变大小=否 + this.setDefaultCloseOperation(Main1.EXIT_ON_CLOSE);// 窗口关闭方式为关闭窗口同时结束程序 + this.setVisible(true); // 窗口是否显示=是 + } + + @Override + public void mouseClicked(MouseEvent e) { + // TODO 自动生成的方法存根 + } + + @Override + public void mousePressed(MouseEvent e) { + // TODO 自动生成的方法存根 + } + + @Override + public void mouseReleased(MouseEvent e) { + // TODO 自动生成的方法存根 + } + + @Override + public void mouseEntered(MouseEvent e) { + // TODO 自动生成的方法存根 + } + + @Override + public void mouseExited(MouseEvent e) { + // TODO 自动生成的方法存根 + } +} + + + + + + diff --git a/src/java2022spring/Wuziqi.java b/src/java2022spring/Wuziqi.java index 2d722ab..1a9dec3 100644 --- a/src/java2022spring/Wuziqi.java +++ b/src/java2022spring/Wuziqi.java @@ -1,58 +1,185 @@ package java2022spring; import javax.swing.*; - import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; -//窗体设置 +import java.awt.image.BufferedImage; +//棋盘设置 public class Wuziqi { - public static void main(String[] args) { - @SuppressWarnings("unused") - Main1 m = new Main1(); + public static void main(String[] args) { + MyJFrame mj=new MyJFrame(); + mj.myJFrame(); + } } -} -class Main1 extends JFrame implements MouseListener { - int width = Toolkit.getDefaultToolkit().getScreenSize().width;// 获取屏幕宽度 - int height = Toolkit.getDefaultToolkit().getScreenSize().height;// 获取屏幕高度 - - public Main1() { - this.setTitle("五子棋"); // 标题 - this.setSize(800, 600); // 窗口大小 - this.setLocation((width - 800) / 2, (height - 600) / 2); // 设置窗口默认位置以屏幕居中 - this.setResizable(false); // 窗口是否可以改变大小=否 - this.setDefaultCloseOperation(Main1.EXIT_ON_CLOSE);// 窗口关闭方式为关闭窗口同时结束程序 - this.setVisible(true); // 窗口是否显示=是 - } + class MyJFrame extends JFrame implements MouseListener,Runnable { + +private static final long serialVersionUID = 7547087204849324043L;//此处有参考代码 + int qx = 20, qy = 40, qw = 490, qh = 490;//棋盘位置、宽高 + int bw = 150, bh = 50, bx = 570, by = 150;//按钮宽高、位置 + int x = 0, y = 0;//保存棋子坐标 + int[][] SaveGame = new int[15][15];//保存每个棋子 + int qc = 1;//记录白棋=2,黑棋=1 + int qn = 0;//判断棋子是否重复 + boolean canplay = true;//判断游戏是否开始和结束 + String go = "黑子先行";//游戏信息 + int maxtime=0;//设置最大时间 + Thread t=new Thread(this);//做倒计时的线程类 + int blacktime=0; + int whitetime=0;//设置双方剩余时间 + String blackmessage= "无限制"; + String whitemessage="无限制"; + //窗体 + + @SuppressWarnings("removal") +public void myJFrame() { + this.setTitle("五子棋"); //标题 + this.setSize(800, 600); //窗口大小 + this.setResizable(false); //窗口是否可以改变大小=否 + this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);//窗口关闭方式为关闭窗口同时结束程序 + int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度 + int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度 +// System.out.println("宽度:"+width);//测试 +// System.out.println("高度:"+height);//测试 + this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗口默认位置以屏幕居中 + this.addMouseListener(this); + this.setVisible(true); //窗口是否显示=是 + t.start(); + t.suspend(); + + } - @Override - public void mouseClicked(MouseEvent e) { - // TODO 自动生成的方法存根 - } + //覆写paint方法,绘制界面 + public void paint(Graphics g) {//此处有参考代码 - @Override - public void mousePressed(MouseEvent e) { - // TODO 自动生成的方法存根 - } + //双缓冲技术防止屏幕闪烁 + BufferedImage bi = new BufferedImage(800, 550, BufferedImage.TYPE_INT_ARGB); + Graphics g2 = bi.createGraphics(); - @Override - public void mouseReleased(MouseEvent e) { - // TODO 自动生成的方法存根 - } + g2.setColor(Color.BLACK);//设置画笔颜色 + g2.setFont(new Font("宋体", 10, 50));//设置字体 + g2.drawString("五子棋", 565, 100);//绘制字符 - @Override - public void mouseEntered(MouseEvent e) { - // TODO 自动生成的方法存根 - } + //棋盘 + g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色 + g2.fillRect(qx, qy, qw, qh);//绘制棋盘背景矩形 + + //开始按钮 + g2.setColor(Color.WHITE);//设置画笔颜色 + g2.fillRect(bx, by, bw, bh);//绘制开始按钮 + g2.setFont(new Font("黑体", 10, 30));//设置字体 + g2.setColor(Color.black);//设置画笔颜色 + g2.drawString("开始", 615, 185);//绘制字符 - @Override - public void mouseExited(MouseEvent e) { - // TODO 自动生成的方法存根 - } -} + //悔棋按钮 + g2.setColor(Color.LIGHT_GRAY);//设置画笔颜色 + g2.fillRect(bx, by + 60, bw, bh);//绘制悔棋按钮 + g2.setFont(new Font("黑体", 10, 30));//设置字体 + g2.setColor(Color.WHITE);//设置画笔颜色 + g2.drawString("悔棋", 615, 245);//绘制字符 + + //认输按钮 + g2.setColor(Color.GRAY);//设置画笔颜色 + g2.fillRect(bx, by + 120, bw, bh);//绘制认输按钮 + g2.setFont(new Font("黑体", 10, 30));//设置字体 + g2.setColor(Color.WHITE);//设置画笔颜色 + g2.drawString("认输", 615, 305);//绘制字符 + + //游戏信息栏 + g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色 + g2.fillRect(550, 350, 200, 70);//绘制游戏状态区域 + g2.setColor(Color.black);//设置画笔颜色 + g2.setFont(new Font("黑体", 10, 20));//设置字体 + g2.drawString("游戏信息", 610, 380);//绘制字符 + g2.drawString(go, 610, 410);//绘制字符,加载初始信息提示 + + //游戏设置栏 + g2.setColor(Color.orange); + g2.drawRect(600, 430, bw-50, bh-20); + g2.setFont(new Font("宋体", 10,20)); + g2.setColor(Color.pink); + g2.drawString("时间设置",610 , 450); + g2.setColor(Color.BLACK); + g2.drawString("黑方时间", 550, 480); + g2.setColor(Color.WHITE); + g2.drawString("剩余时间:", 530, 510); + g2.drawString(blackmessage,550 , 530); + g2.setColor(Color.BLACK); + g2.drawString("白方时间", 680, 480); + g2.setColor(Color.WHITE); + g2.drawString("剩余时间:", 660, 510); + g2.drawString(whitemessage, 680, 530); + g2.setColor(Color.BLACK);//设置画笔颜色 + + //绘制棋盘格线 + for (int x = 0; x <= qw; x += 35) { + g2.drawLine(qx, x + qy, qw + qx, x + qy);//绘制一条横线 + g2.drawLine(x + qx, qy, x + qx, qh + qy);//绘制一条竖线 + } + //绘制标注点 + for (int i = 3; i <= 11; i += 4) { + for (int y = 3; y <= 11; y += 4) { + g2.fillOval(35 * i + qx - 3, 35 * y + qy - 3, 6, 6);//绘制实心圆 + } + } + //绘制棋子 + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + + if (SaveGame[i][j] == 1)//黑子 + { + int sx = i * 35 + qx;//坐标 + int sy = j * 35 + qy; + g2.setColor(Color.BLACK); + g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 + } + + if (SaveGame[i][j] == 2)//白子 + { + int sx = i * 35 + qx; + int sy = j * 35 + qy; + g2.setColor(Color.WHITE); + g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 + g2.setColor(Color.BLACK); + g2.drawOval(sx - 13, sy - 13, 26, 26);//绘制空心圆 + } + } + } + g.drawImage(bi, 0, 0, this); + } + + + @Override//鼠标点击 + public void mouseClicked(MouseEvent e) { + } + @Override//鼠标按下 + public void mousePressed(MouseEvent e) { + + } + + @Override//鼠标抬起 + public void mouseReleased(MouseEvent e) { + + } + + @Override//鼠标进入 + public void mouseEntered(MouseEvent e) { + + } + + @Override//鼠标离开 + public void mouseExited(MouseEvent e) { + + } + +@Override +public void run() { + +} +} -- Gitee From 089938ffa93ec98cab6ccb3643c629627af5836a Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Tue, 31 May 2022 12:45:27 +0800 Subject: [PATCH 16/21] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E4=B8=8B?= =?UTF-8?q?=E6=A3=8B=E5=8A=9F=E8=83=BD=E5=92=8C=E5=88=A4=E6=96=AD=E8=BE=93?= =?UTF-8?q?=E8=B5=A2=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=9C=89=E4=B8=80=E7=82=B9?= =?UTF-8?q?=E9=9A=BE=E5=BA=A6=EF=BC=8C=E4=BD=86=E6=98=AF=E4=BA=94=E5=AD=90?= =?UTF-8?q?=E6=A3=8B=E7=9C=9F=E7=9A=84=E5=A5=BD=E5=A5=BD=E7=8E=A9=EF=BC=8C?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=8A=9F=E8=83=BD=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E6=BB=A1=E6=BB=A1=E7=9A=84=E6=88=90=E5=B0=B1=E6=84=9F=E3=80=82?= =?UTF-8?q?=E4=BD=86=E6=98=AF=E9=87=8D=E6=96=B0=E6=B8=B8=E6=88=8F=E3=80=81?= =?UTF-8?q?=E6=82=94=E6=A3=8B=E5=8A=9F=E8=83=BD=E4=BB=A5=E5=8F=8A=E5=80=92?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E4=BB=80=E4=B9=88=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=9C=89=E7=82=B9=E9=9A=BE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Wuziqi.java | 105 ++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 27 deletions(-) diff --git a/src/java2022spring/Wuziqi.java b/src/java2022spring/Wuziqi.java index 1a9dec3..eeba39f 100644 --- a/src/java2022spring/Wuziqi.java +++ b/src/java2022spring/Wuziqi.java @@ -1,10 +1,13 @@ package java2022spring; +import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; -//棋盘设置 +import java.io.File; +import java.io.IOException; + public class Wuziqi { public static void main(String[] args) { MyJFrame mj=new MyJFrame(); @@ -13,9 +16,11 @@ public class Wuziqi { } class MyJFrame extends JFrame implements MouseListener,Runnable { - -private static final long serialVersionUID = 7547087204849324043L;//此处有参考代码 - int qx = 20, qy = 40, qw = 490, qh = 490;//棋盘位置、宽高 + /** + * + */ + private static final long serialVersionUID = 7547087204849324043L; +int qx = 20, qy = 40, qw = 490, qh = 490;//棋盘位置、宽高 int bw = 150, bh = 50, bx = 570, by = 150;//按钮宽高、位置 int x = 0, y = 0;//保存棋子坐标 int[][] SaveGame = new int[15][15];//保存每个棋子 @@ -29,33 +34,39 @@ private static final long serialVersionUID = 7547087204849324043L;// int whitetime=0;//设置双方剩余时间 String blackmessage= "无限制"; String whitemessage="无限制"; + //--------------------------------------------------------------------------------------------------------------------- //窗体 - @SuppressWarnings("removal") public void myJFrame() { + this.setTitle("五子棋"); //标题 this.setSize(800, 600); //窗口大小 this.setResizable(false); //窗口是否可以改变大小=否 this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);//窗口关闭方式为关闭窗口同时结束程序 + int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度 int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度 // System.out.println("宽度:"+width);//测试 // System.out.println("高度:"+height);//测试 + this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗口默认位置以屏幕居中 + this.addMouseListener(this); + this.setVisible(true); //窗口是否显示=是 t.start(); t.suspend(); } + + //--------------------------------------------------------------------------------------------------------------------- //覆写paint方法,绘制界面 - public void paint(Graphics g) {//此处有参考代码 + public void paint(Graphics g) { //双缓冲技术防止屏幕闪烁 BufferedImage bi = new BufferedImage(800, 550, BufferedImage.TYPE_INT_ARGB); Graphics g2 = bi.createGraphics(); - g2.setColor(Color.BLACK);//设置画笔颜色 g2.setFont(new Font("宋体", 10, 50));//设置字体 g2.drawString("五子棋", 565, 100);//绘制字符 @@ -63,7 +74,6 @@ public void myJFrame() { //棋盘 g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色 g2.fillRect(qx, qy, qw, qh);//绘制棋盘背景矩形 - //开始按钮 g2.setColor(Color.WHITE);//设置画笔颜色 g2.fillRect(bx, by, bw, bh);//绘制开始按钮 @@ -94,11 +104,12 @@ public void myJFrame() { g2.drawString(go, 610, 410);//绘制字符,加载初始信息提示 //游戏设置栏 - g2.setColor(Color.orange); + g2.setColor(Color.pink); g2.drawRect(600, 430, bw-50, bh-20); g2.setFont(new Font("宋体", 10,20)); - g2.setColor(Color.pink); + g2.setColor(Color.orange); g2.drawString("时间设置",610 , 450); + g2.setColor(Color.BLACK); g2.drawString("黑方时间", 550, 480); g2.setColor(Color.WHITE); @@ -109,8 +120,8 @@ public void myJFrame() { g2.setColor(Color.WHITE); g2.drawString("剩余时间:", 660, 510); g2.drawString(whitemessage, 680, 530); - g2.setColor(Color.BLACK);//设置画笔颜色 + g2.setColor(Color.BLACK);//设置画笔颜色 //绘制棋盘格线 for (int x = 0; x <= qw; x += 35) { g2.drawLine(qx, x + qy, qw + qx, x + qy);//绘制一条横线 @@ -118,24 +129,22 @@ public void myJFrame() { } //绘制标注点 - for (int i = 3; i <= 11; i += 4) { + for (int i = 3; i <= 11; i += 4) { for (int y = 3; y <= 11; y += 4) { g2.fillOval(35 * i + qx - 3, 35 * y + qy - 3, 6, 6);//绘制实心圆 } - } + } //绘制棋子 - for (int i = 0; i < 15; i++) { + for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { - - if (SaveGame[i][j] == 1)//黑子 + if (SaveGame[i][j] == 1)//黑子 { - int sx = i * 35 + qx;//坐标 - int sy = j * 35 + qy; - g2.setColor(Color.BLACK); - g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 + int sx = i * 35 + qx;//坐标 + int sy = j * 35 + qy; + g2.setColor(Color.BLACK); + g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 } - if (SaveGame[i][j] == 2)//白子 { int sx = i * 35 + qx; @@ -150,8 +159,8 @@ public void myJFrame() { g.drawImage(bi, 0, 0, this); } - - + + //--------------------------------------------------------------------------------------------------------------------- @Override//鼠标点击 public void mouseClicked(MouseEvent e) { @@ -159,9 +168,51 @@ public void myJFrame() { @Override//鼠标按下 public void mousePressed(MouseEvent e) { - + //获取鼠标点击位置 + x = e.getX(); + y = e.getY(); + + //判断是否已开始游戏 + if (canplay == true)//canplay如果为false则不执行下棋的操作,结束游戏 + { + //判断点击是否为棋盘内 + if (x > qx && x < qx + qw && y > qy && y < qy + qh) { + //计算点击位置最近的点 + if ((x - qx) % 35 > 17) { + x = (x - qx) / 35 + 1; + } else { + x = (x - qx) / 35; + } + if ((y - qy) % 35 > 17) { + y = (y - qy) / 35 + 1; + } else { + y = (y - qy) / 35; + } + + //判断当前位置有没有棋子 + if (SaveGame[x][y] == 0) { + SaveGame[x][y] = qc; + qn = 0; + } else { + qn = 1; + JOptionPane.showMessageDialog(this, "该位置已经有棋子,请重新落子"); + } + + //切换棋子 + if (qn == 0) { + if (qc == 1) { + qc = 2; + go = "轮到白子"; + } else { + qc = 1; + go = "轮到黑子"; + }} + this.repaint(); + } + } } + @Override//鼠标抬起 public void mouseReleased(MouseEvent e) { @@ -177,9 +228,9 @@ public void myJFrame() { } + @Override public void run() { - + } -} - +} \ No newline at end of file -- Gitee From 88f2ef78fc372ebf74e44d53f64c5803082fe646 Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Wed, 1 Jun 2022 14:51:07 +0800 Subject: [PATCH 17/21] =?UTF-8?q?=E5=86=99=E4=BA=86=E7=82=B9=E5=87=BB?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E5=B0=B1=E5=8F=AF=E4=BB=A5=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E8=80=83=E8=AF=95=E7=9A=84=E6=8C=89=E9=92=AE?= =?UTF-8?q?=EF=BC=8C=E6=AD=A3=E5=9C=A8=E5=86=99=E6=82=94=E6=A3=8B=E3=80=81?= =?UTF-8?q?=E8=AE=A4=E8=BE=93=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=85=88=E6=8A=8A?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=BC=80=E5=A7=8B=E5=8A=9F=E8=83=BD=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81push=E5=90=A7=EF=BC=8C=E5=9C=A8=E7=BC=96?= =?UTF-8?q?=E5=86=99=E8=BF=99=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E8=BF=87=E7=A8=8B=E4=B8=AD=EF=BC=8C=E6=9C=89=E7=82=B9=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E4=BA=86=E6=80=9D=E8=B7=AF=E6=B7=B7=E4=B9=B1=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E4=B8=8D=E7=9F=A5=E9=81=93=E8=AF=A5?= =?UTF-8?q?=E4=BB=8E=E5=93=AA=E9=87=8C=E4=B8=8B=E6=89=8B=EF=BC=8C=E5=90=8E?= =?UTF-8?q?=E6=9D=A5=E9=80=9A=E8=BF=87=E6=9F=A5=E6=89=BE=E8=B5=84=E6=96=99?= =?UTF-8?q?=E7=90=86=E6=B8=85=E4=BA=86=E6=80=9D=E8=B7=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Wuziqi.java | 143 +++++++++++++++++++++++++++++---- 1 file changed, 128 insertions(+), 15 deletions(-) diff --git a/src/java2022spring/Wuziqi.java b/src/java2022spring/Wuziqi.java index eeba39f..157623c 100644 --- a/src/java2022spring/Wuziqi.java +++ b/src/java2022spring/Wuziqi.java @@ -1,12 +1,9 @@ package java2022spring; -import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; public class Wuziqi { public static void main(String[] args) { @@ -19,7 +16,7 @@ public class Wuziqi { /** * */ - private static final long serialVersionUID = 7547087204849324043L; +private static final long serialVersionUID = 7547087204849324043L; int qx = 20, qy = 40, qw = 490, qh = 490;//棋盘位置、宽高 int bw = 150, bh = 50, bx = 570, by = 150;//按钮宽高、位置 int x = 0, y = 0;//保存棋子坐标 @@ -52,18 +49,14 @@ public void myJFrame() { this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗口默认位置以屏幕居中 this.addMouseListener(this); - this.setVisible(true); //窗口是否显示=是 t.start(); t.suspend(); } - - //--------------------------------------------------------------------------------------------------------------------- //覆写paint方法,绘制界面 public void paint(Graphics g) { - //双缓冲技术防止屏幕闪烁 BufferedImage bi = new BufferedImage(800, 550, BufferedImage.TYPE_INT_ARGB); Graphics g2 = bi.createGraphics(); @@ -104,10 +97,10 @@ public void myJFrame() { g2.drawString(go, 610, 410);//绘制字符,加载初始信息提示 //游戏设置栏 - g2.setColor(Color.pink); + g2.setColor(Color.orange); g2.drawRect(600, 430, bw-50, bh-20); g2.setFont(new Font("宋体", 10,20)); - g2.setColor(Color.orange); + g2.setColor(Color.pink); g2.drawString("时间设置",610 , 450); g2.setColor(Color.BLACK); @@ -121,7 +114,11 @@ public void myJFrame() { g2.drawString("剩余时间:", 660, 510); g2.drawString(whitemessage, 680, 530); + + + g2.setColor(Color.BLACK);//设置画笔颜色 + //绘制棋盘格线 for (int x = 0; x <= qw; x += 35) { g2.drawLine(qx, x + qy, qw + qx, x + qy);//绘制一条横线 @@ -134,7 +131,7 @@ public void myJFrame() { g2.fillOval(35 * i + qx - 3, 35 * y + qy - 3, 6, 6);//绘制实心圆 } } - + //绘制棋子 for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { @@ -157,8 +154,81 @@ public void myJFrame() { } } g.drawImage(bi, 0, 0, this); - } + + private boolean WinLose() { + boolean flag = false;//输赢 + int count = 1;//相连数 + int color = SaveGame[x][y];//记录棋子颜色 + + //判断横向棋子是否相连 + int i = 1;//迭代数 + while (color == SaveGame[x + i][y])//y值相同,使用循环来判断,向右判断 + { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x - i][y])//向左判断 + { + count++; + i++; + } + if (count >= 5) { + flag = true; + } + + //判断纵向棋子是否相连 + count = 1; + i = 1;//迭代数 + while (color == SaveGame[x][y + i]) { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x][y - i]) { + count++; + i++; + } + if (count >= 5) { + flag = true; + } + + //判断斜向棋子是否相连(左上右下) + count = 1; + i = 1;//迭代数 + while (color == SaveGame[x - i][y - i]) { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x + i][y + i]) { + count++; + i++; + } + if (count >= 5) { + flag = true; + } + + //判断斜向棋子是否相连(左下右上) + count = 1; + i = 1;//迭代数 + while (color == SaveGame[x + i][y - i]) { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x - i][y + i]) { + count++; + i++; + } + if (count >= 5) { + flag = true; + } + + return flag; + } + //--------------------------------------------------------------------------------------------------------------------- @Override//鼠标点击 @@ -172,6 +242,7 @@ public void myJFrame() { x = e.getX(); y = e.getY(); + //判断是否已开始游戏 if (canplay == true)//canplay如果为false则不执行下棋的操作,结束游戏 { @@ -208,11 +279,54 @@ public void myJFrame() { go = "轮到黑子"; }} this.repaint(); - } + boolean wl = this.WinLose(); + if (wl) { + JOptionPane.showMessageDialog(this, "游戏结束," + (SaveGame[x][y] == 1 ? "黑方赢了" : "白方赢了"));//弹出提示对话框 + canplay = false;//结束游戏进程 + } +}} + if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by && e.getY() < by + bh) { + //判断游戏是否开始 + if (canplay == false) { + //如果游戏结束,则开始游戏 + canplay = true; + JOptionPane.showMessageDialog(this, "游戏开始"); + //初始化游戏 + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + SaveGame[i][j] = 0; + } + } + + //黑子先行 + qc = 1; + go = "轮到黑子"; + + repaint();//重新执行一次paint方法 + + } else { + //如果游戏进行中,则重新开始 + JOptionPane.showMessageDialog(this, "重新开始"); + //初始化游戏 + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + SaveGame[i][j] = 0; + } + } + + //黑子先行 + qc = 1; + go = "轮到黑子"; + + blacktime=maxtime; + whitetime=maxtime; + + //重新执行一次paint方法 + repaint(); + } } } - @Override//鼠标抬起 public void mouseReleased(MouseEvent e) { @@ -228,7 +342,6 @@ public void myJFrame() { } - @Override public void run() { -- Gitee From f350d3770010b0096790e4b17d641a1aaa17f97a Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Wed, 1 Jun 2022 15:46:41 +0800 Subject: [PATCH 18/21] =?UTF-8?q?=E8=AE=A4=E8=BE=93=E3=80=81=E6=82=94?= =?UTF-8?q?=E6=A3=8B=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=AE=9E=E7=8E=B0=E4=BA=86?= =?UTF-8?q?=E8=AE=A4=E8=BE=93=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BD=86=E6=98=AF?= =?UTF-8?q?=E6=82=94=E6=A3=8B=E5=8A=9F=E8=83=BD=E6=B2=A1=E6=9C=89=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=EF=BC=8CB=E7=AB=99=E4=B8=8A=E5=BE=88=E5=A4=9A?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E8=A7=86=E9=A2=91=EF=BC=8C=E4=BD=86=E6=98=AF?= =?UTF-8?q?=E5=AD=A6=E4=B9=A0=E4=BA=86=E5=BE=88=E4=B9=85=EF=BC=8C=E4=BD=86?= =?UTF-8?q?=E6=98=AF=E6=88=91=E6=84=9F=E8=A7=89=E6=88=91=E4=B8=8D=E5=A4=AA?= =?UTF-8?q?=E8=83=BD=E5=86=99=E5=87=BA=E6=9D=A5=E4=BA=86=EF=BC=8C=E5=86=99?= =?UTF-8?q?=E4=B8=AA=E7=AE=80=E5=8D=95=E7=9A=84=E4=B8=8D=E8=83=BD=E6=82=94?= =?UTF-8?q?=E6=A3=8B=E5=92=8C=E8=AE=A4=E8=BE=93=E5=8A=9F=E8=83=BD=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E5=BE=88=E5=90=83=E5=8A=9B=E4=BA=86=EF=BC=8C=E7=95=99?= =?UTF-8?q?=E7=82=B9=E6=97=B6=E9=97=B4=E6=B5=8B=E8=AF=95=E5=86=99=E6=8A=A5?= =?UTF-8?q?=E5=91=8A=EF=BC=8C=E6=84=9F=E8=A7=89=E5=BF=AB=E8=A6=81=E6=9D=A5?= =?UTF-8?q?=E4=B8=8D=E5=8F=8A=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Wuziqi.java | 682 +++++++++++++++++---------------- 1 file changed, 360 insertions(+), 322 deletions(-) diff --git a/src/java2022spring/Wuziqi.java b/src/java2022spring/Wuziqi.java index 157623c..d738c06 100644 --- a/src/java2022spring/Wuziqi.java +++ b/src/java2022spring/Wuziqi.java @@ -1,349 +1,387 @@ package java2022spring; -import javax.swing.*; -import java.awt.*; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.awt.image.BufferedImage; - -public class Wuziqi { - public static void main(String[] args) { - MyJFrame mj=new MyJFrame(); - mj.myJFrame(); + import javax.swing.*; + import java.awt.*; + import java.awt.event.MouseEvent; + import java.awt.event.MouseListener; + import java.awt.image.BufferedImage; + + public class Wuziqi { + public static void main(String[] args) { + MyJFrame mj=new MyJFrame(); + mj.myJFrame(); + } + } + class MyJFrame extends JFrame implements MouseListener,Runnable { + /** + * + */ + private static final long serialVersionUID = 7547087204849324043L; + int qx = 20, qy = 40, qw = 490, qh = 490;//棋盘位置、宽高 + int bw = 150, bh = 50, bx = 570, by = 150;//按钮宽高、位置 + int x = 0, y = 0;//保存棋子坐标 + int[][] SaveGame = new int[15][15];//保存每个棋子 + int qc = 1;//记录白棋=2,黑棋=1 + int qn = 0;//判断棋子是否重复 + boolean canplay = true;//判断游戏是否开始和结束 + String go = "黑子先行";//游戏信息 + int maxtime=0;//设置最大时间 + Thread t=new Thread(this);//做倒计时的线程类 + int blacktime=0; + int whitetime=0;//设置双方剩余时间 + String blackmessage= "无限制"; + String whitemessage="无限制"; + //--------------------------------------------------------------------------------------------------------------------- + //窗体 + @SuppressWarnings("removal") + public void myJFrame() { + this.setTitle("五子棋"); //标题 + this.setSize(800, 600); //窗口大小 + this.setResizable(false); //窗口是否可以改变大小=否 + this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);//窗口关闭方式为关闭窗口同时结束程序 + + int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度 + int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度 + // System.out.println("宽度:"+width);//测试 + // System.out.println("高度:"+height);//测试 + + this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗口默认位置以屏幕居中 + this.addMouseListener(this); + this.setVisible(true); //窗口是否显示=是 + t.start(); + t.suspend(); } - } - - class MyJFrame extends JFrame implements MouseListener,Runnable { - /** - * - */ -private static final long serialVersionUID = 7547087204849324043L; -int qx = 20, qy = 40, qw = 490, qh = 490;//棋盘位置、宽高 - int bw = 150, bh = 50, bx = 570, by = 150;//按钮宽高、位置 - int x = 0, y = 0;//保存棋子坐标 - int[][] SaveGame = new int[15][15];//保存每个棋子 - int qc = 1;//记录白棋=2,黑棋=1 - int qn = 0;//判断棋子是否重复 - boolean canplay = true;//判断游戏是否开始和结束 - String go = "黑子先行";//游戏信息 - int maxtime=0;//设置最大时间 - Thread t=new Thread(this);//做倒计时的线程类 - int blacktime=0; - int whitetime=0;//设置双方剩余时间 - String blackmessage= "无限制"; - String whitemessage="无限制"; - //--------------------------------------------------------------------------------------------------------------------- - //窗体 - @SuppressWarnings("removal") -public void myJFrame() { - - this.setTitle("五子棋"); //标题 - this.setSize(800, 600); //窗口大小 - this.setResizable(false); //窗口是否可以改变大小=否 - this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);//窗口关闭方式为关闭窗口同时结束程序 - - int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度 - int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度 -// System.out.println("宽度:"+width);//测试 -// System.out.println("高度:"+height);//测试 - - this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗口默认位置以屏幕居中 - - this.addMouseListener(this); - this.setVisible(true); //窗口是否显示=是 - t.start(); - t.suspend(); - - } - //--------------------------------------------------------------------------------------------------------------------- - //覆写paint方法,绘制界面 - public void paint(Graphics g) { - //双缓冲技术防止屏幕闪烁 - BufferedImage bi = new BufferedImage(800, 550, BufferedImage.TYPE_INT_ARGB); - Graphics g2 = bi.createGraphics(); - g2.setColor(Color.BLACK);//设置画笔颜色 - g2.setFont(new Font("宋体", 10, 50));//设置字体 - g2.drawString("五子棋", 565, 100);//绘制字符 - - //棋盘 - g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色 - g2.fillRect(qx, qy, qw, qh);//绘制棋盘背景矩形 - //开始按钮 - g2.setColor(Color.WHITE);//设置画笔颜色 - g2.fillRect(bx, by, bw, bh);//绘制开始按钮 - g2.setFont(new Font("黑体", 10, 30));//设置字体 - g2.setColor(Color.black);//设置画笔颜色 - g2.drawString("开始", 615, 185);//绘制字符 - - //悔棋按钮 - g2.setColor(Color.LIGHT_GRAY);//设置画笔颜色 - g2.fillRect(bx, by + 60, bw, bh);//绘制悔棋按钮 - g2.setFont(new Font("黑体", 10, 30));//设置字体 - g2.setColor(Color.WHITE);//设置画笔颜色 - g2.drawString("悔棋", 615, 245);//绘制字符 - - //认输按钮 - g2.setColor(Color.GRAY);//设置画笔颜色 - g2.fillRect(bx, by + 120, bw, bh);//绘制认输按钮 - g2.setFont(new Font("黑体", 10, 30));//设置字体 - g2.setColor(Color.WHITE);//设置画笔颜色 - g2.drawString("认输", 615, 305);//绘制字符 - - //游戏信息栏 + //--------------------------------------------------------------------------------------------------------------------- + //覆写paint方法,绘制界面 + public void paint(Graphics g) { + + //双缓冲技术防止屏幕闪烁 + BufferedImage bi = new BufferedImage(800, 550, BufferedImage.TYPE_INT_ARGB); + Graphics g2 = bi.createGraphics(); + g2.setColor(Color.BLACK);//设置画笔颜色 + g2.setFont(new Font("宋体", 10, 50));//设置字体 + g2.drawString("五子棋", 565, 100);//绘制字符 + + //棋盘 g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色 - g2.fillRect(550, 350, 200, 70);//绘制游戏状态区域 - g2.setColor(Color.black);//设置画笔颜色 - g2.setFont(new Font("黑体", 10, 20));//设置字体 - g2.drawString("游戏信息", 610, 380);//绘制字符 - g2.drawString(go, 610, 410);//绘制字符,加载初始信息提示 - - //游戏设置栏 - g2.setColor(Color.orange); - g2.drawRect(600, 430, bw-50, bh-20); - g2.setFont(new Font("宋体", 10,20)); - g2.setColor(Color.pink); - g2.drawString("时间设置",610 , 450); - - g2.setColor(Color.BLACK); - g2.drawString("黑方时间", 550, 480); - g2.setColor(Color.WHITE); - g2.drawString("剩余时间:", 530, 510); - g2.drawString(blackmessage,550 , 530); - g2.setColor(Color.BLACK); - g2.drawString("白方时间", 680, 480); - g2.setColor(Color.WHITE); - g2.drawString("剩余时间:", 660, 510); - g2.drawString(whitemessage, 680, 530); - - - - - g2.setColor(Color.BLACK);//设置画笔颜色 - - //绘制棋盘格线 - for (int x = 0; x <= qw; x += 35) { - g2.drawLine(qx, x + qy, qw + qx, x + qy);//绘制一条横线 - g2.drawLine(x + qx, qy, x + qx, qh + qy);//绘制一条竖线 - } - - //绘制标注点 - for (int i = 3; i <= 11; i += 4) { - for (int y = 3; y <= 11; y += 4) { - g2.fillOval(35 * i + qx - 3, 35 * y + qy - 3, 6, 6);//绘制实心圆 - } - } - - //绘制棋子 - for (int i = 0; i < 15; i++) { - for (int j = 0; j < 15; j++) { - if (SaveGame[i][j] == 1)//黑子 - { - int sx = i * 35 + qx;//坐标 - int sy = j * 35 + qy; - g2.setColor(Color.BLACK); - g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 - } - if (SaveGame[i][j] == 2)//白子 - { - int sx = i * 35 + qx; - int sy = j * 35 + qy; - g2.setColor(Color.WHITE); - g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 - g2.setColor(Color.BLACK); - g2.drawOval(sx - 13, sy - 13, 26, 26);//绘制空心圆 - } - } - } - g.drawImage(bi, 0, 0, this); - } - - private boolean WinLose() { - boolean flag = false;//输赢 - int count = 1;//相连数 - int color = SaveGame[x][y];//记录棋子颜色 - - //判断横向棋子是否相连 - int i = 1;//迭代数 - while (color == SaveGame[x + i][y])//y值相同,使用循环来判断,向右判断 - { - count++; - i++; - } - i = 1;//迭代数 - while (color == SaveGame[x - i][y])//向左判断 - { - count++; - i++; - } - if (count >= 5) { - flag = true; + g2.fillRect(qx, qy, qw, qh);//绘制棋盘背景矩形 + //开始按钮 + g2.setColor(Color.WHITE);//设置画笔颜色 + g2.fillRect(bx, by, bw, bh);//绘制开始按钮 + g2.setFont(new Font("黑体", 10, 30));//设置字体 + g2.setColor(Color.black);//设置画笔颜色 + g2.drawString("开始", 615, 185);//绘制字符 + + //悔棋按钮 + g2.setColor(Color.LIGHT_GRAY);//设置画笔颜色 + g2.fillRect(bx, by + 60, bw, bh);//绘制悔棋按钮 + g2.setFont(new Font("黑体", 10, 30));//设置字体 + g2.setColor(Color.WHITE);//设置画笔颜色 + g2.drawString("悔棋", 615, 245);//绘制字符 + + //认输按钮 + g2.setColor(Color.GRAY);//设置画笔颜色 + g2.fillRect(bx, by + 120, bw, bh);//绘制认输按钮 + g2.setFont(new Font("黑体", 10, 30));//设置字体 + g2.setColor(Color.WHITE);//设置画笔颜色 + g2.drawString("认输", 615, 305);//绘制字符 + + //游戏信息栏 + g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色 + g2.fillRect(550, 350, 200, 70);//绘制游戏状态区域 + g2.setColor(Color.black);//设置画笔颜色 + g2.setFont(new Font("黑体", 10, 20));//设置字体 + g2.drawString("游戏信息", 610, 380);//绘制字符 + g2.drawString(go, 610, 410);//绘制字符,加载初始信息提示 + + //游戏设置栏 + g2.setColor(Color.orange); + g2.drawRect(600, 430, bw-50, bh-20); + g2.setFont(new Font("宋体", 10,20)); + g2.setColor(Color.pink); + g2.drawString("时间设置",610 , 450); + + g2.setColor(Color.BLACK); + g2.drawString("黑方时间", 550, 480); + g2.setColor(Color.WHITE); + g2.drawString("剩余时间:", 530, 510); + g2.drawString(blackmessage,550 , 530); + g2.setColor(Color.BLACK); + g2.drawString("白方时间", 680, 480); + g2.setColor(Color.WHITE); + g2.drawString("剩余时间:", 660, 510); + g2.drawString(whitemessage, 680, 530); + g2.setColor(Color.BLACK);//设置画笔颜色 + + //绘制棋盘格线 + for (int x = 0; x <= qw; x += 35) { + g2.drawLine(qx, x + qy, qw + qx, x + qy);//绘制一条横线 + g2.drawLine(x + qx, qy, x + qx, qh + qy);//绘制一条竖线 } - //判断纵向棋子是否相连 - count = 1; - i = 1;//迭代数 - while (color == SaveGame[x][y + i]) { - count++; - i++; - } - i = 1;//迭代数 - while (color == SaveGame[x][y - i]) { - count++; - i++; - } - if (count >= 5) { - flag = true; + //绘制标注点 + for (int i = 3; i <= 11; i += 4) { + for (int y = 3; y <= 11; y += 4) { + g2.fillOval(35 * i + qx - 3, 35 * y + qy - 3, 6, 6);//绘制实心圆 + } } - //判断斜向棋子是否相连(左上右下) - count = 1; - i = 1;//迭代数 - while (color == SaveGame[x - i][y - i]) { - count++; - i++; - } - i = 1;//迭代数 - while (color == SaveGame[x + i][y + i]) { - count++; - i++; + //绘制棋子 + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + if (SaveGame[i][j] == 1)//黑子 + { + int sx = i * 35 + qx;//坐标 + int sy = j * 35 + qy; + g2.setColor(Color.BLACK); + g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 + } + if (SaveGame[i][j] == 2)//白子 + { + int sx = i * 35 + qx; + int sy = j * 35 + qy; + g2.setColor(Color.WHITE); + g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 + g2.setColor(Color.BLACK); + g2.drawOval(sx - 13, sy - 13, 26, 26);//绘制空心圆 + } + } } - if (count >= 5) { - flag = true; + g.drawImage(bi, 0, 0, this); } - //判断斜向棋子是否相连(左下右上) - count = 1; - i = 1;//迭代数 - while (color == SaveGame[x + i][y - i]) { - count++; - i++; - } - i = 1;//迭代数 - while (color == SaveGame[x - i][y + i]) { - count++; - i++; - } - if (count >= 5) { - flag = true; - } + private boolean WinLose() { + boolean flag = false;//输赢 + int count = 1;//相连数 + int color = SaveGame[x][y];//记录棋子颜色 + + //判断横向棋子是否相连 + int i = 1;//迭代数 + while (color == SaveGame[x + i][y])//y值相同,使用循环来判断,向右判断 + { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x - i][y])//向左判断 + { + count++; + i++; + } + if (count >= 5) { + flag = true; + } + + //判断纵向棋子是否相连 + count = 1; + i = 1;//迭代数 + while (color == SaveGame[x][y + i]) { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x][y - i]) { + count++; + i++; + } + if (count >= 5) { + flag = true; + } + + //判断斜向棋子是否相连(左上右下) + count = 1; + i = 1;//迭代数 + while (color == SaveGame[x - i][y - i]) { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x + i][y + i]) { + count++; + i++; + } + if (count >= 5) { + flag = true; + } + + //判断斜向棋子是否相连(左下右上) + count = 1; + i = 1;//迭代数 + while (color == SaveGame[x + i][y - i]) { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x - i][y + i]) { + count++; + i++; + } + if (count >= 5) { + flag = true; + } + + return flag; + } + //--------------------------------------------------------------------------------------------------------------------- + @Override//鼠标点击 + public void mouseClicked(MouseEvent e) { - return flag; } + @Override//鼠标按下 + public void mousePressed(MouseEvent e) { + //获取鼠标点击位置 + x = e.getX(); + y = e.getY(); + + //判断是否已开始游戏 + if (canplay == true)//canplay如果为false则不执行下棋的操作,结束游戏 + { + //判断点击是否为棋盘内 + if (x > qx && x < qx + qw && y > qy && y < qy + qh) { + //计算点击位置最近的点 + if ((x - qx) % 35 > 17) { + x = (x - qx) / 35 + 1; + } else { + x = (x - qx) / 35; + } + if ((y - qy) % 35 > 17) { + y = (y - qy) / 35 + 1; + } else { + y = (y - qy) / 35; + } + //判断当前位置有没有棋子 + if (SaveGame[x][y] == 0) { + SaveGame[x][y] = qc; + qn = 0; + } else { + qn = 1; + JOptionPane.showMessageDialog(this, "该位置已经有棋子,请重新落子"); + } - //--------------------------------------------------------------------------------------------------------------------- - @Override//鼠标点击 - public void mouseClicked(MouseEvent e) { - - } - - @Override//鼠标按下 - public void mousePressed(MouseEvent e) { - //获取鼠标点击位置 - x = e.getX(); - y = e.getY(); - - - //判断是否已开始游戏 - if (canplay == true)//canplay如果为false则不执行下棋的操作,结束游戏 - { - //判断点击是否为棋盘内 - if (x > qx && x < qx + qw && y > qy && y < qy + qh) { - //计算点击位置最近的点 - if ((x - qx) % 35 > 17) { - x = (x - qx) / 35 + 1; - } else { - x = (x - qx) / 35; - } - if ((y - qy) % 35 > 17) { - y = (y - qy) / 35 + 1; - } else { - y = (y - qy) / 35; - } - - //判断当前位置有没有棋子 - if (SaveGame[x][y] == 0) { - SaveGame[x][y] = qc; - qn = 0; - } else { - qn = 1; - JOptionPane.showMessageDialog(this, "该位置已经有棋子,请重新落子"); - } - - //切换棋子 - if (qn == 0) { - if (qc == 1) { - qc = 2; - go = "轮到白子"; - } else { - qc = 1; - go = "轮到黑子"; - }} - this.repaint(); - boolean wl = this.WinLose(); - if (wl) { - JOptionPane.showMessageDialog(this, "游戏结束," + (SaveGame[x][y] == 1 ? "黑方赢了" : "白方赢了"));//弹出提示对话框 - canplay = false;//结束游戏进程 - } -}} - if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by && e.getY() < by + bh) { - //判断游戏是否开始 - if (canplay == false) { - //如果游戏结束,则开始游戏 - canplay = true; - JOptionPane.showMessageDialog(this, "游戏开始"); - //初始化游戏 - for (int i = 0; i < 15; i++) { - for (int j = 0; j < 15; j++) { - SaveGame[i][j] = 0; + //切换棋子 + if (qn == 0) { + if (qc == 1) { + qc = 2; + go = "轮到白子"; + } else { + qc = 1; + go = "轮到黑子"; + }} + this.repaint(); + boolean wl = this.WinLose(); + if (wl) { + JOptionPane.showMessageDialog(this, "游戏结束," + (SaveGame[x][y] == 1 ? "黑方赢了" : "白方赢了"));//弹出提示对话框 + canplay = false;//结束游戏进程 + } +} + } + if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by && e.getY() < by + bh) { + //判断游戏是否开始 + if (canplay == false) { + //如果游戏结束,则开始游戏 + canplay = true; + JOptionPane.showMessageDialog(this, "游戏开始"); + //初始化游戏 + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + SaveGame[i][j] = 0; + } + } + + //黑子先行 + qc = 1; + go = "轮到黑子"; + + repaint();//重新执行一次paint方法 + } else { + //如果游戏进行中,则重新开始 + JOptionPane.showMessageDialog(this, "重新开始"); + //初始化游戏 + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + SaveGame[i][j] = 0; + } + } + + //黑子先行 + qc = 1; + go = "轮到黑子"; + blacktime=maxtime; + whitetime=maxtime; + + //重新执行一次paint方法 + repaint(); } } - - //黑子先行 - qc = 1; - go = "轮到黑子"; - - repaint();//重新执行一次paint方法 - - } else { - //如果游戏进行中,则重新开始 - JOptionPane.showMessageDialog(this, "重新开始"); - //初始化游戏 + + //实现悔棋按钮 + //判断是否点击悔棋按钮 + if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 60 && e.getY() < by + 60 + bh) { + //判断游戏是否开始 + if (canplay == true) { + //遍历棋盘上是否有棋子 + int z = 0; for (int i = 0; i < 15; i++) { - for (int j = 0; j < 15; j++) { - SaveGame[i][j] = 0; - } - } - - //黑子先行 - qc = 1; - go = "轮到黑子"; - - blacktime=maxtime; - whitetime=maxtime; - - //重新执行一次paint方法 - repaint(); + for (int j = 0; j < 15; j++) { + if (SaveGame[i][j] != 0) { + z++; + } + } + } + //判断是否有棋子 + if (z != 0) { + JOptionPane.showMessageDialog(this, "不允许悔棋"); } + else { + JOptionPane.showMessageDialog(this, "棋盘上已无棋子"); + } + } + else { + JOptionPane.showMessageDialog(this, "请先开始游戏"); + } + } + + //实现认输按钮 + //判断是否点击认输按钮 + if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 120 && e.getY() < by + 120 + bh) { + //判断游戏是否开始 + if (canplay == true) { + //判断是谁认输 + if (qc == 1) { + JOptionPane.showMessageDialog(this, "黑方认输,白方获胜"); + canplay = false; + } + else if (qc == 2) { + JOptionPane.showMessageDialog(this, "白方认输,黑方获胜"); + canplay = false; + } + } + else { + JOptionPane.showMessageDialog(this, "请先开始游戏"); + } + } } - } - @Override//鼠标抬起 - public void mouseReleased(MouseEvent e) { - } + @Override//鼠标抬起 + public void mouseReleased(MouseEvent e) { + + } + + @Override//鼠标进入 + public void mouseEntered(MouseEvent e) { - @Override//鼠标进入 - public void mouseEntered(MouseEvent e) { + } - } + @Override//鼠标离开 + public void mouseExited(MouseEvent e) { - @Override//鼠标离开 - public void mouseExited(MouseEvent e) { + } - } -@Override -public void run() { + @Override + public void run() { -} -} \ No newline at end of file + } + } + -- Gitee From 55e377de752c8748be5372b35dc86318913472ba Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Wed, 1 Jun 2022 22:46:17 +0800 Subject: [PATCH 19/21] =?UTF-8?q?=E5=80=92=E8=AE=A1=E6=97=B6=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=BE=88=E7=AE=80=E5=8D=95=EF=BC=8C=E4=BD=86=E6=98=AF?= =?UTF-8?q?=E4=B9=9F=E6=98=AF=E7=90=A2=E7=A3=A8=E4=BA=86=E6=8C=BA=E4=B9=85?= =?UTF-8?q?=E7=9A=84=EF=BC=8C=E5=8F=AF=E6=98=AF=E6=85=A2=E6=85=A2=E5=9C=B0?= =?UTF-8?q?=E5=8F=91=E7=8E=B0=E4=BB=A3=E7=A0=81=E7=9A=84=E4=B9=90=E8=B6=A3?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=E6=9A=91=E5=81=87=E6=9C=89=E7=A9=BA=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=AE=BE=E8=AE=A1=E4=B8=80=E7=82=B9=E5=B0=8F=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Wuziqi.java | 39 +++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/java2022spring/Wuziqi.java b/src/java2022spring/Wuziqi.java index d738c06..ebc9eb6 100644 --- a/src/java2022spring/Wuziqi.java +++ b/src/java2022spring/Wuziqi.java @@ -360,8 +360,45 @@ package java2022spring; JOptionPane.showMessageDialog(this, "请先开始游戏"); } } + + //实现设置时间按钮 + //判断是否点击设置时间按钮 + if(e.getX()>=600&&e.getX()<=550+bw&&e.getY()>=430&&e.getY()<=410+bh) { + String input=JOptionPane.showInputDialog("请输入游戏的最长时间(分钟)"); + try { + maxtime=Integer.parseInt(input)*60; + if(maxtime<0||maxtime==0) { + JOptionPane.showMessageDialog(this, "请输入大于零的时间"); + } + + if(maxtime>0) { + int result=JOptionPane.showConfirmDialog(this, "设置完成,是否重新开始游戏"); + if(result==0) { + //遍历并初始化数组 + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + SaveGame[i][j] = 0; + } + } + + //黑子先行 + qc = 1; + go = "轮到黑子"; + blacktime=maxtime; + whitetime=maxtime; + + t.resume(); + repaint(); + } + } + } + catch (NumberFormatException e1) { + // TODO 自动生成的 catch 块 + JOptionPane.showMessageDialog(this, "请输入整数数字"); + }//分钟转换为秒 } - + } + @Override//鼠标抬起 public void mouseReleased(MouseEvent e) { -- Gitee From 0c822496119efa93e872c970da27cf1577f4fa7a Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Fri, 3 Jun 2022 21:53:51 +0800 Subject: [PATCH 20/21] =?UTF-8?q?=E6=9C=AC=E6=9D=A5=E6=83=B3=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=9C=80=E7=BB=88=E6=8F=90=E4=BA=A4=E7=9A=84=EF=BC=8C?= =?UTF-8?q?=E5=8F=88=E5=AD=A6=E4=BA=86=E6=B7=BB=E5=8A=A0=E8=83=8C=E6=99=AF?= =?UTF-8?q?=E9=9F=B3=E4=B9=90=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8Cwav?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E6=8C=BA=E5=A5=BD=E7=94=A8=E7=9A=84=EF=BC=8C?= =?UTF-8?q?=E6=80=BB=E7=9A=84=E6=9D=A5=E8=AF=B4=EF=BC=8C=E5=86=99=E8=BF=99?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81=E8=BF=98=E6=98=AF=E6=88=90?= =?UTF-8?q?=E9=95=BF=E4=BA=86=E4=B8=8D=E5=B0=91=EF=BC=8C=E4=B8=8D=E6=96=AD?= =?UTF-8?q?=E5=9C=B0=E6=9F=A5=E8=B5=84=E6=96=99=E3=80=81=E9=97=AE=E5=BE=80?= =?UTF-8?q?=E5=B1=8A=E5=B8=88=E5=85=84=E5=B8=88=E5=A7=90=EF=BC=8C=E6=80=BB?= =?UTF-8?q?=E7=AE=97=E6=98=AF=E5=AE=8C=E6=95=B4=E5=86=99=E5=87=BA=E6=9D=A5?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Wuziqi.java | 769 ++++++++++++++++++--------------- 1 file changed, 414 insertions(+), 355 deletions(-) diff --git a/src/java2022spring/Wuziqi.java b/src/java2022spring/Wuziqi.java index ebc9eb6..21bd2a3 100644 --- a/src/java2022spring/Wuziqi.java +++ b/src/java2022spring/Wuziqi.java @@ -1,366 +1,398 @@ package java2022spring; - import javax.swing.*; - import java.awt.*; - import java.awt.event.MouseEvent; - import java.awt.event.MouseListener; - import java.awt.image.BufferedImage; - - public class Wuziqi { - public static void main(String[] args) { - MyJFrame mj=new MyJFrame(); - mj.myJFrame(); - } +import javax.imageio.ImageIO; +import javax.sound.sampled.AudioFormat; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.SourceDataLine; +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +public class Wuziqi { + public static void main(String[] args) throws Exception, Exception{ + MyJFrame mj=new MyJFrame(); + mj.myJFrame(); + + File file = new File("D:\\系统默认\\桌面\\萧玄-牵丝戏.wav"); + AudioInputStream am; + am = AudioSystem.getAudioInputStream(file); + AudioFormat af = am.getFormat(); + SourceDataLine sd ; + sd = AudioSystem.getSourceDataLine(af); + sd.open(); + sd.start(); + int sumByteRead = 0; //读取的总字节数 + byte [] b = new byte[320];//设置字节数组大小 + + while (sumByteRead != -1) {//-1代表没有 不等于-1时就无限读取 + sumByteRead = am.read(b, 0, b.length);//读取哪个数组 + if(sumByteRead >= 0 ) {//读取了之后将数据写入混频器,开始播放 + sd.write(b, 0, b.length); + } } - class MyJFrame extends JFrame implements MouseListener,Runnable { - /** - * - */ - private static final long serialVersionUID = 7547087204849324043L; - int qx = 20, qy = 40, qw = 490, qh = 490;//棋盘位置、宽高 - int bw = 150, bh = 50, bx = 570, by = 150;//按钮宽高、位置 - int x = 0, y = 0;//保存棋子坐标 - int[][] SaveGame = new int[15][15];//保存每个棋子 - int qc = 1;//记录白棋=2,黑棋=1 - int qn = 0;//判断棋子是否重复 - boolean canplay = true;//判断游戏是否开始和结束 - String go = "黑子先行";//游戏信息 - int maxtime=0;//设置最大时间 - Thread t=new Thread(this);//做倒计时的线程类 - int blacktime=0; - int whitetime=0;//设置双方剩余时间 - String blackmessage= "无限制"; - String whitemessage="无限制"; - //--------------------------------------------------------------------------------------------------------------------- - //窗体 - @SuppressWarnings("removal") - public void myJFrame() { - this.setTitle("五子棋"); //标题 - this.setSize(800, 600); //窗口大小 - this.setResizable(false); //窗口是否可以改变大小=否 - this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);//窗口关闭方式为关闭窗口同时结束程序 - - int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度 - int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度 - // System.out.println("宽度:"+width);//测试 - // System.out.println("高度:"+height);//测试 - - this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗口默认位置以屏幕居中 - this.addMouseListener(this); - this.setVisible(true); //窗口是否显示=是 - t.start(); - t.suspend(); + //关闭 + sd.drain(); + sd.close(); } - //--------------------------------------------------------------------------------------------------------------------- - //覆写paint方法,绘制界面 - public void paint(Graphics g) { - - //双缓冲技术防止屏幕闪烁 - BufferedImage bi = new BufferedImage(800, 550, BufferedImage.TYPE_INT_ARGB); - Graphics g2 = bi.createGraphics(); - g2.setColor(Color.BLACK);//设置画笔颜色 - g2.setFont(new Font("宋体", 10, 50));//设置字体 - g2.drawString("五子棋", 565, 100);//绘制字符 + } - //棋盘 + class MyJFrame extends JFrame implements MouseListener,Runnable { + /** + * + */ + private static final long serialVersionUID = 7547087204849324043L; +int qx = 20, qy = 40, qw = 490, qh = 490;//棋盘位置、宽高 + int bw = 150, bh = 50, bx = 570, by = 150;//按钮宽高、位置 + int x = 0, y = 0;//保存棋子坐标 + int[][] SaveGame = new int[15][15];//保存每个棋子 + int qc = 1;//记录白棋=2,黑棋=1 + int qn = 0;//判断棋子是否重复 + boolean canplay = true;//判断游戏是否开始和结束 + String go = "黑子先行";//游戏信息 + int maxtime=0;//设置最大时间 + Thread t=new Thread(this);//做倒计时的线程类 + int blacktime=0; + int whitetime=0;//设置双方剩余时间 + String blackmessage= "无限制"; + String whitemessage="无限制"; + //--------------------------------------------------------------------------------------------------------------------- + //窗体 + @SuppressWarnings("removal") +public void myJFrame() { + this.setTitle("五子棋"); //标题 + this.setSize(800, 600); //窗口大小 + this.setResizable(false); //窗口是否可以改变大小=否 + this.setDefaultCloseOperation(MyJFrame.EXIT_ON_CLOSE);//窗口关闭方式为关闭窗口同时结束程序 + + int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度 + int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度 +// System.out.println("宽度:"+width);//测试 +// System.out.println("高度:"+height);//测试 + + this.setLocation((width - 800) / 2, (height - 600) / 2); //设置窗口默认位置以屏幕居中 + this.addMouseListener(this); + this.setVisible(true); //窗口是否显示=是 + t.start(); + t.suspend(); + } + //--------------------------------------------------------------------------------------------------------------------- + //覆写paint方法,绘制界面 + public void paint(Graphics g) { + //双缓冲技术防止屏幕闪烁 + BufferedImage bi = new BufferedImage(800, 550, BufferedImage.TYPE_INT_ARGB); + Graphics g2 = bi.createGraphics(); + int width = Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度 + int height = Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度 + g2.clearRect((width - 500) / 2, (height +105) / 2, width-1280,height+500); + g2.setColor(Color.BLACK);//设置画笔颜色 + g2.setFont(new Font("宋体", 10, 50));//设置字体 + g2.drawString("五子棋", 565, 100);//绘制字符 + //棋盘 + g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色 + g2.fillRect(qx, qy, qw, qh);//绘制棋盘背景矩形 + //开始按钮 + g2.setColor(Color.WHITE);//设置画笔颜色 + g2.fillRect(bx, by, bw, bh);//绘制开始按钮 + g2.setFont(new Font("黑体", 10, 30));//设置字体 + g2.setColor(Color.black);//设置画笔颜色 + g2.drawString("开始", 615, 185);//绘制字符 + + //悔棋按钮 + g2.setColor(Color.LIGHT_GRAY);//设置画笔颜色 + g2.fillRect(bx, by + 60, bw, bh);//绘制悔棋按钮 + g2.setFont(new Font("黑体", 10, 30));//设置字体 + g2.setColor(Color.WHITE);//设置画笔颜色 + g2.drawString("悔棋", 615, 245);//绘制字符 + + //认输按钮 + g2.setColor(Color.GRAY);//设置画笔颜色 + g2.fillRect(bx, by + 120, bw, bh);//绘制认输按钮 + g2.setFont(new Font("黑体", 10, 30));//设置字体 + g2.setColor(Color.WHITE);//设置画笔颜色 + g2.drawString("认输", 615, 305);//绘制字符 + + //游戏信息栏 g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色 - g2.fillRect(qx, qy, qw, qh);//绘制棋盘背景矩形 - //开始按钮 - g2.setColor(Color.WHITE);//设置画笔颜色 - g2.fillRect(bx, by, bw, bh);//绘制开始按钮 - g2.setFont(new Font("黑体", 10, 30));//设置字体 - g2.setColor(Color.black);//设置画笔颜色 - g2.drawString("开始", 615, 185);//绘制字符 - - //悔棋按钮 - g2.setColor(Color.LIGHT_GRAY);//设置画笔颜色 - g2.fillRect(bx, by + 60, bw, bh);//绘制悔棋按钮 - g2.setFont(new Font("黑体", 10, 30));//设置字体 - g2.setColor(Color.WHITE);//设置画笔颜色 - g2.drawString("悔棋", 615, 245);//绘制字符 - - //认输按钮 - g2.setColor(Color.GRAY);//设置画笔颜色 - g2.fillRect(bx, by + 120, bw, bh);//绘制认输按钮 - g2.setFont(new Font("黑体", 10, 30));//设置字体 - g2.setColor(Color.WHITE);//设置画笔颜色 - g2.drawString("认输", 615, 305);//绘制字符 - - //游戏信息栏 - g2.setColor(Color.getHSBColor(30, (float) 0.10, (float) 0.90));//设置画笔颜色 - g2.fillRect(550, 350, 200, 70);//绘制游戏状态区域 - g2.setColor(Color.black);//设置画笔颜色 - g2.setFont(new Font("黑体", 10, 20));//设置字体 - g2.drawString("游戏信息", 610, 380);//绘制字符 - g2.drawString(go, 610, 410);//绘制字符,加载初始信息提示 - - //游戏设置栏 - g2.setColor(Color.orange); - g2.drawRect(600, 430, bw-50, bh-20); - g2.setFont(new Font("宋体", 10,20)); - g2.setColor(Color.pink); - g2.drawString("时间设置",610 , 450); - - g2.setColor(Color.BLACK); - g2.drawString("黑方时间", 550, 480); - g2.setColor(Color.WHITE); - g2.drawString("剩余时间:", 530, 510); - g2.drawString(blackmessage,550 , 530); - g2.setColor(Color.BLACK); - g2.drawString("白方时间", 680, 480); - g2.setColor(Color.WHITE); - g2.drawString("剩余时间:", 660, 510); - g2.drawString(whitemessage, 680, 530); - g2.setColor(Color.BLACK);//设置画笔颜色 - - //绘制棋盘格线 - for (int x = 0; x <= qw; x += 35) { - g2.drawLine(qx, x + qy, qw + qx, x + qy);//绘制一条横线 - g2.drawLine(x + qx, qy, x + qx, qh + qy);//绘制一条竖线 + g2.fillRect(550, 350, 200, 70);//绘制游戏状态区域 + g2.setColor(Color.black);//设置画笔颜色 + g2.setFont(new Font("黑体", 10, 20));//设置字体 + g2.drawString("游戏信息", 610, 380);//绘制字符 + g2.drawString(go, 610, 410);//绘制字符,加载初始信息提示 + + //游戏设置栏 + g2.setColor(Color.orange); + g2.drawRect(600, 430, bw-50, bh-20); + g2.setFont(new Font("宋体", 10,20)); + g2.setColor(Color.pink); + g2.drawString("时间设置",610 , 450); + + g2.setColor(Color.BLACK); + g2.drawString("黑方时间", 550, 480); + g2.setColor(Color.WHITE); + g2.drawString("剩余时间:", 530, 510); + g2.drawString(blackmessage,550 , 530); + g2.setColor(Color.BLACK); + g2.drawString("白方时间", 680, 480); + g2.setColor(Color.WHITE); + g2.drawString("剩余时间:", 660, 510); + g2.drawString(whitemessage, 680, 530); + g2.setColor(Color.BLACK);//设置画笔颜色 + + //绘制棋盘格线 + for (int x = 0; x <= qw; x += 35) { + g2.drawLine(qx, x + qy, qw + qx, x + qy);//绘制一条横线 + g2.drawLine(x + qx, qy, x + qx, qh + qy);//绘制一条竖线 + } + + //绘制标注点 + for (int i = 3; i <= 11; i += 4) { + for (int y = 3; y <= 11; y += 4) { + g2.fillOval(35 * i + qx - 3, 35 * y + qy - 3, 6, 6);//绘制实心圆 + } + } + //绘制棋子 + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + if (SaveGame[i][j] == 1)//黑子 + { + int sx = i * 35 + qx;//坐标 + int sy = j * 35 + qy; + g2.setColor(Color.BLACK); + g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 + } + if (SaveGame[i][j] == 2)//白子 + { + int sx = i * 35 + qx; + int sy = j * 35 + qy; + g2.setColor(Color.WHITE); + g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 + g2.setColor(Color.BLACK); + g2.drawOval(sx - 13, sy - 13, 26, 26);//绘制空心圆 + } + } + } + g.drawImage(bi, 0, 0, this); + } + private boolean WinLose() { + boolean flag = false;//输赢 + int count = 1;//相连数 + int color = SaveGame[x][y];//记录棋子颜色 + + //判断横向棋子是否相连 + int i = 1;//迭代数 + while (color == SaveGame[x + i][y])//y值相同,使用循环来判断,向右判断 + { + count++; + i++; } - - //绘制标注点 - for (int i = 3; i <= 11; i += 4) { - for (int y = 3; y <= 11; y += 4) { - g2.fillOval(35 * i + qx - 3, 35 * y + qy - 3, 6, 6);//绘制实心圆 - } + i = 1;//迭代数 + while (color == SaveGame[x - i][y])//向左判断 + { + count++; + i++; + } + if (count >= 5) { + flag = true; } - //绘制棋子 - for (int i = 0; i < 15; i++) { - for (int j = 0; j < 15; j++) { - if (SaveGame[i][j] == 1)//黑子 - { - int sx = i * 35 + qx;//坐标 - int sy = j * 35 + qy; - g2.setColor(Color.BLACK); - g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 - } - if (SaveGame[i][j] == 2)//白子 - { - int sx = i * 35 + qx; - int sy = j * 35 + qy; - g2.setColor(Color.WHITE); - g2.fillOval(sx - 13, sy - 13, 26, 26);//绘制实心圆 - g2.setColor(Color.BLACK); - g2.drawOval(sx - 13, sy - 13, 26, 26);//绘制空心圆 - } - } + //判断纵向棋子是否相连 + count = 1; + i = 1;//迭代数 + while (color == SaveGame[x][y + i]) { + count++; + i++; } - g.drawImage(bi, 0, 0, this); + i = 1;//迭代数 + while (color == SaveGame[x][y - i]) { + count++; + i++; + } + if (count >= 5) { + flag = true; } - private boolean WinLose() { - boolean flag = false;//输赢 - int count = 1;//相连数 - int color = SaveGame[x][y];//记录棋子颜色 - - //判断横向棋子是否相连 - int i = 1;//迭代数 - while (color == SaveGame[x + i][y])//y值相同,使用循环来判断,向右判断 - { - count++; - i++; - } - i = 1;//迭代数 - while (color == SaveGame[x - i][y])//向左判断 - { - count++; - i++; - } - if (count >= 5) { - flag = true; - } - - //判断纵向棋子是否相连 - count = 1; - i = 1;//迭代数 - while (color == SaveGame[x][y + i]) { - count++; - i++; - } - i = 1;//迭代数 - while (color == SaveGame[x][y - i]) { - count++; - i++; - } - if (count >= 5) { - flag = true; - } - - //判断斜向棋子是否相连(左上右下) - count = 1; - i = 1;//迭代数 - while (color == SaveGame[x - i][y - i]) { - count++; - i++; - } - i = 1;//迭代数 - while (color == SaveGame[x + i][y + i]) { - count++; - i++; - } - if (count >= 5) { - flag = true; - } - - //判断斜向棋子是否相连(左下右上) - count = 1; - i = 1;//迭代数 - while (color == SaveGame[x + i][y - i]) { - count++; - i++; - } - i = 1;//迭代数 - while (color == SaveGame[x - i][y + i]) { - count++; - i++; - } - if (count >= 5) { - flag = true; - } - - return flag; - } - //--------------------------------------------------------------------------------------------------------------------- - @Override//鼠标点击 - public void mouseClicked(MouseEvent e) { + //判断斜向棋子是否相连(左上右下) + count = 1; + i = 1;//迭代数 + while (color == SaveGame[x - i][y - i]) { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x + i][y + i]) { + count++; + i++; + } + if (count >= 5) { + flag = true; + } + //判断斜向棋子是否相连(左下右上) + count = 1; + i = 1;//迭代数 + while (color == SaveGame[x + i][y - i]) { + count++; + i++; + } + i = 1;//迭代数 + while (color == SaveGame[x - i][y + i]) { + count++; + i++; + } + if (count >= 5) { + flag = true; } - @Override//鼠标按下 - public void mousePressed(MouseEvent e) { - //获取鼠标点击位置 - x = e.getX(); - y = e.getY(); - //判断是否已开始游戏 - if (canplay == true)//canplay如果为false则不执行下棋的操作,结束游戏 - { - //判断点击是否为棋盘内 - if (x > qx && x < qx + qw && y > qy && y < qy + qh) { - //计算点击位置最近的点 - if ((x - qx) % 35 > 17) { - x = (x - qx) / 35 + 1; - } else { - x = (x - qx) / 35; - } - if ((y - qy) % 35 > 17) { - y = (y - qy) / 35 + 1; - } else { - y = (y - qy) / 35; - } + return flag; + } - //判断当前位置有没有棋子 - if (SaveGame[x][y] == 0) { - SaveGame[x][y] = qc; - qn = 0; - } else { - qn = 1; - JOptionPane.showMessageDialog(this, "该位置已经有棋子,请重新落子"); - } - //切换棋子 - if (qn == 0) { - if (qc == 1) { - qc = 2; - go = "轮到白子"; - } else { - qc = 1; - go = "轮到黑子"; - }} - this.repaint(); - boolean wl = this.WinLose(); - if (wl) { - JOptionPane.showMessageDialog(this, "游戏结束," + (SaveGame[x][y] == 1 ? "黑方赢了" : "白方赢了"));//弹出提示对话框 - canplay = false;//结束游戏进程 - } -} - } - if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by && e.getY() < by + bh) { - //判断游戏是否开始 - if (canplay == false) { - //如果游戏结束,则开始游戏 - canplay = true; - JOptionPane.showMessageDialog(this, "游戏开始"); - //初始化游戏 - for (int i = 0; i < 15; i++) { - for (int j = 0; j < 15; j++) { - SaveGame[i][j] = 0; - } - } + //--------------------------------------------------------------------------------------------------------------------- + @Override//鼠标点击 + public void mouseClicked(MouseEvent e) { + } + @SuppressWarnings("removal") +@Override//鼠标按下 + public void mousePressed(MouseEvent e) { + //获取鼠标点击位置 + x = e.getX(); + y = e.getY(); + //判断是否已开始游戏 + if (canplay == true)//canplay如果为false则不执行下棋的操作,结束游戏 + { + //判断点击是否为棋盘内 + if (x > qx && x < qx + qw && y > qy && y < qy + qh) { + //计算点击位置最近的点 + if ((x - qx) % 35 > 17) { + x = (x - qx) / 35 + 1; + } else { + x = (x - qx) / 35; + } + if ((y - qy) % 35 > 17) { + y = (y - qy) / 35 + 1; + } else { + y = (y - qy) / 35; + } - //黑子先行 - qc = 1; - go = "轮到黑子"; - - repaint();//重新执行一次paint方法 - } else { - //如果游戏进行中,则重新开始 - JOptionPane.showMessageDialog(this, "重新开始"); - //初始化游戏 - for (int i = 0; i < 15; i++) { - for (int j = 0; j < 15; j++) { - SaveGame[i][j] = 0; - } - } + //判断当前位置有没有棋子 + if (SaveGame[x][y] == 0) { + SaveGame[x][y] = qc; + qn = 0; + } else { + qn = 1; + JOptionPane.showMessageDialog(this, "该位置已经有棋子,请重新落子"); + } - //黑子先行 - qc = 1; - go = "轮到黑子"; - blacktime=maxtime; - whitetime=maxtime; - - //重新执行一次paint方法 - repaint(); - } - } - - //实现悔棋按钮 - //判断是否点击悔棋按钮 - if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 60 && e.getY() < by + 60 + bh) { + //切换棋子 + if (qn == 0) { + if (qc == 1) { + qc = 2; + go = "轮到白子"; + } else { + qc = 1; + go = "轮到黑子"; + } + } + this.repaint(); + boolean wl = this.WinLose(); + if (wl) { + JOptionPane.showMessageDialog(this, "游戏结束," + (SaveGame[x][y] == 1 ? "黑方赢了" : "白方赢了"));//弹出提示对话框 + canplay = false;//结束游戏进程 + } +} + } + if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by && e.getY() < by + bh) { //判断游戏是否开始 - if (canplay == true) { - //遍历棋盘上是否有棋子 - int z = 0; + if (canplay == false) { + //如果游戏结束,则开始游戏 + canplay = true; + JOptionPane.showMessageDialog(this, "游戏开始"); + //初始化游戏 for (int i = 0; i < 15; i++) { - for (int j = 0; j < 15; j++) { - if (SaveGame[i][j] != 0) { - z++; - } - } - } - //判断是否有棋子 - if (z != 0) { - JOptionPane.showMessageDialog(this, "不允许悔棋"); - } - else { - JOptionPane.showMessageDialog(this, "棋盘上已无棋子"); - } - } - else { - JOptionPane.showMessageDialog(this, "请先开始游戏"); - } - } - - //实现认输按钮 - //判断是否点击认输按钮 - if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 120 && e.getY() < by + 120 + bh) { - //判断游戏是否开始 - if (canplay == true) { - //判断是谁认输 - if (qc == 1) { - JOptionPane.showMessageDialog(this, "黑方认输,白方获胜"); - canplay = false; - } - else if (qc == 2) { - JOptionPane.showMessageDialog(this, "白方认输,黑方获胜"); - canplay = false; - } + for (int j = 0; j < 15; j++) { + SaveGame[i][j] = 0; + } + } + + //黑子先行 + qc = 1; + go = "轮到黑子"; + + repaint();//重新执行一次paint方法 + } else { - JOptionPane.showMessageDialog(this, "请先开始游戏"); + //如果游戏进行中,则重新开始 + JOptionPane.showMessageDialog(this, "重新开始"); + //初始化游戏 + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + SaveGame[i][j] = 0; + } + } + + //黑子先行 + qc = 1; + go = "轮到黑子"; + + blacktime=maxtime; + whitetime=maxtime; + + //重新执行一次paint方法 + repaint(); } } + //实现悔棋按钮 + //判断是否点击悔棋按钮 + if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 60 && e.getY() < by + 60 + bh) { + //判断游戏是否开始 + if (canplay == true) { + //遍历棋盘上是否有棋子 + int z = 0; + for (int i = 0; i < 15; i++) { + for (int j = 0; j < 15; j++) { + if (SaveGame[i][j] != 0) { + z++; + } + } + } + //判断是否有棋子 + if (z != 0) { + JOptionPane.showMessageDialog(this, "不允许悔棋"); + } + else { + JOptionPane.showMessageDialog(this, "棋盘上已无棋子"); + } + } + else { + JOptionPane.showMessageDialog(this, "请先开始游戏"); + } + } + //实现认输按钮 + //判断是否点击认输按钮 + if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 120 && e.getY() < by + 120 + bh) { + //判断游戏是否开始 + if (canplay == true) { + //判断是谁认输 + if (qc == 1) { + JOptionPane.showMessageDialog(this, "黑方认输,白方获胜"); + canplay = false; + } + else if (qc == 2) { + JOptionPane.showMessageDialog(this, "白方认输,黑方获胜"); + canplay = false; + } + } + else { + JOptionPane.showMessageDialog(this, "请先开始游戏"); + } + } + //实现设置时间按钮 //判断是否点击设置时间按钮 if(e.getX()>=600&&e.getX()<=550+bw&&e.getY()>=430&&e.getY()<=410+bh) { @@ -397,28 +429,55 @@ package java2022spring; JOptionPane.showMessageDialog(this, "请输入整数数字"); }//分钟转换为秒 } - } - - - @Override//鼠标抬起 - public void mouseReleased(MouseEvent e) { + + + } - } - @Override//鼠标进入 - public void mouseEntered(MouseEvent e) { + @Override//鼠标抬起 + public void mouseReleased(MouseEvent e) { - } + } - @Override//鼠标离开 - public void mouseExited(MouseEvent e) { + @Override//鼠标进入 + public void mouseEntered(MouseEvent e) { - } + } + @Override//鼠标离开 + public void mouseExited(MouseEvent e) { - @Override - public void run() { - - } + } +@Override +public void run() { + // TODO 自动生成的方法存根 + //判断是否有时间限制 + if(maxtime>0) { + while(true){//死循环设置倒计时 + if(qc==1) { + blacktime--; + if(blacktime==0) { + JOptionPane.showMessageDialog(this, "黑方超时,游戏结束"); + } + }else { + whitetime--; + if(whitetime==0) { + JOptionPane.showMessageDialog(this, "白方超时,游戏结束"); + } + } + + blackmessage = blacktime / 3600 + ":" + (blacktime / 60 - blacktime / 3600 * 60) + ":" + + (blacktime - blacktime / 60 * 60); + whitemessage = whitetime / 3600 + ":" + (whitetime / 60 - whitetime / 3600 * 60) + ":" + + (whitetime - whitetime / 60 * 60); + this.repaint(); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO 自动生成的 catch 块 + + } + } } - +} +} \ No newline at end of file -- Gitee From 54b57e83a25b0c73a197923496c9e0277359b364 Mon Sep 17 00:00:00 2001 From: Zhong <1648522715@qq.com> Date: Mon, 6 Jun 2022 21:29:06 +0800 Subject: [PATCH 21/21] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=82=E8=80=83?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81=E7=9A=84=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E4=B8=94=E6=8F=90=E4=BA=A4=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E7=9A=84=E8=83=8C=E6=99=AF=E9=9F=B3=E4=B9=90=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Wuziqi.java | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/java2022spring/Wuziqi.java b/src/java2022spring/Wuziqi.java index 21bd2a3..df9e52b 100644 --- a/src/java2022spring/Wuziqi.java +++ b/src/java2022spring/Wuziqi.java @@ -80,6 +80,8 @@ public void myJFrame() { t.suspend(); } //--------------------------------------------------------------------------------------------------------------------- + + //覆写paint方法,绘制界面 public void paint(Graphics g) { //双缓冲技术防止屏幕闪烁 @@ -143,6 +145,7 @@ public void myJFrame() { g2.setColor(Color.BLACK);//设置画笔颜色 //绘制棋盘格线 + //以下部分代码有参考,参考绘制,但不是全抄。 for (int x = 0; x <= qw; x += 35) { g2.drawLine(qx, x + qy, qw + qx, x + qy);//绘制一条横线 g2.drawLine(x + qx, qy, x + qx, qh + qy);//绘制一条竖线 @@ -177,6 +180,8 @@ public void myJFrame() { } g.drawImage(bi, 0, 0, this); } + + private boolean WinLose() { boolean flag = false;//输赢 int count = 1;//相连数 @@ -199,6 +204,7 @@ public void myJFrame() { flag = true; } + //判断纵向棋子是否相连 count = 1; i = 1;//迭代数 @@ -215,6 +221,7 @@ public void myJFrame() { flag = true; } + //判断斜向棋子是否相连(左上右下) count = 1; i = 1;//迭代数 @@ -231,6 +238,7 @@ public void myJFrame() { flag = true; } + //判断斜向棋子是否相连(左下右上) count = 1; i = 1;//迭代数 @@ -256,6 +264,8 @@ public void myJFrame() { public void mouseClicked(MouseEvent e) { } @SuppressWarnings("removal") + + @Override//鼠标按下 public void mousePressed(MouseEvent e) { //获取鼠标点击位置 @@ -347,8 +357,10 @@ public void myJFrame() { } } - //实现悔棋按钮 + + //实现悔棋按钮 //判断是否点击悔棋按钮 + //以下部分判断悔棋按钮有参考 if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 60 && e.getY() < by + 60 + bh) { //判断游戏是否开始 if (canplay == true) { @@ -373,6 +385,8 @@ public void myJFrame() { JOptionPane.showMessageDialog(this, "请先开始游戏"); } } + + //实现认输按钮 //判断是否点击认输按钮 if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 120 && e.getY() < by + 120 + bh) { @@ -393,6 +407,7 @@ public void myJFrame() { } } + //实现设置时间按钮 //判断是否点击设置时间按钮 if(e.getX()>=600&&e.getX()<=550+bw&&e.getY()>=430&&e.getY()<=410+bh) { @@ -448,6 +463,8 @@ public void myJFrame() { public void mouseExited(MouseEvent e) { } + + @Override public void run() { // TODO 自动生成的方法存根 @@ -465,12 +482,13 @@ public void run() { JOptionPane.showMessageDialog(this, "白方超时,游戏结束"); } } - + //以下部分代码有参考 blackmessage = blacktime / 3600 + ":" + (blacktime / 60 - blacktime / 3600 * 60) + ":" + (blacktime - blacktime / 60 * 60); whitemessage = whitetime / 3600 + ":" + (whitetime / 60 - whitetime / 3600 * 60) + ":" + (whitetime - whitetime / 60 * 60); this.repaint(); + try { Thread.sleep(1000); } catch (InterruptedException e) { -- Gitee