package shuju;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class MainFrame extends JFrame implements ActionListener,FocusListener
{
public static int Flag=0;
JButton LineBtn,OkBtn,TreeBtn,RetryBtn;
TestPanel testPanel;
JPanel JpanelLeft,JpanelButton,JpanelRight;
ResultPanel lastPanel;
SpanningTree sp;
TextField text[];
JLabel label[];
public MainFrame()
{
Container con=getContentPane();
con.setLayout(new GridLayout(1,2));
JpanelLeft=new JPanel();
JpanelRight=new JPanel();
JpanelButton=new JPanel();
testPanel=new TestPanel();
con.add(JpanelLeft);
// JpanelScRo.add(JpanelRight);
con.add(JpanelRight);
JpanelLeft.setLayout(null);
LineBtn=new JButton("画线");
OkBtn=new JButton("添加权值");
TreeBtn=new JButton("生成树");
RetryBtn = new JButton("重置");
JpanelLeft.add(testPanel);
testPanel.setBounds(0, 0, 300, 300);
JpanelLeft.add(JpanelButton);
JpanelButton.setBounds(0, 300, 280, 50);
// JpanelScRo.setBounds(300,0, 300,300);
JpanelButton.add(LineBtn);
JpanelButton.add(OkBtn);
JpanelButton.add(TreeBtn);
testPanel.setBackground(Color.LIGHT_GRAY);
LineBtn.addActionListener(this);
OkBtn.addActionListener(this);
TreeBtn.addActionListener(this);
this.setTitle("TestFrame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(520, 685);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==LineBtn)
{
testPanel.Flag=1;
}
if(e.getSource()==OkBtn)
{
System.out.println("添加权值按钮被按下");
// JpanelScRo.requestFocusInWindow();
JpanelRight.removeAll();
label=new JLabel[testPanel.Lines.size()];
this.text=new TextField[testPanel.Lines.size()];
for(int j=1;j<=testPanel.Lines.size();j++)
{
label[j-1]=new JLabel("结点"+(testPanel.LineOral[j-1][0]+1)+"和结点"+(testPanel.LineOral[j-1][1]+1));//
text[j-1]=new TextField(9);
text[j-1].addFocusListener(this);
JpanelRight.add(label[j-1]);
JpanelRight.add(text[j-1]);
//panelGrid.add(text[i]);
this.setVisible(true);
}
// for()
//JpanelRight
}
if(e.getSource()==TreeBtn)
{
Double[] weight;
int p[],q[],textnum=0;
int p1[],q1[];
p=new int[(testPanel.ovals.size())*(testPanel.ovals.size())];
q=new int[(testPanel.ovals.size())*(testPanel.ovals.size())];
weight= new Double[testPanel.Lines.size()];
try
{
for(textnum=0;textnum<testPanel.Lines.size();textnum++)
{
weight[textnum]=Double.parseDouble(this.text[textnum].getText());
p[textnum]=testPanel.LineOral[textnum][0]+1;
q[textnum]=testPanel.LineOral[textnum][1]+1;
}
for(int i=0;i<testPanel.Lines.size();i++)
{
System.out.println(""+p[i]+" "+q[i]+" "+weight[i]);
}
//testPanel
sp = new SpanningTree();
sp.init(p,q,weight,testPanel.ovals.size());
sp.prim();
sp.print();
p1=new int[sp.edge.size()];
q1=new int[sp.edge.size()];
for(int i=0;i<sp.edge.size(); ++i){
p1[i]=sp.edge.get(i).start;
q1[i]=sp.edge.get(i).end;
}
for(int i=0;i<sp.edge.size();i++)
{
System.out.println(""+p1[i]+" "+q1[i]);
}
lastPanel=new ResultPanel();
JpanelLeft.add(lastPanel);
lastPanel.setBounds(0, 350, 300, 300);
lastPanel.setBackground(Color.red);
lastPanel.DrawResult(testPanel.ovals, p1,q1);
}
catch (NumberFormatException ee) {
text[textnum] .setText("输入错误,请重新输入。");
text[textnum] .setBackground(Color.red);
}
}
}
public void focusGained(FocusEvent e) {
TextField text = (TextField)e.getSource();//获取触发事件的对象。
text.selectAll(); //当组件得到输入焦点时将输入框中的内容选中。
text.setBackground(Color.yellow); //当组件得到得到焦点时将输入框中背景色置为黄色。
}
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
TextField text = (TextField)e.getSource();
text.setBackground(Color.white); //当组件得到失去焦点时将输入框中背景色置为白色。
}
public static void main(String[] args)
{
new MainFrame();
}
}
class TestPanel extends JPanel {
public static int Flag=0;//初始状态是画圆,=1是填充颜色,=2是开始画线,=3生成权值窗口
public TestPanel() {
//MainFrame mainFrame=new MainFrame();
TestMouse action = new TestMouse(this);
this.addMouseListener(action);
this.addMouseMotionListener(action);
}
public ArrayList<Point[]> ovals = new ArrayList<Point[]>();//存储画好圆的坐标
public ArrayList<Point[]> Lines = new ArrayList<Point[]>();//存储画好线的坐标
public int LineOral[][]=new int[200][2];//此数组存放直线的两端点的圆
public Point[] goOval = null;//开始画圆的坐标
public Point[] goLine = null;//开始画线的坐标
public Point[] changeOval = null;//改变颜色的圆
public void addOval(Point[] pair) {
ovals.add(pair);
}
public void addLine(Point[] pair) {
Lines.add(pair);
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
Font font=new Font("隶书",Font.BOLD,16);
g2.setFont(font);
//System.out.println(ovals.size());
/*把以前画好的圆和线重绘*/
if(Flag==0)
{
g2.drawString("点击面板,添加结点", 20, 20);
for (int i = 0, length = ovals.size(); i < length; ++i)
{
//System.out.println("1111111");
Point[] oval = (Point[]) ovals.get(i);
g2.drawOval((int) oval[0].getX(), (int) oval[0].getY(),23, 23);
g2.drawString(""+(i+1),(int) oval[0].getX()+9, (int)oval[0].getY()+15);
// g2.setColor(Color.WHITE);
// g2.fillOval((int) oval[0].getX(), (int) oval[0].getY(),23, 23);
}
if (goOval != null)
{ //画当前的圆和线
//System.out.println("22222222");
g2.drawOval((int) goOval[0].getX(), (int) goOval[0].getY(), 23, 23);
}
}
else if(Flag==1)
{
for (int i = 0, length = ovals.size(); i < length; ++i)
{
//System.out.println("1111111");
Point[] oval = (Point[]) ovals.get(i);
g2.drawOval((int) oval[0].getX(), (int) oval[0].getY(),23, 23);
g2.drawString(""+(i+1),(int) oval[0].getX()+9, (int)oval[0].getY()+15);
}
for (int i = 0, length = Lines.size(); i < length; ++i)
{
//System.out.println("1111111");
Point[] line = (Point[]) Lines.get(i);
g2.drawLine((int) line[0].getX()+10, (int) line[0].getY()+10,(int) line[1].getX(), (int) line[1].getY());
}
if (changeOval != null)
{ //画当前的圆和线
//System.out.println("22222222");
g2.setColor(Color.BLUE);
g2.fillOval((int)changeOval[0].getX(),(int) changeOval[0].getY(), 23, 23);
//g2.drawLine((int) goLine[0].getX(), (int) goLine[0].getY(), 23, 23);
}
this.Flag=2;
//System.out.pr
- 1
- 2
前往页