您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > Java 根据输入三边绘制三角形
Java根据输入三边绘制三角形importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.AdjustmentEvent;importjava.awt.event.AdjustmentListener;importjava.util.Scanner;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JScrollBar;importjavax.swing.JScrollPane;importjavax.swing.JTextField;importjavax.swing.ScrollPaneConstants;publicclassDrawTriangle{JFramejf;inta,b,c;intxs=100,ys=100;//a边始点坐标intx,y;//终点坐标Stringresult=请输入三边!;Fontf=newFont(黑体,Font.ITALIC,24);Fontfb=newFont(宋体,Font.PLAIN,14);JLabeljl;JLabeljlx;JLabeljly;JTextFieldjfx;JTextFieldjfy;JButtonjb;JPaneljp1,jp2;JScrollPanejs;publicDrawTriangle(){jf=newJFrame();jf.setSize(800,600);jf.setVisible(true);jf.setTitle(画三角形);jf.setLocationRelativeTo(null);jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);jl=newJLabel(改变起点A坐标:);jlx=newJLabel(x);jly=newJLabel(y);jfx=newJTextField(100);jfy=newJTextField(100);jb=newJButton(点击开始画三角形);jp1=newJPanel();jp2=newJPanel();js=newJScrollPane();jf.setLayout(null);jp1.setPreferredSize(newDimension(600,600));//设置绘图面板初始大小js.setBounds(0,0,jf.getWidth()-15,520);//设置滚动面板的位置和大小//js.add(jp1);js.setViewportView(jp1);//将绘图面板加入滚动面板js.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);js.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);jp2.setBounds(0,520,600,40);//控制面板jl.setBounds(50,520,120,30);jlx.setBounds(170,520,30,30);jfx.setBounds(200,520,90,30);jly.setBounds(290,520,30,30);jfy.setBounds(320,520,90,30);jb.setBounds(420,520,120,40);jp2.add(jl);jp2.add(jlx);jp2.add(jfx);jp2.add(jly);jp2.add(jfy);jp2.add(jb);jf.add(js);//窗体内加入js滚动面板jf.add(jp2);//窗体内加入操作面板jb.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){jp1.setPreferredSize(newDimension(xs+maxInt(),ys+maxInt()));//重置绘图面板大Graphicsg=jp1.getGraphics();g.setFont(f);g.clearRect(0,0,jp1.getWidth(),jp1.getHeight());//以背景色填充JFrame,以便重新绘制图形if(!jfx.getText().isEmpty()&&!jfy.getText().isEmpty()){xs=Integer.parseInt(jfx.getText().toString());ys=Integer.parseInt(jfy.getText().toString());}paint(g);//将画笔g传至自定义的画画函数}});JScrollBarbar=js.getHorizontalScrollBar();//返回控制视口的水平视图位置的水平滚动条JScrollBarbar1=js.getVerticalScrollBar();//返回控制视口垂直视图位置的垂直滚动条//滚动重绘bar.addAdjustmentListener(newAdjustmentListener(){publicvoidadjustmentValueChanged(AdjustmentEvente){jp1.setPreferredSize(newDimension(xs+maxInt(),ys+maxInt()));//重置绘图面板大Graphicsg=jp1.getGraphics();//在面板上创建画笔g.setFont(f);g.clearRect(0,0,jp1.getWidth(),jp1.getHeight());//以背景色填充JFrame,以便重新绘制图形if(!jfx.getText().isEmpty()&&!jfy.getText().isEmpty()){xs=Integer.parseInt(jfx.getText().toString());ys=Integer.parseInt(jfy.getText().toString());}paint(g);//将画笔g传至自定义的画画函数}});//监听滚动条模型发生更改//滚动bar1.addAdjustmentListener(newAdjustmentListener(){publicvoidadjustmentValueChanged(AdjustmentEvente){jp1.setPreferredSize(newDimension(xs+maxInt(),ys+maxInt()));//重置绘图面板大小Graphicsg=jp1.getGraphics();g.setFont(f);g.clearRect(0,0,jp1.getWidth(),jp1.getHeight());//以背景色填充JFrame,以便重新绘制图形if(!jfx.getText().isEmpty()&&!jfy.getText().isEmpty()){xs=Integer.parseInt(jfx.getText().toString());ys=Integer.parseInt(jfy.getText().toString());}paint(g);//将画笔g传至自定义的画画函数}});//监听滚动条模型发生更改}publicintmaxInt(){returnab?(ac?a:c):(bc?b:c);}//能否找到坐标publicbooleanisTriangle(){//(xs,xs)a边始点for(inti=xs-maxInt();i=xs+maxInt();i++)for(intj=ys+1;j=ys+maxInt();j++)//jxs使b,c边位于a边下方{//Math.sqrt必须转换为整型,才能比较(采用坐标距离公式联立二元一次方程求解坐标)if(((int)Math.sqrt((i-xs)*(i-xs)+(j-ys)*(j-ys))==b)&&((int)Math.sqrt((xs+a-i)*(xs+a-i)+(j-ys)*(j-ys))==c)&&(a+b)c&&(a+c)b&&(b+c)a){x=i;y=j;//JOptionPane.showMessageDialog(jf,x=+x+y=+y);returntrue;//返回函数}}returnfalse;}publicvoidpaint(Graphicsg){Strings=a=+a+b=+b+c=+c+;g.setFont(f);g.setColor(Color.red);g.drawString(s,10,60);g.setFont(f);g.setColor(Color.red);g.drawLine(xs,ys,xs+a,ys);//固定第一边g.drawString(A,xs,ys);//画顶点g.drawString(B,xs+a,ys);g.setFont(fb);g.setColor(Color.blue);g.drawString(a,(xs+xs+a)/2,(ys+ys)/2);//在边中点画边a的标记if(isTriangle()){g.setFont(f);g.setColor(Color.red);g.drawLine(xs,ys,x,y);g.setFont(fb);g.setColor(Color.blue);g.drawString(b,(xs+x)/2,(ys+y)/2);//在边中点画边b的标记g.setFont(f);g.setColor(Color.red);g.drawLine(xs+a,ys,x,y);g.drawString(C,x,y);g.setFont(fb);g.setColor(Color.blue);g.drawString(c,(xs+a+x)/2,(ys+y)/2);//在边中点画边c的标记}else{g.setFont(f);g.setColor(Color.red);g.drawLine(xs,ys,xs,ys+b);g.drawString(C1,xs,ys+b);g.setFont(fb);g.setColor(Color.blue);g.drawString(b,(xs+xs)/2,(ys+ys+b)/2);//在边中点画边b的标记g.setFont(f);g.setColor(Color.red);g.drawLine(xs+a,ys,xs+a,ys+c);g.drawString(C2,xs+a,ys+c);g.setFont(fb);g.setColor(Color.blue);g.drawString(c,(xs+xs+a+a)/2,(ys+ys+c)/2);//在边中点画边c的标记}g.setFont(f);g.setColor(Color.red);g.drawString(s+result,10,60);}publicstaticvoidmain(String[]args){DrawTriangletr=newDrawTriangle();tr.jf.setVisible(false);//开始界面不可见while(true){Scannersc=newScanner(System.in);inta,b,c;System.out.println
本文标题:Java 根据输入三边绘制三角形
链接地址:https://www.777doc.com/doc-6040126 .html