想画七段码,但是不知道具体用什么函数?求解

解决方案 »

  1.   

    用Graphics2D自己画罗……
    楼主想画华丽点的,
    还是想画个简单点的
      

  2.   

    写了个例子给楼主参考下
    import java.awt.Graphics;import javax.swing.JFrame;
    import javax.swing.JPanel;public class SevenLight extends JFrame {

    private MyPanel pnlMain;

    public SevenLight() {
    super();
    initialize();
    }

    private void initialize() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(400, 300);
    this.setLocationRelativeTo(null);
    pnlMain = new MyPanel();
    this.getContentPane().add(pnlMain);
    }

    public static void drawSeven(Graphics g, int x, int y, int num) {
    switch (num) {
    case 1:
    g.drawRoundRect(x+36, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+36, y+42, 8, 32, 8, 8);
    break;
    case 2:
    g.drawRoundRect(x+6, y, 32, 8, 8, 8);
    g.drawRoundRect(x+36, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+36, 32, 8, 8, 8);
    g.drawRoundRect(x, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+72, 32, 8, 8, 8);
    break;
    case 3:
    g.drawRoundRect(x+6, y, 32, 8, 8, 8);
    g.drawRoundRect(x+36, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+36, 32, 8, 8, 8);
    g.drawRoundRect(x+36, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+72, 32, 8, 8, 8);
    break;
    case 4:
    g.drawRoundRect(x, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+36, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+36, 32, 8, 8, 8);
    g.drawRoundRect(x+36, y+42, 8, 32, 8, 8);
    break;
    case 5:
    g.drawRoundRect(x+6, y, 32, 8, 8, 8);
    g.drawRoundRect(x, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+36, 32, 8, 8, 8);
    g.drawRoundRect(x+36, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+72, 32, 8, 8, 8);
    break;
    case 6:
    g.drawRoundRect(x+6, y, 32, 8, 8, 8);
    g.drawRoundRect(x, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+36, 32, 8, 8, 8);
    g.drawRoundRect(x, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+36, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+72, 32, 8, 8, 8);
    break;
    case 7:
    g.drawRoundRect(x+6, y, 32, 8, 8, 8);
    g.drawRoundRect(x+36, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+36, y+42, 8, 32, 8, 8);
    break;
    case 8:
    g.drawRoundRect(x+6, y, 32, 8, 8, 8);
    g.drawRoundRect(x, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+36, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+36, 32, 8, 8, 8);
    g.drawRoundRect(x, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+36, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+72, 32, 8, 8, 8);
    break;
    case 9:
    g.drawRoundRect(x+6, y, 32, 8, 8, 8);
    g.drawRoundRect(x, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+36, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+36, 32, 8, 8, 8);
    g.drawRoundRect(x+36, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+72, 32, 8, 8, 8);
    break;
    case 0:
    default:
    g.drawRoundRect(x+6, y, 32, 8, 8, 8);
    g.drawRoundRect(x, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x+36, y+6, 8, 32, 8, 8);
    g.drawRoundRect(x, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+36, y+42, 8, 32, 8, 8);
    g.drawRoundRect(x+6, y+72, 32, 8, 8, 8);
    break;
    }
    }

    class MyPanel extends JPanel {
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    drawSeven(g, 1, 1, 0);
    drawSeven(g, 51, 1, 1);
    drawSeven(g, 101, 1, 2);
    drawSeven(g, 151, 1, 3);
    drawSeven(g, 201, 1, 4);
    drawSeven(g, 1, 101, 5);
    drawSeven(g, 51, 101, 6);
    drawSeven(g, 101, 101, 7);
    drawSeven(g, 151, 101, 8);
    drawSeven(g, 201, 101, 9);
    }
    }

    public static void main(String[] args) {
    SevenLight frame = new SevenLight();
    frame.setVisible(true);
    }
    }