我写了一个类,画七段数码的,但是不知怎样调用,而且关键我的坐标不会处理,因为老师要求,把计算器放大,显示数码的面板也跟着放大。我头都大了,就是不会啊,我实在不知怎么处理坐标,所以把头一个坐标设为(50,50)。
class SevenDigitalDemo
{
public int x=50;
public int y=50;
public void UpDemoPainting(Graphics g)//UpDemoPainting
{
Polygon p1=new Polygon();
p1.addPoint(x,y);
p1.addPoint(x+20,y+20);
p1.addPoint(x+80,y+20);
p1.addPoint(x+100,y);
g.drawPolygon(p1);
g.setColor(Color.red);
g.fillPolygon(p1);}public void RightDemoPainting(Graphics g)//RightDemoPainting
{
Polygon p2=new Polygon();
p2.addPoint(x+83,y+23);
p2.addPoint(x+83,y+83);
p2.addPoint(x+96,y+96);
p2.addPoint(x+103,y+89);
p2.addPoint(x+103,y+3);
g.setColor(Color.red);
g.drawPolygon(p2);
g.fillPolygon(p2);
}
public void LeftDemoPainting(Graphics g)//LeftDemoPainting
{
Polygon p3=new Polygon();
p3.addPoint(x-3,y+3);
p3.addPoint(x-3,y+89);
p3.addPoint(x+4,y+96);
p3.addPoint(x+17,y+83);
p3.addPoint(x+17,y+23);
g.setColor(Color.red);
g.drawPolygon(p3);
g.fillPolygon(p3);
}
public void MidDemoPainting(Graphics g)//MidDemoPainting
{
Polygon p4=new Polygon();
p4.addPoint(x+20,y+86);
p4.addPoint(x+80,y+86);
p4.addPoint(x+92,y+98);
p4.addPoint(x+80,y+106);
p4.addPoint(x+20,y+106);
p4.addPoint(x+8,y+98);
g.drawPolygon(p4);
g.fillPolygon(p4);
}public void LeftdownDemoPainting(Graphics g)//LeftdownDemoPainting
{
Polygon p5=new Polygon();
p5.addPoint(x+4,y+100);
p5.addPoint(x+17,y+109);
p5.addPoint(x+17,y+169);
p5.addPoint(x-3,y+189);
p5.addPoint(x-3,y+107);
g.drawPolygon(p5);
g.fillPolygon(p5);
}public void RightdownDemoPainting(Graphics g)//RightdownDemoPainting
{
Polygon p6=new Polygon();
p6.addPoint(x+96,y+100);
p6.addPoint(x+103,y+107);
p6.addPoint(x+103,y+189);
p6.addPoint(x+83,y+169);
p6.addPoint(x+83,y+109);
g.drawPolygon(p6);
g.fillPolygon(p6);
}public void DownDemoPainting(Graphics g)//DownDemoPainting
{
Polygon p7=new Polygon();
p7.addPoint(x+20,y+172);
p7.addPoint(x+80,y+172);
p7.addPoint(x+100,y+192);
p7.addPoint(x,y+192);
g.drawPolygon(p7);
g.fillPolygon(p7);
}
}
老师的意思是七段数码一定要画出来,按下1的时候,就显示七段数码中的1
,按下2,就显示2。
--------
| |
| |
| |
|--------|
| |
| |
| |
| |
----------
即不同时间显示8的不同部位而已/
class SevenDigitalDemo
{
public int x=50;
public int y=50;
public void UpDemoPainting(Graphics g)//UpDemoPainting
{
Polygon p1=new Polygon();
p1.addPoint(x,y);
p1.addPoint(x+20,y+20);
p1.addPoint(x+80,y+20);
p1.addPoint(x+100,y);
g.drawPolygon(p1);
g.setColor(Color.red);
g.fillPolygon(p1);}public void RightDemoPainting(Graphics g)//RightDemoPainting
{
Polygon p2=new Polygon();
p2.addPoint(x+83,y+23);
p2.addPoint(x+83,y+83);
p2.addPoint(x+96,y+96);
p2.addPoint(x+103,y+89);
p2.addPoint(x+103,y+3);
g.setColor(Color.red);
g.drawPolygon(p2);
g.fillPolygon(p2);
}
public void LeftDemoPainting(Graphics g)//LeftDemoPainting
{
Polygon p3=new Polygon();
p3.addPoint(x-3,y+3);
p3.addPoint(x-3,y+89);
p3.addPoint(x+4,y+96);
p3.addPoint(x+17,y+83);
p3.addPoint(x+17,y+23);
g.setColor(Color.red);
g.drawPolygon(p3);
g.fillPolygon(p3);
}
public void MidDemoPainting(Graphics g)//MidDemoPainting
{
Polygon p4=new Polygon();
p4.addPoint(x+20,y+86);
p4.addPoint(x+80,y+86);
p4.addPoint(x+92,y+98);
p4.addPoint(x+80,y+106);
p4.addPoint(x+20,y+106);
p4.addPoint(x+8,y+98);
g.drawPolygon(p4);
g.fillPolygon(p4);
}public void LeftdownDemoPainting(Graphics g)//LeftdownDemoPainting
{
Polygon p5=new Polygon();
p5.addPoint(x+4,y+100);
p5.addPoint(x+17,y+109);
p5.addPoint(x+17,y+169);
p5.addPoint(x-3,y+189);
p5.addPoint(x-3,y+107);
g.drawPolygon(p5);
g.fillPolygon(p5);
}public void RightdownDemoPainting(Graphics g)//RightdownDemoPainting
{
Polygon p6=new Polygon();
p6.addPoint(x+96,y+100);
p6.addPoint(x+103,y+107);
p6.addPoint(x+103,y+189);
p6.addPoint(x+83,y+169);
p6.addPoint(x+83,y+109);
g.drawPolygon(p6);
g.fillPolygon(p6);
}public void DownDemoPainting(Graphics g)//DownDemoPainting
{
Polygon p7=new Polygon();
p7.addPoint(x+20,y+172);
p7.addPoint(x+80,y+172);
p7.addPoint(x+100,y+192);
p7.addPoint(x,y+192);
g.drawPolygon(p7);
g.fillPolygon(p7);
}
}
老师的意思是七段数码一定要画出来,按下1的时候,就显示七段数码中的1
,按下2,就显示2。
--------
| |
| |
| |
|--------|
| |
| |
| |
| |
----------
即不同时间显示8的不同部位而已/
import java.awt.*;
import javax.swing.*;class Drawpolyline extends Frame
{
public Drawpolyline()
{
super("drawpolyline");
//setSize(300,350);
pack();
show();
}
public void paint(Graphics g)
{
Polygon p1=new Polygon();
p1.addPoint(width/10,height/7);
p1.addPoint(width/10+20,height/7+20);
p1.addPoint(width/10+80,height/7+20);//上
p1.addPoint(width/10+100,height/7);
g.drawPolygon(p1);
g.setColor(Color.red);
g.fillPolygon(p1);
}
public void setBounds(int x,int y,int width,int height)
{ super.setBounds(x,y,width,height);
repaint();
}
public Dimension getMinimumSize()
{
return new Dimension(width,height);
}
public Dimension getPreferredSize()
{
return new Dimension(width,height);
}
private int width=300,height=350;
public static void main(String[] args)
{
new Drawpolyline();
}
}
import javax.swing.*;class Star2 extends JComponent
{
public void paint(Graphics g)
{
double x1,x2,y1,y2;
for(double angle=0;angle<Math.PI;angle=angle+Math.PI/16)
{
x1=Math.cos(angle)*radius+radius;
y1=Math.sin(angle)*radius+radius;
x2=Math.cos(angle+Math.PI)*radius+radius;
y2=Math.sin(angle+Math.PI)*radius+radius;
g.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
}
}
public void setBounds(int x,int y,int width,int height)
{
radius=Math.min(width,height)/2;
super.setBounds(x,y,width,height);
repaint();
}
public Dimension getMinimumSize()
{
return new Dimension(2*radius,2*radius);
}
public Dimension getPreferredSize()
{
return new Dimension(2*radius,2*radius);
}
private int radius=100;
}public class Nesting{
public static void main(String[] args)
{
JFrame frame=new JFrame("Nesting containers");
Container c=frame.getContentPane();
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(4,1));
panel.add(new JButton("One"));
panel.add(new JButton("Two"));
panel.add(new JButton("Three"));
panel.add(new JButton("Four"));
c.add(panel,BorderLayout.WEST);
c.add(new Star2(),BorderLayout.CENTER);
frame.pack();
frame.show();
}
}
昨天我发给你的,还是有点不对.你运行一下就知道那儿不对了.上面的事例程序有public void setBounds(int x,int y,int width,int height)
{
radius=Math.min(width,height)/2;
super.setBounds(x,y,width,height);
repaint();
}
public Dimension getMinimumSize()
{
return new Dimension(2*radius,2*radius);
}
public Dimension getPreferredSize()
{
return new Dimension(2*radius,2*radius);
}而下面我写的好象没什么用.为什么??今天我要把这一段代码添加到我的计算器中了.import java.awt.*;
import javax.swing.*;class Drawpolyline extends Frame
{
public Drawpolyline()
{
super("drawpolyline");
//setSize(300,350);
pack();
show();
} public void paint(Graphics g)
{
Dimension tsize=getSize(); g.setColor(getBackground());
g.fillRect(0, 0, tsize.width, tsize.height);
g.setColor(getForeground());
width=tsize.width*3/8;
height=tsize.height;
; x[0]=a;
y[0]=b+5*height/232;
x[1]=x[0]+26*width/139;
y[1]=y[0]+26*height/232;
x[2]=x[1];
y[2]=y[1]+67*height/232;
x[3]=x[2]-15*width/139;
y[3]=y[2]+15*height/232;
x[4]=a;
y[4]=y[0]+96*height/232;
Polygon p1=new Polygon(x,y,5);
g.drawPolygon(p1);
g.setColor(Color.red);
g.fillPolygon(p1);
x1[0]=x[0]+5*width/139;
y1[0]=y[0]-5*height/232;
x1[1]=x1[0]+127*width/139;
y1[1]=y1[0];
x1[2]=x1[1]-26*width/139;
y1[2]=y1[1]+26*height/232;
x1[3]=x[0]+31*width/139;
y1[3]=y1[0]+26*height/232;
Polygon p2=new Polygon(x1,y1,4);
g.drawPolygon(p2);
g.setColor(Color.RED);
g.fillPolygon(p2);
x2[0]=x1[2]+5*width/139;
y2[0]=y1[2]+5*height/232;
x2[1]=x2[0]+26*width/139;
y2[1]=y2[0]-26*height/232;
x2[2]=x2[1];
y2[2]=y2[1]+96*height/232;
x2[3]=x2[2]-8*width/139;
y2[3]=y[2]+15*height/232;
x2[4]=x2[0];
y2[4]=y2[0]+67*height/232;
Polygon p3=new Polygon(x2,y2,5);
g.drawPolygon(p3);
g.setColor(Color.RED);
g.fillPolygon(p3);
x3[0]=x[1];
y3[0]=y[2]+8*height/232;
x3[1]=x3[0]+86*width/139;
y3[1]=y3[0];
x3[2]=x3[1]+12*width/139;
y3[2]=y3[1]+12*height/232;
x3[3]=x3[2]-12*width/139;
y3[3]=y3[2]+12*height/232;
x3[4]=x3[3]-86*width/139;
y3[4]=y3[3];
x3[5]=x3[4]-12*width/139;
y3[5]=y3[3]-12*height/232;
Polygon p4=new Polygon(x3,y3,6);
g.drawPolygon(p4);
g.setColor(Color.RED);
g.fillPolygon(p4);
x4[0]=x3[4];
y4[0]=y3[4]+8*height/232;
x4[1]=x3[4];
y4[1]=y4[0]+67*height/232;
x4[2]=x4[1]-26*width/139;
y4[2]=y4[1]+26*height/232;
x4[3]=x4[2];
y4[3]=y4[2]-96*height/232;
x4[4]=x4[3]+12*width/139;
y4[4]=y4[3]-11*height/232;
Polygon p5=new Polygon(x4,y4,5);
g.drawPolygon(p5);
g.setColor(Color.RED);
g.fillPolygon(p5);
x5[0]=x4[1]+5*width/139;;
y5[0]=y4[1]+5*height/232;
x5[1]=x5[0]+80*width/139;
y5[1]=y5[0];
x5[2]=x5[1]+26*width/139;
y5[2]=y5[1]+26*height/232;
x5[3]=x5[0]-26*width/139;
y5[3]=y5[0]+26*height/232;
Polygon p6=new Polygon(x5,y5,4);
g.drawPolygon(p6);
g.setColor(Color.RED);
g.fillPolygon(p6);
x6[0]=x2[3];
y6[0]=y2[3]+8*height/232;
x6[1]=x6[0]+10*width/139;
y6[1]=y6[0]+10*height/232;
x6[2]=x6[1];
y6[2]=y6[1]+96*height/232;
x6[3]=x6[2]-26*width/139;
y6[3]=y6[2]-26*height/232;
x6[4]=x6[3];
y6[4]=y3[4]+8*height/232;
Polygon p7=new Polygon(x6,y6,5);
g.drawPolygon(p7);
g.setColor(Color.RED);
g.fillPolygon(p7);
}
public void setBounds(int x,int y,int width,int height)
{
super.setBounds(x,y,width,height);
repaint();
}
public Dimension getMinimumSize()
{
return new Dimension(width,height);
}
public Dimension getPreferredSize()
{
return new Dimension(width,height);
}
private int width=70,height=117,a=100,b=50;
; int[] x =new int[5];
int[] y =new int[5];
int[] x1 =new int[4];
int[] y1 =new int[4];
int[] x2=new int[5];
int[] y2 =new int[5];
int[] x3=new int[6];
int[] y3 =new int[6];
int[] x4=new int[5];
int[] y4=new int[5];
int[] x5 =new int[4];
int[] y5 =new int[4];
int[] x6 =new int[5];
int[] y6 =new int[5];
public static void main(String[] args)
{
new Drawpolyline();
}
}
不好理解!!!!!!!!!!!!!!
改成下面的样子就没有问题了。
import java.awt.*;
import javax.swing.*;class Drawpolyline extends JComponent
{
public void paint(Graphics g)
{
g.setColor(getBackground());
g.fillRect(0, 0,width,height);
g.setColor(getForeground()); x[0]=a;
y[0]=b+5*height/232;
x[1]=x[0]+26*width/139;
y[1]=y[0]+26*height/232;
x[2]=x[1];
y[2]=y[1]+67*height/232;
x[3]=x[2]-15*width/139;
y[3]=y[2]+15*height/232;
x[4]=a;
y[4]=y[0]+96*height/232;
Polygon p1=new Polygon(x,y,5);
g.drawPolygon(p1);
g.setColor(Color.red);
g.fillPolygon(p1);
x1[0]=x[0]+5*width/139;
y1[0]=y[0]-5*height/232;
x1[1]=x1[0]+127*width/139;
y1[1]=y1[0];
x1[2]=x1[1]-26*width/139;
y1[2]=y1[1]+26*height/232;
x1[3]=x[0]+31*width/139;
y1[3]=y1[0]+26*height/232;
Polygon p2=new Polygon(x1,y1,4);
g.drawPolygon(p2);
g.setColor(Color.RED);
g.fillPolygon(p2);
x2[0]=x1[2]+5*width/139;
y2[0]=y1[2]+5*height/232;
x2[1]=x2[0]+26*width/139;
y2[1]=y2[0]-26*height/232;
x2[2]=x2[1];
y2[2]=y2[1]+96*height/232;
x2[3]=x2[2]-8*width/139;
y2[3]=y[2]+15*height/232;
x2[4]=x2[0];
y2[4]=y2[0]+67*height/232;
Polygon p3=new Polygon(x2,y2,5);
g.drawPolygon(p3);
g.setColor(Color.RED);
g.fillPolygon(p3);
x3[0]=x[1];
y3[0]=y[2]+8*height/232;
x3[1]=x3[0]+86*width/139;
y3[1]=y3[0];
x3[2]=x3[1]+12*width/139;
y3[2]=y3[1]+12*height/232;
x3[3]=x3[2]-12*width/139;
y3[3]=y3[2]+12*height/232;
x3[4]=x3[3]-86*width/139;
y3[4]=y3[3];
x3[5]=x3[4]-12*width/139;
y3[5]=y3[3]-12*height/232;
Polygon p4=new Polygon(x3,y3,6);
g.drawPolygon(p4);
g.setColor(Color.RED);
g.fillPolygon(p4);
x4[0]=x3[4];
y4[0]=y3[4]+8*height/232;
x4[1]=x3[4];
y4[1]=y4[0]+67*height/232;
x4[2]=x4[1]-26*width/139;
y4[2]=y4[1]+26*height/232;
x4[3]=x4[2];
y4[3]=y4[2]-96*height/232;
x4[4]=x4[3]+12*width/139;
y4[4]=y4[3]-11*height/232;
Polygon p5=new Polygon(x4,y4,5);
g.drawPolygon(p5);
g.setColor(Color.RED);
g.fillPolygon(p5);
x5[0]=x4[1]+5*width/139;;
y5[0]=y4[1]+5*height/232;
x5[1]=x5[0]+80*width/139;
y5[1]=y5[0];
x5[2]=x5[1]+26*width/139;
y5[2]=y5[1]+26*height/232;
x5[3]=x5[0]-26*width/139;
y5[3]=y5[0]+26*height/232;
Polygon p6=new Polygon(x5,y5,4);
g.drawPolygon(p6);
g.setColor(Color.RED);
g.fillPolygon(p6);
x6[0]=x2[3];
y6[0]=y2[3]+8*height/232;
x6[1]=x6[0]+10*width/139;
y6[1]=y6[0]+10*height/232;
x6[2]=x6[1];
y6[2]=y6[1]+96*height/232;
x6[3]=x6[2]-26*width/139;
y6[3]=y6[2]-26*height/232;
x6[4]=x6[3];
y6[4]=y3[4]+8*height/232;
Polygon p7=new Polygon(x6,y6,5);
g.drawPolygon(p7);
g.setColor(Color.RED);
g.fillPolygon(p7);
} public void setBounds(int x,int y,int width,int height)
{
this.width=width;
this.height=height;
super.setBounds(x,y,width,height);
repaint();
}
public Dimension getMinimumSize()
{
return new Dimension(width,height);
}
public Dimension getPreferredSize()
{
return new Dimension(width,height);
}
private int width=200,height=150,a=0,b=0;
; int[] x =new int[5];
int[] y =new int[5];
int[] x1 =new int[4];
int[] y1 =new int[4];
int[] x2=new int[5];
int[] y2 =new int[5];
int[] x3=new int[6];
int[] y3 =new int[6];
int[] x4=new int[5];
int[] y4=new int[5];
int[] x5 =new int[4];
int[] y5 =new int[4];
int[] x6 =new int[5];
int[] y6 =new int[5];
public static void main(String[] args)
{
JFrame f=new JFrame("test");
f.getContentPane().add(new Drawpolyline());
f.pack();
f.setVisible(true);
}
}
写事件时我想把诸如g.drawPolygon(p7);
g.setColor(Color.RED);
g.fillPolygon(p7);都注释掉,而在写事件的时候写,不如按下一的时候,就把最右边的两段着色。但是我不知为什么p不可见,当然我把SevenDigitalDemo当成内部类来用之后,当然还存在一个g问题,在public void actionPerformed(ActionEvent e)好象不能用到g所以很难实行,你有什么好办法,建议以下。import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.color.*;
import javax.swing.JOptionPane;
class SevenDigitalDemo extends JPanel
{ Polygon p1,p2,p3,p4,p5,p6,p7; public SevenDigitalDemo()
{
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Dimension tsize=getSize(); g.setColor(getBackground());
g.fillRect(0, 0, tsize.width, tsize.height);
g.setColor(getForeground());
width=tsize.width/3;
height=tsize.height*5/6;
a=tsize.width/40;
b=tsize.height/10;
x[0]=a;
y[0]=b+12;//+5*height/232;
x[1]=x[0]+26*width/139;
y[1]=y[0]+26*height/232;
x[2]=x[1];
y[2]=y[1]+67*height/232;
x[3]=x[2]-15*width/139;
y[3]=y[2]+15*height/232;
x[4]=a;
y[4]=y[0]+96*height/232;
Polygon p1=new Polygon(x,y,5);
g.drawPolygon(p1);
g.setColor(Color.red);
g.fillPolygon(p1);
x1[0]=x[0]+5*width/139;
y1[0]=y[0]-5*height/232;
x1[1]=x1[0]+127*width/139;
y1[1]=y1[0];
x1[2]=x1[1]-26*width/139;
y1[2]=y1[1]+26*height/232;
x1[3]=x[0]+31*width/139;
y1[3]=y1[0]+26*height/232;
Polygon p2=new Polygon(x1,y1,4);
g.drawPolygon(p2);
g.setColor(Color.RED);
g.fillPolygon(p2);
x2[0]=x1[2]+5*width/139;
y2[0]=y1[2]+5*height/232;
x2[1]=x2[0]+26*width/139;
y2[1]=y2[0]-26*height/232;
x2[2]=x2[1];
y2[2]=y2[1]+96*height/232;
x2[3]=x2[2]-8*width/139;
y2[3]=y[2]+15*height/232;
x2[4]=x2[0];
y2[4]=y2[0]+67*height/232;
Polygon p3=new Polygon(x2,y2,5);
g.drawPolygon(p3);
g.setColor(Color.RED);
g.fillPolygon(p3);
x3[0]=x[1];
y3[0]=y[2]+8*height/232;
x3[1]=x3[0]+86*width/139;
y3[1]=y3[0];
x3[2]=x3[1]+12*width/139;
y3[2]=y3[1]+12*height/232;
x3[3]=x3[2]-12*width/139;
y3[3]=y3[2]+12*height/232;
x3[4]=x3[3]-86*width/139;
y3[4]=y3[3];
x3[5]=x3[4]-12*width/139;
y3[5]=y3[3]-12*height/232;
Polygon p4=new Polygon(x3,y3,6);
g.drawPolygon(p4);
g.setColor(Color.RED);
g.fillPolygon(p4);
x4[0]=x3[4];
y4[0]=y3[4]+8*height/232;
x4[1]=x3[4];
y4[1]=y4[0]+67*height/232;
x4[2]=x4[1]-26*width/139;
y4[2]=y4[1]+26*height/232;
x4[3]=x4[2];
y4[3]=y4[2]-96*height/232;
x4[4]=x4[3]+12*width/139;
y4[4]=y4[3]-11*height/232;
Polygon p5=new Polygon(x4,y4,5);
g.drawPolygon(p5);
g.setColor(Color.RED);
g.fillPolygon(p5);
x5[0]=x4[1]+5*width/139;;
y5[0]=y4[1]+5*height/232;
x5[1]=x5[0]+80*width/139;
y5[1]=y5[0];
x5[2]=x5[1]+26*width/139;
y5[2]=y5[1]+26*height/232;
x5[3]=x5[0]-26*width/139;
y5[3]=y5[0]+26*height/232;
Polygon p6=new Polygon(x5,y5,4);
g.drawPolygon(p6);
g.setColor(Color.RED);
g.fillPolygon(p6);
x6[0]=x2[3];
y6[0]=y2[3]+8*height/232;
x6[1]=x6[0]+10*width/139;
y6[1]=y6[0]+10*height/232;
x6[2]=x6[1];
y6[2]=y6[1]+96*height/232;
x6[3]=x6[2]-26*width/139;
y6[3]=y6[2]-26*height/232;
x6[4]=x6[3];
y6[4]=y3[4]+8*height/232;
Polygon p7=new Polygon(x6,y6,5);
g.drawPolygon(p7);
g.setColor(Color.RED);
g.fillPolygon(p7);
}
public void setBounds(int x,int y,int width,int height)
{
this.width=width;
this.height=height;
super.setBounds(x,y,width,height);
repaint();
}
public Dimension getMinimumSize()
{
return new Dimension(width,height);
}
public Dimension getPreferredSize()
{
return new Dimension(width,height);
}
private int width,height,a,b; int[] x =new int[5];
int[] y =new int[5];
int[] x1 =new int[4];
int[] y1 =new int[4];
int[] x2=new int[5];
int[] y2 =new int[5];
int[] x3=new int[6];
int[] y3 =new int[6];
int[] x4=new int[5];
int[] y4=new int[5];
int[] x5 =new int[4];
int[] y5 =new int[4];
int[] x6 =new int[5];
int[] y6 =new int[5];
}
public class Calculator extends JFrame implements ActionListener
{
JButton button0,button1,button2,button3,button4,button5,button6,button7,button8,button9;
JButton buttonBk, buttonCe, buttonC;
JButton buttonMC, buttonMR, buttonMS, buttonMAdd;
JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,
buttonDiv, buttonMod;
JButton buttonSqrt, buttonDaoshu, buttonEqual;
JTextField textOutput;
public Calculator()
{
super("计算器程序");
Container container=getContentPane();
JPanel panel=new JPanel();
container.add(panel);
panel.setLayout(new BorderLayout());
SevenDigitalDemo panel1=new SevenDigitalDemo();
panel1.setBackground(Color.RED);
panel1.add(new JButton("bbb"));
panel.add(panel1,BorderLayout.NORTH);
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
panel.add(panel2,BorderLayout.SOUTH);
panel2.setLayout(new BorderLayout());
panel2.add(panel3,BorderLayout.NORTH);
panel3.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton buttonBk = new JButton("Backspace");
buttonBk.setForeground(new Color(255, 0, 0));
JButton buttonCe = new JButton("CE");
buttonCe.setForeground(new Color(255, 0, 0));
JButton buttonC = new JButton("C");
buttonC.setForeground(new Color(255, 0, 0));
JButton buttonQuit=new JButton("Quit");
buttonQuit.setForeground(new Color(255, 0, 0));
buttonBk.addActionListener(this);
buttonCe.addActionListener(this);
buttonC.addActionListener(this);
buttonQuit.addActionListener(this);
panel3.add(buttonBk);
panel3.add(buttonCe);
panel3.add(buttonC);
panel3.add(buttonQuit);
JPanel panel4=new JPanel();
panel2.add(panel4,BorderLayout.CENTER);
panel4.setLayout(new GridLayout(4,5));
JButton button0=new JButton("0");
button0.setForeground(Color.BLUE);
button0.addActionListener(this);
JButton button1=new JButton("1");
button1.setForeground(Color.BLUE);
button1.addActionListener(this);
JButton button2=new JButton("2");
button2.setForeground(Color.BLUE);
button2.addActionListener(this);
JButton button3=new JButton("3");
button3.setForeground(Color.BLUE);
button3.addActionListener(this);
JButton button4=new JButton("4");
button4.setForeground(Color.BLUE);
button4.addActionListener(this);
JButton button5=new JButton("5");
button5.setForeground(Color.BLUE);
button5.addActionListener(this);
JButton button6=new JButton("6");
button6.setForeground(Color.BLUE);
button6.addActionListener(this);
JButton button7=new JButton("7");
button7.setForeground(Color.BLUE);
button7.addActionListener(this);
JButton button8=new JButton("8");
button8.setForeground(Color.BLUE);
button8.addActionListener(this);
JButton button9=new JButton("9");
button9.setForeground(Color.BLUE);
button9.addActionListener(this);
JButton buttonDiv=new JButton("/");
buttonDiv.setForeground(Color.RED);
buttonDiv.addActionListener(this);
JButton buttonSqrt=new JButton("sqrt");
buttonSqrt.setForeground(Color.BLUE);
buttonSqrt.addActionListener(this);
JButton buttonMul=new JButton("*");
buttonMul.setForeground(Color.RED);
buttonMul.addActionListener(this);
JButton buttonMod=new JButton("%");
buttonMod.setForeground(Color.BLUE);
buttonMod.addActionListener(this);
JButton buttonSub=new JButton("-");
buttonSub.setForeground(Color.RED);
buttonSub.addActionListener(this);
JButton buttonDaoshu=new JButton("1/x");
buttonDaoshu.setForeground(Color.BLUE);
buttonDaoshu.addActionListener(this);
JButton buttonAddandSub=new JButton("+/-");
buttonAddandSub.setForeground(Color.BLUE);
buttonAddandSub.addActionListener(this);
JButton buttonPoint=new JButton(".");
buttonPoint.setForeground(Color.BLUE);
buttonPoint.addActionListener(this);
JButton buttonAdd=new JButton("+");
buttonAdd.setForeground(Color.BLUE);
buttonAdd.addActionListener(this);
JButton buttonEqual=new JButton("=");
buttonEqual.setForeground(Color.red);
buttonEqual.addActionListener(this);
panel4.add(button7);
panel4.add(button8);
panel4.add(button9);
panel4.add(buttonDiv);
panel4.add(buttonSqrt);
panel4.add(button4);
panel4.add(button5);
panel4.add(button6);
panel4.add(buttonMul);
panel4.add(buttonMod);
panel4.add(button1);
panel4.add(button2);
panel4.add(button3);
panel4.add(buttonSub);
panel4.add(buttonDaoshu);
panel4.add(button0);
panel4.add(buttonAddandSub);
panel4.add(buttonPoint);
panel4.add(buttonAdd);
panel4.add(buttonEqual);
setSize(350,300);
show();
}
public static void main(String[] args)
{
Calculator calculator=new Calculator();
calculator.setDefaultCloseOperation(Calculator.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button0)
{
}
}
}
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;public class Calculator extends JFrame implements ActionListener
{
JButton button0,button1,button2,button3,button4,button5,button6,button7,button8,button9;
JButton buttonBk, buttonCe, buttonC;
JButton buttonMC, buttonMR, buttonMS, buttonMAdd;
JButton buttonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul,
buttonDiv, buttonMod;
JButton buttonSqrt, buttonDaoshu, buttonEqual;
DecimalComponent panel1;
JTextField textOutput;
public Calculator()
{
super("计算器程序");
Container container=getContentPane();
JPanel panel=new JPanel();
container.add(panel);
panel.setLayout(new BorderLayout());
panel1=new DecimalComponent();
panel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
panel.add(panel1,BorderLayout.CENTER);
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
panel.add(panel2,BorderLayout.SOUTH);
panel2.setLayout(new BorderLayout());
panel2.add(panel3,BorderLayout.NORTH);
panel3.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton buttonBk = new JButton("Backspace");
buttonBk.setForeground(new Color(255, 0, 0));
JButton buttonCe = new JButton("CE");
buttonCe.setForeground(new Color(255, 0, 0));
JButton buttonC = new JButton("C");
buttonC.setForeground(new Color(255, 0, 0));
JButton buttonQuit=new JButton("Quit");
buttonQuit.setForeground(new Color(255, 0, 0));
buttonBk.addActionListener(this);
buttonCe.addActionListener(this);
buttonC.addActionListener(this);
buttonQuit.addActionListener(this);
panel3.add(buttonBk);
panel3.add(buttonCe);
panel3.add(buttonC);
panel3.add(buttonQuit);
JPanel panel4=new JPanel();
panel2.add(panel4,BorderLayout.CENTER);
panel4.setLayout(new GridLayout(4,5));
JButton button0=new JButton("0");
button0.setForeground(Color.BLUE);
button0.addActionListener(this);
JButton button1=new JButton("1");
button1.setForeground(Color.BLUE);
button1.addActionListener(this);
JButton button2=new JButton("2");
button2.setForeground(Color.BLUE);
button2.addActionListener(this);
JButton button3=new JButton("3");
button3.setForeground(Color.BLUE);
button3.addActionListener(this);
JButton button4=new JButton("4");
button4.setForeground(Color.BLUE);
button4.addActionListener(this);
JButton button5=new JButton("5");
button5.setForeground(Color.BLUE);
button5.addActionListener(this);
JButton button6=new JButton("6");
button6.setForeground(Color.BLUE);
button6.addActionListener(this);
JButton button7=new JButton("7");
button7.setForeground(Color.BLUE);
button7.addActionListener(this);
JButton button8=new JButton("8");
button8.setForeground(Color.BLUE);
button8.addActionListener(this);
JButton button9=new JButton("9");
button9.setForeground(Color.BLUE);
button9.addActionListener(this);
JButton buttonDiv=new JButton("/");
buttonDiv.setForeground(Color.RED);
buttonDiv.addActionListener(this);
JButton buttonSqrt=new JButton("sqrt");
buttonSqrt.setForeground(Color.BLUE);
buttonSqrt.addActionListener(this);
JButton buttonMul=new JButton("*");
buttonMul.setForeground(Color.RED);
buttonMul.addActionListener(this);
JButton buttonMod=new JButton("%");
buttonMod.setForeground(Color.BLUE);
buttonMod.addActionListener(this);
JButton buttonSub=new JButton("-");
buttonSub.setForeground(Color.RED);
buttonSub.addActionListener(this);
JButton buttonDaoshu=new JButton("1/x");
buttonDaoshu.setForeground(Color.BLUE);
buttonDaoshu.addActionListener(this);
JButton buttonAddandSub=new JButton("+/-");
buttonAddandSub.setForeground(Color.BLUE);
buttonAddandSub.addActionListener(this);
JButton buttonPoint=new JButton(".");
buttonPoint.setForeground(Color.BLUE);
buttonPoint.addActionListener(this);
JButton buttonAdd=new JButton("+");
buttonAdd.setForeground(Color.BLUE);
buttonAdd.addActionListener(this);
JButton buttonEqual=new JButton("=");
buttonEqual.setForeground(Color.red);
buttonEqual.addActionListener(this);
panel4.add(button7);
panel4.add(button8);
panel4.add(button9);
panel4.add(buttonDiv);
panel4.add(buttonSqrt);
panel4.add(button4);
panel4.add(button5);
panel4.add(button6);
panel4.add(buttonMul);
panel4.add(buttonMod);
panel4.add(button1);
panel4.add(button2);
panel4.add(button3);
panel4.add(buttonSub);
panel4.add(buttonDaoshu);
panel4.add(button0);
panel4.add(buttonAddandSub);
panel4.add(buttonPoint);
panel4.add(buttonAdd);
panel4.add(buttonEqual);
pack();
show();
}
public static void main(String[] args)
{
Calculator calculator=new Calculator();
calculator.setDefaultCloseOperation(Calculator.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
panel1.setNum(-1234567890.456);
}
}
import javax.swing.*;
import java.text.*;public class DecimalComponent extends JComponent
{
private int ComWidth=600,ComHeight=80;
private int width,height;
private int X=0,Y=0;
private String num="";
private int style[]=new int[8];
DecimalFormat fmt=new DecimalFormat("####.###############");
public DecimalComponent(){
this.setMinimumSize(new Dimension(ComWidth,ComHeight));
this.setPreferredSize(new Dimension(ComWidth,ComHeight));
} public void setNum(double a){
num=fmt.format(a);
if(num.length()>15)
num=num.substring(0,15);
repaint();
} public void paint(Graphics g)
{
this.setBackground(Color.white);
g.setColor(getBackground());
g.fillRect(0, 0,ComWidth,ComHeight);
g.setColor(getForeground());
width=ComWidth/17;
int widthAll=ComWidth/15;
height=7*ComHeight/8;
Y=ComHeight/16;
X=ComWidth;
for(int i=num.length()-1;i>=0;i--){
if(num.charAt(i)!='.')
X-=widthAll;
else
X-=widthAll/5;
getPositions(X,Y);
convert(num.charAt(i));
draw(g);
}
} public void setBounds(int x,int y,int width,int height)
{
this.ComWidth=width;
this.ComHeight=height;
super.setBounds(x,y,width,height);
repaint();
}
public Dimension getMinimumSize()
{
return new Dimension(ComWidth,ComHeight);
}
public Dimension getPreferredSize()
{
return new Dimension(ComWidth,ComHeight);
}
private void convert(char a){
switch(a){
case '0':
for(int i=0;i<8;i++) style[i]=1;
style[6]=style[7]=0;
break;
case '1':
for(int i=0;i<8;i++) style[i]=0;
style[1]=style[2]=1;
style[7]=0;
break;
case '2':
for(int i=0;i<8;i++) style[i]=1;
style[5]=style[2]=style[7]=0;
break;
case '3':
for(int i=0;i<8;i++) style[i]=1;
style[4]=style[5]=style[7]=0;
break;
case '4':
for(int i=0;i<8;i++) style[i]=1;
style[0]=style[3]=style[4]=style[7]=0;
break;
case '5':
for(int i=0;i<8;i++) style[i]=1;
style[1]=style[4]=style[7]=0;
break;
case '6':
for(int i=0;i<8;i++) style[i]=1;
style[1]=style[7]=0;
break;
case '7':
for(int i=0;i<8;i++) style[i]=0;
style[0]=style[1]=style[2]=1;
style[7]=0;
break;
case '8':
for(int i=0;i<8;i++) style[i]=1;
style[7]=0;
break;
case '9':
for(int i=0;i<8;i++) style[i]=1;
style[4]=style[7]=0;
break;
case '.':
for(int i=0;i<8;i++) style[i]=0;
style[7]=1;
break;
case '-':
for(int i=0;i<8;i++) style[i]=0;
style[6]=1;
break;
default:
for(int i=0;i<8;i++) style[i]=1;
style[1]=style[2]=0;
break;
}
}
private void getPositions(int a,int b){
x[0]=a;
y[0]=b+5*height/232;
x[1]=x[0]+26*width/139;
y[1]=y[0]+26*height/232;
x[2]=x[1];
y[2]=y[1]+67*height/232;
x[3]=x[2]-15*width/139;
y[3]=y[2]+15*height/232;
x[4]=a;
y[4]=y[0]+96*height/232;
///////////////////////
x1[0]=x[0]+5*width/139;
y1[0]=y[0]-5*height/232;
x1[1]=x1[0]+127*width/139;
y1[1]=y1[0];
x1[2]=x1[1]-26*width/139;
y1[2]=y1[1]+26*height/232;
x1[3]=x[0]+31*width/139;
y1[3]=y1[0]+26*height/232;
/////////////////////////
x2[0]=x1[2]+5*width/139;
y2[0]=y1[2]+5*height/232;
x2[1]=x2[0]+26*width/139;
y2[1]=y2[0]-26*height/232;
x2[2]=x2[1];
y2[2]=y2[1]+96*height/232;
x2[3]=x2[2]-8*width/139;
y2[3]=y[2]+15*height/232;
x2[4]=x2[0];
y2[4]=y2[0]+67*height/232;
//////////////////////////
x3[0]=x[1];
y3[0]=y[2]+8*height/232;
x3[1]=x3[0]+86*width/139;
y3[1]=y3[0];
x3[2]=x3[1]+12*width/139;
y3[2]=y3[1]+12*height/232;
x3[3]=x3[2]-12*width/139;
y3[3]=y3[2]+12*height/232;
x3[4]=x3[3]-86*width/139;
y3[4]=y3[3];
x3[5]=x3[4]-12*width/139;
y3[5]=y3[3]-12*height/232;
//////////////////////////
x4[0]=x3[4];
y4[0]=y3[4]+8*height/232;
x4[1]=x3[4];
y4[1]=y4[0]+67*height/232;
x4[2]=x4[1]-26*width/139;
y4[2]=y4[1]+26*height/232;
x4[3]=x4[2];
y4[3]=y4[2]-96*height/232;
x4[4]=x4[3]+12*width/139;
y4[4]=y4[3]-11*height/232;
////////////////////////////
x5[0]=x4[1]+5*width/139;;
y5[0]=y4[1]+5*height/232;
x5[1]=x5[0]+80*width/139;
y5[1]=y5[0];
x5[2]=x5[1]+26*width/139;
y5[2]=y5[1]+26*height/232;
x5[3]=x5[0]-26*width/139;
y5[3]=y5[0]+26*height/232;
///////////////////////////
x6[0]=x2[3];
y6[0]=y2[3]+8*height/232;
x6[1]=x6[0]+10*width/139;
y6[1]=y6[0]+10*height/232;
x6[2]=x6[1];
y6[2]=y6[1]+96*height/232;
x6[3]=x6[2]-26*width/139;
y6[3]=y6[2]-26*height/232;
x6[4]=x6[3];
y6[4]=y3[4]+8*height/232;
}
private void draw(Graphics g){
if(style[5]==1){
Polygon p1=new Polygon(x,y,5);
g.drawPolygon(p1);
g.setColor(Color.red);
g.fillPolygon(p1);
}
if(style[0]==1){
Polygon p2=new Polygon(x1,y1,4);
g.drawPolygon(p2);
g.setColor(Color.RED);
g.fillPolygon(p2);
}
if(style[1]==1){
Polygon p3=new Polygon(x2,y2,5);
g.drawPolygon(p3);
g.setColor(Color.RED);
g.fillPolygon(p3);
}
if(style[6]==1){
Polygon p4=new Polygon(x3,y3,6);
g.drawPolygon(p4);
g.setColor(Color.RED);
g.fillPolygon(p4);
}
if(style[4]==1){
Polygon p5=new Polygon(x4,y4,5);
g.drawPolygon(p5);
g.setColor(Color.RED);
g.fillPolygon(p5);
}
if(style[3]==1){
Polygon p6=new Polygon(x5,y5,4);
g.drawPolygon(p6);
g.setColor(Color.RED);
g.fillPolygon(p6);
}
if(style[2]==1){
Polygon p7=new Polygon(x6,y6,5);
g.drawPolygon(p7);
g.setColor(Color.RED);
g.fillPolygon(p7);
}
if(style[7]==1){
g.setColor(Color.RED);
g.fillOval(x[0],y[0]+7*height/8,width/6,height/8);
}
} int[] x =new int[6];
int[] y =new int[6];
int[] x1 =new int[4];
int[] y1 =new int[4];
int[] x2=new int[5];
int[] y2 =new int[5];
int[] x3=new int[6];
int[] y3 =new int[6];
int[] x4=new int[5];
int[] y4=new int[5];
int[] x5 =new int[4];
int[] y5 =new int[4];
int[] x6 =new int[5];
int[] y6 =new int[5];
}
你把显示数字的看成一个显示框就好了,不用太多的关心它,你的操作不是集中在这个上面的。
你的操作应该是如何处理输入,然后把结果显示给别人看就可以了。
如果你想结果自己改变,可以参考一下observer这种设计模式,时间问题就不多说了!
继续努力!
下面的double number有值后就能显示。但我不知到,在main中怎样用他/
import java.awt.*;
import javax.swing.*;
import java.text.*;public class SevenDigitalDemo extends JPanel
{ Polygon p1,p2,p3,p4,p5,p6,p7;
Dimension tsize;
//private int X=0,Y=0;
private int ComWidth=232,ComHeight=139;
private int width =ComWidth/6,height = ComHeight*4/6,a,b;
String num="0";
private int style[]=new int[8];
boolean []bIsShow=new boolean[8];
DecimalFormat fmt=new DecimalFormat("####.###############");
double number;
public SevenDigitalDemo()
{
this.setMinimumSize(new Dimension(ComWidth,ComHeight));
this.setPreferredSize(new Dimension(ComWidth,ComHeight));
//enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
public void setNumber(double a)
{
number=a;
}
public void getPosition(int a,int b)
{ //super.paint(g) ; x[0]=a;
y[0]=b+12;//+5*height/232;
x[1]=x[0]+26*width/139;
y[1]=y[0]+26*height/232;
x[2]=x[1];
y[2]=y[1]+67*height/232;
x[3]=x[2]-15*width/139;
y[3]=y[2]+15*height/232;
x[4]=a;
y[4]=y[0]+96*height/232;
p1=new Polygon(x,y,5);
//////////////////////////////////////////////
x1[0]=x[0]+5*width/139;
y1[0]=y[0]-5*height/232;
x1[1]=x1[0]+127*width/139;
y1[1]=y1[0];
x1[2]=x1[1]-26*width/139;
y1[2]=y1[1]+26*height/232;
x1[3]=x[0]+31*width/139;
y1[3]=y1[0]+26*height/232;
p2=new Polygon(x1,y1,4);
///////////////////////////////////////////////////
x2[0]=x1[2]+5*width/139;
y2[0]=y1[2]+5*height/232;
x2[1]=x2[0]+26*width/139;
y2[1]=y2[0]-26*height/232;
x2[2]=x2[1];
y2[2]=y2[1]+96*height/232;
x2[3]=x2[2]-8*width/139;
y2[3]=y[2]+15*height/232;
x2[4]=x2[0];
y2[4]=y2[0]+67*height/232;
p3=new Polygon(x2,y2,5);
//////////////////////////////////////////////////////
x3[0]=x[1];
y3[0]=y[2]+8*height/232;
x3[1]=x3[0]+86*width/139;
y3[1]=y3[0];
x3[2]=x3[1]+12*width/139;
y3[2]=y3[1]+12*height/232;
x3[3]=x3[2]-12*width/139;
y3[3]=y3[2]+12*height/232;
x3[4]=x3[3]-86*width/139;
y3[4]=y3[3];
x3[5]=x3[4]-12*width/139;
y3[5]=y3[3]-12*height/232;
p4=new Polygon(x3,y3,6);
//////////////////////////////////////////////////////////
x4[0]=x3[4];
y4[0]=y3[4]+8*height/232;
x4[1]=x3[4];
y4[1]=y4[0]+67*height/232;
x4[2]=x4[1]-26*width/139;
y4[2]=y4[1]+26*height/232;
x4[3]=x4[2];
y4[3]=y4[2]-96*height/232;
x4[4]=x4[3]+12*width/139;
y4[4]=y4[3]-11*height/232;
p5=new Polygon(x4,y4,5);
///////////////////////////////////////////////
x5[0]=x4[1]+5*width/139;;
y5[0]=y4[1]+5*height/232;
x5[1]=x5[0]+80*width/139;
y5[1]=y5[0];
x5[2]=x5[1]+26*width/139;
y5[2]=y5[1]+26*height/232;
x5[3]=x5[0]-26*width/139;
y5[3]=y5[0]+26*height/232;
p6=new Polygon(x5,y5,4);
//////////////////////////////////////////////////////
x6[0]=x2[3];
y6[0]=y2[3]+8*height/232;
x6[1]=x6[0]+10*width/139;
y6[1]=y6[0]+10*height/232;
x6[2]=x6[1];
y6[2]=y6[1]+96*height/232;
x6[3]=x6[2]-26*width/139;
y6[3]=y6[2]-26*height/232;
x6[4]=x6[3];
y6[4]=y3[4]+8*height/232;
p7=new Polygon(x6,y6,5);
} public void setBooleanNumber(char a)
{
if(a=='0')
{
bIsShow[0]=true;
bIsShow[1]=true;
bIsShow[2]=true;
bIsShow[3]=false;
bIsShow[4]=true;
bIsShow[5]=true;
bIsShow[6]=true;
bIsShow[7]=true; }
else if(a=='1')
{
bIsShow[0]=false;
bIsShow[1]=false;
bIsShow[2]=true;
bIsShow[3]=false;
bIsShow[4]=false;
bIsShow[5]=false;
bIsShow[6]=true;
bIsShow[7]=false; }
else if(a=='2')
{
bIsShow[0]=false;
bIsShow[1]=true;
bIsShow[2]=true;
bIsShow[3]=true;
bIsShow[4]=true;
bIsShow[5]=true;
bIsShow[6]=false;
bIsShow[7]=true; }
else if(a=='3')
{
bIsShow[0]=false;
bIsShow[1]=true;
bIsShow[2]=true;
bIsShow[3]=true;
bIsShow[4]=false;
bIsShow[5]=true;
bIsShow[6]=true;
bIsShow[7]=true;
}
else if(a=='4')
{
bIsShow[0]=true;
bIsShow[1]=false;
bIsShow[2]=true;
bIsShow[3]=true;
bIsShow[4]=false;
bIsShow[5]=false;
bIsShow[6]=true;
bIsShow[7]=true;
}
else if(a=='5')
{
bIsShow[0]=true;
bIsShow[1]=true;
bIsShow[2]=false;
bIsShow[3]=true;
bIsShow[4]=false;
bIsShow[5]=true;
bIsShow[6]=true;
bIsShow[7]=true;
}
else if(a=='6')
{
bIsShow[0]=true;
bIsShow[1]=true;
bIsShow[2]=false;
bIsShow[3]=true;
bIsShow[4]=true;
bIsShow[5]=true;
bIsShow[6]=true;
bIsShow[7]=true;
}
else if(a=='7')
{
bIsShow[0]=false;
bIsShow[1]=true;
bIsShow[2]=true;
bIsShow[3]=false;
bIsShow[4]=false;
bIsShow[5]=false;
bIsShow[6]=true;
bIsShow[7]=false;
}
else if(a=='8')
{
bIsShow[0]=true;
bIsShow[1]=true;
bIsShow[2]=true;
bIsShow[3]=true;
bIsShow[4]=true;
bIsShow[5]=true;
bIsShow[6]=true;
bIsShow[7]=true;
}
else if(a=='9')
{
bIsShow[0]=true;
bIsShow[1]=true;
bIsShow[2]=true;
bIsShow[3]=true;
bIsShow[4]=false;
bIsShow[5]=true;
bIsShow[6]=true;
bIsShow[7]=true;
}
else if(a=='-')
{
bIsShow[0]=false;
bIsShow[1]=false;
bIsShow[2]=false;
bIsShow[3]=true;
bIsShow[4]=false;
bIsShow[5]=false;
bIsShow[6]=false;
bIsShow[7]=false;
}
else if(a=='.')
{
bIsShow[0]=false;
bIsShow[1]=false;
bIsShow[2]=false;
bIsShow[3]=false;
bIsShow[4]=false;
bIsShow[5]=false;
bIsShow[6]=false;
bIsShow[7]=false;
}
}
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.red);
//tsize=getSize();
//g.setColor(getBackground());
//g.fillRect(0, 0, ComWidth, ComWidth);
//g.setColor(getForeground());
StringBuffer sb = new StringBuffer(new Double(number).toString());
char c;
int a = ComWidth/500;
int b = 0;
for(int i=0;i<sb.length();i++)
{
c = sb.charAt(i);
getPosition(a,b);
//sb.deleteCharAt(i);
setBooleanNumber(c);
if(bIsShow[0])
g2.fillPolygon(p1);
if(bIsShow[1])
g2.fillPolygon(p2);
if(bIsShow[2])
g2.fillPolygon(p3);
if(bIsShow[3])
g2.fillPolygon(p4);
if(bIsShow[4])
g2.fillPolygon(p5);
if(bIsShow[5])
g2.fillPolygon(p6);
if(bIsShow[6])
g2.fillPolygon(p7);
if(!bIsShow[0]&&!bIsShow[1]&&!bIsShow[2]&&!bIsShow[3]&&!bIsShow[4]&&!bIsShow[5]&&!bIsShow[6]&&!bIsShow[7])
g2.fillOval(x[0],y[0]+7*height/8,width/6,height/8);
if(c=='.')
a=a+ComWidth/40;
else
a=a+ComWidth/12;
}
}
/*
for(int i=0;i<num.length();i++)
{ width=tsize.width;
height=tsize.height;
width=tsize.width/6;
height=tsize.height*5/6;
a=tsize.width/40;
b=tsize.height/10;
CoordinateHandling (a,b);
}*/
public void setBounds(int x,int y,int width,int height)
{
this.ComWidth=width;
this.ComHeight=height;
super.setBounds(x,y,width,height);
repaint();
}public Dimension getMinimumSize()
{
return new Dimension(ComWidth,ComHeight);
}public Dimension getPreferredSize()
{
return new Dimension(ComWidth,ComHeight);
}
int[] x =new int[5];
int[] y =new int[5];
int[] x1 =new int[4];
int[] y1 =new int[4];
int[] x2=new int[5];
int[] y2 =new int[5];
int[] x3=new int[6];
int[] y3 =new int[6];
int[] x4=new int[5];
int[] y4=new int[5];
int[] x5 =new int[4];
int[] y5 =new int[4];
int[] x6 =new int[5];
int[] y6 =new int[5];
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button0)
{
panel1.setNumber(23233223);
repaint();
}
if (e.getSource()==button1)
{
panel1.setNumber(1);
repaint();
}
}
number=a;
repaint();
}
setNum之前处理一下
if(e.getSource()==button0){
//把0加入输入字符串s
panel1.setNumber(s);
}