📄 triframe.java
字号:
package TriFrame;
import SunFa.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class TriFrame extends Frame{
protected int top,left;
protected int count=0; //三角形的行数
protected int count2; //用于控制显示三角形数字的行数
protected int rectlen; //方格的长度
protected int width; //方格的宽度
protected int right;
protected int bottom;
protected int interval;
protected int panelWidth; //面板的宽度
protected ControlPanel controlPanel ;
protected int[][] TriangleContent; //保存输入三角形的值
//protected int[][] resultArray;
protected int[][] child; //保存结果
protected Triangle tri = null;
protected int SUM;//保存最大和值
public TriFrame(){
setTitle("数字三角形问题B");
setBackground(new Color(120,120,200));
//setBackground(Color.white);
top = left =50;
rectlen = 50;
right=450;
bottom = 450;
panelWidth = 200;
setLayout(null);
controlPanel = new ControlPanel();
width = left+right;
controlPanel.setBounds(width,top,panelWidth,bottom+top);
setLocation(100,100);
setSize(width+panelWidth,bottom+top);
add(controlPanel,"East");
//关闭窗口
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
});
setResizable(false);
setVisible(true);
start();
}
public void paint(Graphics g){
System.out.println("调用paint");
g.setColor(Color.white);
g.fillRect(0, 0, width, width);
g.setColor(Color.black);
if(count2 !=0 ){
g.setFont(new Font("黑体",30,30));
int tempX0 = right/2+20;
int tempY0 = top+(count2/4)*interval;
int tempX;
int tempY;
for(int i=0;i<count;i++){
tempX = tempX0-i*interval;
tempY = tempY0+i*interval;
for(int j=0;j<=i;j++){
g.drawString(""+TriangleContent[i][j], tempX+j*interval, tempY);
tempX = tempX+interval;
}
}
SUM = tri.getSum();
if(SUM>0){
System.out.println("正在调用画线,显示最后结果");
g.setColor(new Color(255,0,0));
String resultText;
resultText = "\n最大和值为: "+SUM+"\n选择的情况如下:\n";
int col0 = 0;
for(int i=0;i<=count-2;i++){
tempY = tempY0+i*interval;
int col1=child[i][col0];
if(col1==col0)tempX = tempX0-interval;
else tempX = tempX0+interval;
g.drawLine(tempX0, tempY, tempX, tempY+interval);
resultText +="\n第 "+(i+2)+" 行选择第 "+(col1+1)+" 列,值为: "+TriangleContent[i][col1];
col0=col1;
tempX0 = tempX;
}
controlPanel.setResult(resultText+"\n\n\n\n\n");
}
}
}
public void start(){
if(count==0)interval =0;
else{
count2 = 2*count-1;
interval = (right-left)/count2;
child = new int[count][count];
controlPanel.setResult("");
repaint();
tri = new Triangle(count,TriangleContent,1500,this);
tri.MaxTri(getGraphics());
}
}
public void drawChild(Graphics g){
System.out.println("正在调用drawChild");
g.setColor(Color.white);
//g.fillRect(0, 0, width, width);
//画出三角形的内容
g.setColor(Color.black);
g.setFont(new Font("黑体",30,30));
int tempX0 = right/2+20;
int tempY0 = top+(count2/4)*interval;
int tempX;
int tempY;
for(int i=0;i<count;i++){
tempX = tempX0-i*interval;
tempY = tempY0+i*interval;
for(int j=0;j<=i;j++){
g.drawString(""+TriangleContent[i][j], tempX+j*interval, tempY);
tempX = tempX+interval;
}
}
System.out.println("drawChild正在调用画线");
child = tri.getChild();
g.setColor(Color.red);
for(int i=count-2;i>=0;i--){
tempY = tempY0+i*interval;
tempX = tempX0-i*interval;
for(int j=0;j<=i;j++){
if(child[i][j]!=-1){
if(child[i][j]==j)
g.drawLine(tempX+j*2*interval, tempY, tempX+j*2*interval-interval, tempY+interval);
else
g.drawLine(tempX+j*2*interval, tempY, tempX+j*2*interval+interval, tempY+interval);
}
}
}
}
/*
public class PaintThread extends Thread {
private long TIME = 300;
public void run() {
//Triangle tri = new Triangle();
tri.MaxTri(getGraphics());
}
public void pause(){
try {
Thread.sleep(TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
*/
class ControlPanel extends Panel {
protected Label lblCount = new Label(" 请输入三角形的行数 "+"( 范围 [1,8] ): ",Label.LEFT);
protected Label lblContent = new Label(" 三角形的内容:",Label.LEFT);
protected Label lblResult = new Label(" 结果: ",Label.LEFT); //显示结果
protected TextField tfCount= new TextField(5);
protected TextArea tfContent= new TextArea(6,5);
protected TextArea tfResult= new TextArea(4,10);
protected Button start = new Button("开始");
public ControlPanel(){
setLayout(new BorderLayout());
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(3,1,0,4));
p2.add(lblCount);
p2.add(tfCount);
p2.add(lblContent);
p1.add(p2,"North");
p1.add(tfContent,"Center");
p1.add(start,"South");
add(p1,BorderLayout.NORTH);
add(tfResult,BorderLayout.CENTER);
start.addActionListener(new AL(this));
setBackground(new Color(120,120,200));
}
//向结果区域中写结果
public void setResult(String msg){
tfResult.setText(msg);
}
}
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
public class AL implements ActionListener{
ControlPanel cp= null;
public AL(ControlPanel cp){
//ControlPanel cp = new ControlPanel();
this.cp = cp;
}
public void actionPerformed(ActionEvent evt){
if(evt.getSource()==cp.start){
String strCount;
strCount = cp.tfCount.getText().toString();
if(isNumeric(strCount)){
if(strCount.trim()== null || strCount.trim().equals("")){
JOptionPane.showMessageDialog(null, "请输入三角形的行数");
}else{
count=Integer.parseInt(strCount);
if(count<=0){
JOptionPane.showMessageDialog(null, "三角形的行数不能小于0");
count=0;
}
if(count>8){
JOptionPane.showMessageDialog(null, "三角形的行数不能大于8");
count=0;
}
}//else
}//if isNumeric
else {
JOptionPane.showMessageDialog(null, "三角形的行数的不是数字");
}
String strContent;
strContent = cp.tfContent.getText().toString().trim();
String[] strContentArr = strContent.split(",");
TriangleContent= new int[count][count];
if(strContentArr.length != (1+count)*count/2){
JOptionPane.showMessageDialog(null, "输入的三角形值的个数有误");
count=0;
}else{
int i=0;
for(int j=0;j<count;j++){
for(int k=0;k<=j;k++){
if(!isNumeric(strContentArr[i].trim())){
JOptionPane.showMessageDialog(null, "输入的三角形值的中有非数字: "+(i+1)+" ,其值为: "+strContentArr[i]);
k=j+1;
j=count;
count=0;
}
else{
TriangleContent[j][k]=Integer.parseInt(strContentArr[i].trim());
}
i++;
}
}
}
TriFrame.this.start();
}//if(evt.getSource()==start){
}//actionPerformed
}//AL
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -