📄 calculator.java
字号:
E=0;
compute=0;
num_showing=false;
panel_display.repaint();
}
}
//back listener
class back_listener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(num_count>1)
{
if(num_count==if_point)
if_point=10;
num_count--;
panel_display.repaint();
}
else if(num_count>0)
{
if(num_count==if_point)
if_point=10;
num_count--;
minus=false;
panel_display.repaint();
}
}
}
//minus or positive
class m_or_p implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(num_count==0 && !minus)
{
minus=true;
num_count=1;
if_point=10;
state=0;
panel_display.repaint();
}
else if(num_count==1 && minus )
{
minus=false;
num_count=0;
if_point=10;
state=0;
panel_display.repaint();
}
}
}
public calPanel()
{
num_count=0;
if_point=10;
minus=false;
compute=0;
num_showing=false;
E=0;
state=0;
setLayout(new BorderLayout());
setBackground(new Color(105,105,105));
panel_display=new Display();
panel_display.setBackground(Color.yellow);
panel_button=new JPanel();
panel_button.setBackground(Color.pink);
panel_button.setLayout(new GridLayout(4,5,2,2));
this.add(panel_button,BorderLayout.SOUTH);
this.add(panel_display,BorderLayout.CENTER);
ActionListener n_listener=new numlistener();
ActionListener compute_listener=new Compute();
ActionListener dot =new pointer();
ActionListener eql =new result();
ActionListener cls=new clear_listener();
ActionListener bak=new back_listener();
ActionListener p_o_m =new m_or_p();
addbutton("7",n_listener);
addbutton("8",n_listener);
addbutton("9",n_listener);
addbutton("*",compute_listener);
addbutton("C",cls);
addbutton("4",n_listener);
addbutton("5",n_listener);
addbutton("6",n_listener);
addbutton("/",compute_listener);
addbutton("←",bak);
addbutton("1",n_listener);
addbutton("2",n_listener);
addbutton("3",n_listener);
addbutton("-",compute_listener);
addbutton("+/-",p_o_m);
addbutton("0",n_listener);
addbutton(".",dot);
addbutton("=",eql);
addbutton("+",compute_listener);
addbutton("x^y",compute_listener);
}
void addbutton(String name,ActionListener listener)
{
JButton button=new JButton(name);
panel_button.add(button);
button.addActionListener(listener);
button.setBackground(Color.gray);
button.setFont(new Font("SansSerif",Font.ITALIC,14));
button.setForeground(Color.white);
}
//show panel
class Display extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
if(error)
{ g2.setPaint(Color.red);
int x;
for(x=0;x<5;x++)
{
int[]ply_1x=new int[] {50*x+9,50*x+4+9,50*x+24+9,50*x+28+9,50*x+24+9,50*x+4+9};
int[]ply_1y=new int[] {12,8,8,12,16,16};
Polygon ply1=new Polygon(ply_1x,ply_1y,6);;
int[]ply_2x=new int[] {50*x-1+9,50*x-1+4+9,50*x-1+4+9,50*x-1+9,50*x-1-4+9,50*x-1-4+9};
int[]ply_2y=new int[] {13,17,35,39,35,17};
Polygon ply2=new Polygon(ply_2x,ply_2y,6);
int[]ply_5x=new int[] {50*x-1+9,50*x-1+4+9,50*x-1+4+9,50*x-1+9,50*x-1-4+9,50*x-1-4+9};
int[]ply_5y=new int[] {41,45,63,67,63,45};
Polygon ply5=new Polygon(ply_5x,ply_5y,6);
g2.fill(ply1);
g2.fill(ply2);
g2.fill(ply5);
}
for(x=1;x<5;x++)
{
int[]ply_3x=new int[] {50*x+29+9,50*x+29+4+9,50*x+29+4+9,50*x+29+9,50*x+29-4+9,50*x+29-4+9};
int[]ply_3y=new int[] {13,17,35,39,35,17};
Polygon ply3=new Polygon(ply_3x,ply_3y,6);
int[]ply_6x=new int[] {50*x+29+9,50*x+29+4+9,50*x+29+4+9,50*x+29+9,50*x+29-4+9,50*x+29-4+9};
int[]ply_6y=new int[] {41,45,63,67,63,45};
Polygon ply6=new Polygon(ply_6x,ply_6y,6);
g2.fill(ply3);
g2.fill(ply6);
}
for(x=0;x<3;x++)
{
int[]ply_4x=new int[] {50*x+9,50*x+4+9,50*x+24+9,50*x+28+9,50*x+24+9,50*x+4+9};
int[]ply_4y=new int[] {40,36,36,40,44,44};
Polygon ply4=new Polygon(ply_4x,ply_4y,6);
g2.fill(ply4);
}
int[]ply_4x=new int[] {200+9,200+4+9,200+24+9,200+28+9,200+24+9,200+4+9};
int[]ply_4y=new int[] {40,36,36,40,44,44};
Polygon ply4=new Polygon(ply_4x,ply_4y,6);
g2.fill(ply4);
int[]ply_70x=new int[] {0+9,4+9,22+9,26+9,22+9,4+9};
int[]ply_70y=new int[] {68,64,64,68,72,72};
Polygon ply70=new Polygon(ply_70x,ply_70y,6);
g2.fill(ply70);
int[]ply_7x=new int[] {150+9,150+4+9,150+24+9,150+28+9,150+24+9,150+4+9};
int[]ply_7y=new int[] {68,64,64,68,72,72};
Polygon ply7=new Polygon(ply_7x,ply_7y,6);
g2.fill(ply7);
}
else
{
if(E!=0)
{ Font font=new Font("SansSerif",Font.BOLD,82);
g2.setFont(font);
g2.setPaint(Color.GREEN);
String s= String.valueOf(E);
g2.drawString(s,550,70);
g2.setPaint(Color.red);
int[] ply_1x=new int[] {509,509+4,509+24,509+28,509+24,509+4};
int[] ply_1y=new int[] {12,8,8,12,16,16};
Polygon ply1=new Polygon(ply_1x,ply_1y,6);
int[] ply_2x=new int[] {508,508+4,508+4,508,508-4,508-4};
int[] ply_2y=new int[] {13,17,35,39,35,17};
Polygon ply2=new Polygon(ply_2x,ply_2y,6);
int[] ply_4x=new int[] {509,509+4,509+24,509+28,509+24,509+4};
int[] ply_4y=new int[] {40,36,36,40,44,44};
Polygon ply4=new Polygon(ply_4x,ply_4y,6);
int[] ply_5x=new int[] {508,508+4,508+4,508,508-4,508-4};
int[] ply_5y=new int[] {41,45,63,67,63,45};
Polygon ply5=new Polygon(ply_5x,ply_5y,6);
int[] ply_7x=new int[] {509,509+4,509+24,509+28,509+24,509+4};
int[] ply_7y=new int[] {68,64,64,68,72,72};
Polygon ply7=new Polygon(ply_7x,ply_7y,6);
g2.fill(ply1);
g2.fill(ply2);
g2.fill(ply4);
g2.fill(ply5);
g2.fill(ply7);
}
if(if_point<10)
{
g2.setPaint(Color.BLUE);
int i=if_point;
Rectangle2D point=new Rectangle2D.Double(45+50*(i-1),67,5,5);
g2.fill(point);
}
if(num_count>0)
{
g2.setPaint(Color.magenta);
if(minus)
{
int[] ply_4x=new int[] {9,9+4,9+24,9+28,9+24,9+4};
int[] ply_4y=new int[] {40,36,36,40,44,44};
Polygon ply4=new Polygon(ply_4x,ply_4y,6);
g2.fill(ply4);
}
else
{
number_show number=new number_show(display_num[0],1);
ArrayList arry=number.number();
for(int i=0;i<arry.size();i++)
{
g2.fill((Polygon)arry.get(i));
}
}
if(num_count>1)
{
for(int i=1;i<num_count;i++)
{
number_show number=new number_show(display_num[i],i+1);
ArrayList arry=number.number();
for(int x=0;x<arry.size();x++)
{
g2.fill((Polygon)arry.get(x));
}
}
}
}
}
}
}
//number display class
class number_show
{
private int num;
private int location;
private int[] ply_1x;
private int[] ply_2x;
private int[] ply_3x;
private int[] ply_4x;
private int[] ply_5x;
private int[] ply_6x;
private int[] ply_7x;
private int[] ply_1y;
private int[] ply_2y;
private int[] ply_3y;
private int[] ply_4y;
private int[] ply_5y;
private int[] ply_6y;
private int[] ply_7y;
private ArrayList poly;
public number_show(int n,int loc)
{
num=n;
location=loc;
int x=50*(location-1)+9;
int x1=x-1;
int x2=x+29;
ply_1x=new int[] {x,x+4,x+24,x+28,x+24,x+4};
ply_1y=new int[] {12,8,8,12,16,16};
ply_2x=new int[] {x1,x1+4,x1+4,x1,x1-4,x1-4};
ply_2y=new int[] {13,17,35,39,35,17};
ply_3x=new int[] {x2,x2+4,x2+4,x2,x2-4,x2-4};
ply_3y=new int[] {13,17,35,39,35,17};
ply_4x=new int[] {x,x+4,x+24,x+28,x+24,x+4};
ply_4y=new int[] {40,36,36,40,44,44};
ply_5x=new int[] {x1,x1+4,x1+4,x1,x1-4,x1-4};
ply_5y=new int[] {41,45,63,67,63,45};
ply_6x=new int[] {x2,x2+4,x2+4,x2,x2-4,x2-4};
ply_6y=new int[] {41,45,63,67,63,45};
ply_7x=new int[] {x,x+4,x+24,x+28,x+24,x+4};
ply_7y=new int[] {68,64,64,68,72,72};
poly=new ArrayList();
}
public ArrayList number()
{
Polygon ply1=new Polygon(ply_1x,ply_1y,6);
Polygon ply2=new Polygon(ply_2x,ply_2y,6);
Polygon ply3=new Polygon(ply_3x,ply_3y,6);
Polygon ply4=new Polygon(ply_4x,ply_4y,6);
Polygon ply5=new Polygon(ply_5x,ply_5y,6);
Polygon ply6=new Polygon(ply_6x,ply_6y,6);
Polygon ply7=new Polygon(ply_7x,ply_7y,6);
if(num==0)
{
poly.add(ply1);
poly.add(ply2);
poly.add(ply3);
poly.add(ply5);
poly.add(ply6);
poly.add(ply7);
}
else if(num==1)
{
poly.add(ply3);
poly.add(ply6);
}
else if(num==2)
{
poly.add(ply1);
poly.add(ply3);
poly.add(ply4);
poly.add(ply5);
poly.add(ply7);
}
else if(num==3)
{
poly.add(ply1);
poly.add(ply3);
poly.add(ply4);
poly.add(ply6);
poly.add(ply7);
}
else if(num==4)
{
poly.add(ply2);
poly.add(ply3);
poly.add(ply4);
poly.add(ply6);
}
else if(num==5)
{
poly.add(ply1);
poly.add(ply2);
poly.add(ply4);
poly.add(ply6);
poly.add(ply7);
}
else if(num==6)
{
poly.add(ply1);
poly.add(ply2);
poly.add(ply4);
poly.add(ply5);
poly.add(ply6);
poly.add(ply7);
}
else if(num==7)
{
poly.add(ply1);
poly.add(ply3);
poly.add(ply6);
}
else if(num==8)
{
poly.add(ply1);
poly.add(ply2);
poly.add(ply3);
poly.add(ply4);
poly.add(ply5);
poly.add(ply6);
poly.add(ply7);
}
else if(num==9)
{
poly.add(ply1);
poly.add(ply2);
poly.add(ply3);
poly.add(ply4);
poly.add(ply6);
poly.add(ply7);
}
return poly;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -