public void actionPerformed(ActionEvent event)
{
// deal with "Quit" button
if (event.getSource() == quit)
{
dispose();
System.exit(0);
}
// deal with "Load" button
else if (event.getSource() == load)
{
loadFile() ;
}
// deal with "newfile" button
else if (event.getSource() == newfile)
{
//Chapter56n4.Flage=1;
dispose();
Chapter56n4.Flag=1;
// Chapter56n4
f=new Chapter56n4();
//Chapter56n4.
f.setVisible(true);
//canvas.repaint();
} // deal with ok1
else if (event.getSource() == ok1)
{
String c1 = newname.getText() ;
String c2 = month.getText() ;
String c3 = day.getText() ;
String c4=".data";
String c6=c1+"_Mon_"+c2+"_Day_" +c3+c4;
System.out.println(c1);
System.out.println(c6);
String fileName = c6 ;
Chapter56n4.f.newdatafile.setText("The newfile "+c6+" was Created"); try
{
DataOutputStream dout = new DataOutputStream(new
FileOutputStream(fileName)) ;
for (int j=0;j<10;j++)
{for(int i=0;i<10;i++) dout.writeInt(1); //Seat[j][i]) ;
} dout.close() ; DataInputStream din1 = new DataInputStream(new
FileInputStream(fileName)) ;
int Seat[][]=new int[10][10];
for (int j=0;j<10;j++)
{for(int i=0;i<10;i++)
{
Seat[j][i] = din1.readInt() ;
System.err.print(Seat[j][i]) ;
}
}
din1.close() ;
}
catch(Exception e)
{
System.err.println(e);
}
}//end ok1
// deal with "OK" button
else if (event.getSource() == OK)
{
System.out.println("OK is pressed");
Chapter56n4.Fok=1;
try
{
String c1 = seatrow.getText() ;
String c3 = seatcol.getText() ;
if ((c1!=null)&&(c3!=null))
{
int c2 = 0 ;
int c4 = 0 ;
c2 = Integer.parseInt(c1) ;
c4 = Integer.parseInt(c3) ;
Chapter56n4.Seat[c2][c4]=5;
canvas.repaint();
}
}
catch(Exception e)
{
System.err.println("ERROR:"+e);
}
try
{
String c5 = seatsrow.getText() ;
String c6 = seatscolfrom1.getText() ;
String c7 = seatscolto1.getText() ;
if ((c5!=null)&&(c6!=null)&&(c7!=null))
{
int c20 = 0 ;
int c40 = 0 ;
int c41=0; c20 = Integer.parseInt(c5) ;
c40 = Integer.parseInt(c6) ;
c41 = Integer.parseInt(c7) ;
for(int m=c40;m<=c41;m++)
Chapter56n4.Seat[c20][m]=5;
canvas.repaint();
}
}
catch(Exception e)
{
System.err.println("ERROR:"+e);
} try
{
String c8 = seatsrowfrom.getText() ;
String c9 = seatsrowto.getText() ;
String c11 = seatscolfrom.getText() ;
String c12 = seatscolto.getText() ;
if ((c8!=null)&&(c9!=null)&&(c11!=null)&&(c12!=null))
{
int c201 = 0 ;
int c202 = 0 ;
int c401 = 0;
int c402 = 0;
c201 = Integer.parseInt(c8) ;
c202 = Integer.parseInt(c9) ;
c401 = Integer.parseInt(c11) ;
c402 = Integer.parseInt(c12) ;
for(int g=c201;g<=c202;g++)
{ for(int h=c401;h<=c402;h++) Chapter56n4.Seat[g][h]=5;
};
canvas.repaint();
}
}
catch(Exception e)
{
System.err.println("ERROR:"+e);
} try
{
DataOutputStream dout = new DataOutputStream(new
FileOutputStream(
{
// deal with "Quit" button
if (event.getSource() == quit)
{
dispose();
System.exit(0);
}
// deal with "Load" button
else if (event.getSource() == load)
{
loadFile() ;
}
// deal with "newfile" button
else if (event.getSource() == newfile)
{
//Chapter56n4.Flage=1;
dispose();
Chapter56n4.Flag=1;
// Chapter56n4
f=new Chapter56n4();
//Chapter56n4.
f.setVisible(true);
//canvas.repaint();
} // deal with ok1
else if (event.getSource() == ok1)
{
String c1 = newname.getText() ;
String c2 = month.getText() ;
String c3 = day.getText() ;
String c4=".data";
String c6=c1+"_Mon_"+c2+"_Day_" +c3+c4;
System.out.println(c1);
System.out.println(c6);
String fileName = c6 ;
Chapter56n4.f.newdatafile.setText("The newfile "+c6+" was Created"); try
{
DataOutputStream dout = new DataOutputStream(new
FileOutputStream(fileName)) ;
for (int j=0;j<10;j++)
{for(int i=0;i<10;i++) dout.writeInt(1); //Seat[j][i]) ;
} dout.close() ; DataInputStream din1 = new DataInputStream(new
FileInputStream(fileName)) ;
int Seat[][]=new int[10][10];
for (int j=0;j<10;j++)
{for(int i=0;i<10;i++)
{
Seat[j][i] = din1.readInt() ;
System.err.print(Seat[j][i]) ;
}
}
din1.close() ;
}
catch(Exception e)
{
System.err.println(e);
}
}//end ok1
// deal with "OK" button
else if (event.getSource() == OK)
{
System.out.println("OK is pressed");
Chapter56n4.Fok=1;
try
{
String c1 = seatrow.getText() ;
String c3 = seatcol.getText() ;
if ((c1!=null)&&(c3!=null))
{
int c2 = 0 ;
int c4 = 0 ;
c2 = Integer.parseInt(c1) ;
c4 = Integer.parseInt(c3) ;
Chapter56n4.Seat[c2][c4]=5;
canvas.repaint();
}
}
catch(Exception e)
{
System.err.println("ERROR:"+e);
}
try
{
String c5 = seatsrow.getText() ;
String c6 = seatscolfrom1.getText() ;
String c7 = seatscolto1.getText() ;
if ((c5!=null)&&(c6!=null)&&(c7!=null))
{
int c20 = 0 ;
int c40 = 0 ;
int c41=0; c20 = Integer.parseInt(c5) ;
c40 = Integer.parseInt(c6) ;
c41 = Integer.parseInt(c7) ;
for(int m=c40;m<=c41;m++)
Chapter56n4.Seat[c20][m]=5;
canvas.repaint();
}
}
catch(Exception e)
{
System.err.println("ERROR:"+e);
} try
{
String c8 = seatsrowfrom.getText() ;
String c9 = seatsrowto.getText() ;
String c11 = seatscolfrom.getText() ;
String c12 = seatscolto.getText() ;
if ((c8!=null)&&(c9!=null)&&(c11!=null)&&(c12!=null))
{
int c201 = 0 ;
int c202 = 0 ;
int c401 = 0;
int c402 = 0;
c201 = Integer.parseInt(c8) ;
c202 = Integer.parseInt(c9) ;
c401 = Integer.parseInt(c11) ;
c402 = Integer.parseInt(c12) ;
for(int g=c201;g<=c202;g++)
{ for(int h=c401;h<=c402;h++) Chapter56n4.Seat[g][h]=5;
};
canvas.repaint();
}
}
catch(Exception e)
{
System.err.println("ERROR:"+e);
} try
{
DataOutputStream dout = new DataOutputStream(new
FileOutputStream(
{for(int i=0;i<10;i++) dout.writeInt(Chapter56n4.Seat[j][i]) ;
} dout.close() ;
} catch(IOException e)
{
System.err.println(e);
} ;
}//end of OK // deal with "Quit" button
else if (event.getSource() == Quit)
{
dispose();
System.exit(0);
}
// deal with "Quit1" button
else if (event.getSource() == quit1)
{
dispose();
System.exit(0);
} // deal with "Cancel" button
else if (event.getSource() == Cancel)
{
System.out.println("Cancel is pressed");
seatrow.setText(null);
seatcol.setText(null);
seatsrow.setText(null);
seatscolfrom1.setText(null);
seatsrowfrom.setText(null);
seatscolto1.setText(null);
seatsrowto.setText(null);
seatscolfrom.setText(null);
seatscolto.setText(null);
}
// deal with "Cancel1" button
else if (event.getSource() == Cancel1)
{
System.out.println("Cancel1 is pressed");
newname.setText(null);
month.setText(null);
day.setText(null);
newdatafile.setText(null);
}
} // end of method actionPerformed
/**
*
* loadFile
*
*/ private void loadFile()
{
FileDialog d = new FileDialog(this, "Load File", FileDialog.LOAD) ;
d.setDirectory("./") ;
d.setVisible(true) ;
name = d.getFile() ;
directory = d.getDirectory() ;
canvas.repaint() ;
} // end of method loadFile /**
*
* windowClosing
*
*/ public void windowClosing(WindowEvent event)
{
dispose();
System.exit(0);
} // end of method windowClosing public void windowOpened(WindowEvent event) {}
public void windowIconified(WindowEvent event) {}
public void windowDeiconified(WindowEvent event) {}
public void windowClosed(WindowEvent event) {}
public void windowActivated(WindowEvent event) {}
public void windowDeactivated(WindowEvent event) {}
} // end of class Chapter56n4class Canvas4 extends Canvas
{
Chapter56n4 parent ; /**
*
* constructor
*
*/ public Canvas4(Chapter56n4 f)
{
parent = f ;
} // end of constructor method /**
*
* paint
*
*/ public void paint(Graphics g)
{
Dimension d = getSize() ;
int cx = d.width / 2,
cy = d.height /2 ;
// draw a line round the edge of the canvas
g.setColor(Color.black) ;
g.drawRoundRect(2, 2, d.width - 5, d.height - 5, 20, 20) ; if (parent.name != null) //&&(Chapter56n4.FnewF==0))
{
String filename = parent.directory + parent.name ;
// set up some dimensions for the text labels
Font f1 = new Font("Helvetica", Font.BOLD, 20) ;
g.setFont(f1);
// draw surrounding line
g.setColor(Color.black) ;
g.drawRoundRect(2, 2, d.width - 5, d.height - 5, 20, 20) ; g.drawString("The "+filename+" seats booking status",
30,50); // get init data from file
//String a="wt1.doc";
if (Chapter56n4.Fok==0)
{ Chapter56n4.file1=new File(filename); try{ DataInputStream din1 = new DataInputStream(new
FileInputStream(Chapter56n4.file1)) ;
// int[][] Seat=new int[10][10];
for (int j=0;j<10;j++)
{ for (int i=0;i<10;i++)
{
int n1=din1.readInt();
Chapter56n4.Seat[j][i] =n1;
}
} din1.close() ;
}
catch(IOException e)
{
System.err.println("ERROR:"+e);
}
}//end of get data from file,when Fok=0
// draw the init status of seats
int x,y=70;
for (int k=0;k<10;k++)
{
x=70;
y=y+50;
for (int l=0;l<10;l++)
{
x=x+30;
if (Chapter56n4.Seat[k][l]==5||Chapter56n4.Seat[k][l]==8)
{
g.setColor(Color.red);
g.fillRect(x,y,26,26);
}
else
{
g.setColor(Color.black);
g.drawRect(x,y,26,26);
}
}
} //end of draw init status
}//end of draw User's pecture else //no file,no pecture to draw
{
// write the message "No File" in the centre of the canvas
Font f1 = new Font("TimesRoman", Font.PLAIN, 14) ;
FontMetrics fm1 = g.getFontMetrics(f1) ;
String s1 = "No File" ;
int w1 = fm1.stringWidth(s1) ;
g.setColor(Color.black) ;
g.setFont(f1) ;
int ctx = cx - (w1 / 2) ;
int cty = cy ;
g.drawString(s1, ctx, cty) ;
Font f2 = new Font("TimesRoman", Font.PLAIN, 30) ;
//FontMetrics fm1 = g.getFontMetrics(f1) ;
String s2 = "Welcom to use the seat book system" ;
//int w1 = fm1.stringWidth(s1) ;
g.setColor(Color.red) ;
g.setFont(f2) ;
int x1 =50 ;
int y1 =100 ;
g.drawString(s2, x1, y1) ;
}
} // end of method paint
} // end of class Canvas4
觉得不错,你需要的东东,给你个思路吧
在 canvas 类的构造中增加监听,
public Canvas4(Chapter56n4 f)
{
parent = f ;
this.addMouseListener(new gMouseAdapter());
} // end of constructor method增加类:class gMouseAdapter extends MouseAdapter{
public void mouseClicked(MouseEvent e) {
System.out.println(e.getPoint());
//这里 根据 点击的点的位置,判断落在哪个 方块里面,将里面的颜色改变,
并且将数组的值作修改。 } }
public void mouseClicked(MouseEvent e) {
System.out.println(e.getPoint());
//这里 根据 点击的点的位置,判断落在哪个 方块里面,将里面的颜色改变,
并且将数组的值作修改。 } }
这个应该怎么判断啊? 我刚学,不怎么清楚,老师就说了画布什么得。 关于鼠标得他说谁会可以加分得所以就来问问
y 2-6如果point的x =3.3,y=4.3 就在方格一范围内当然这个需要花点儿时间计算了,自己做做吧,在遇到问题在说
那要看你方块的位置啰
p=e.getPoint();
p.x就是横坐标
p.y就是纵坐标 //从上到下也可用getX(),和getY();
然后在实现监听的方法时,有个MoustEvent e
void this_mouseMoved(MouseEvent e) {
e.getX();//....
e.getY();//....
}
//根据鼠标的位置可以响应你的方法或实现!