开发者

How to draw 'biohazard' with swing

I'm practicing my swing abilities for the upcoming test, and fried gave me idea to draw biohazard sign like this :

开发者_运维百科

alt text http://img62.imageshack.us/img62/8372/lab6b.gif

I could draw the circles with Elipse2D, but then I somehow need to cut those 3 triangles. Any ideas how I can do that ?


You can use Java2D and canvas for this. The things that you may be using are, Circle and Arc. You should have three arcs with 30 degrees.

I tried using simple graphics over the frame.

Here is an attempt

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Biohazard {
    public static void main(String[] args) {
        new Biohazard();
    }

    public Biohazard() {
        JFrame frame = new JFrame("Biohazard");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MyComponent());
        frame.setSize(260, 280);
        frame.setVisible(true);
    }

    public class MyComponent extends JComponent {
        public void paint(Graphics g) {
            int height = 120;
            int width = 120;
            g.setColor(Color.yellow);
            g.fillOval(60, 60, height, width);
            g.setColor(Color.black);
            g.drawOval(60, 60, height, width);

            int swivels = 6;
            int commonx, commony, commonh, commonw;

            for(int i=0;i<swivels;i++){
                commonx = commony = 120-i*10;
                commonh = commonw = i*20;
                g.drawArc(commonx, commony, commonh, commonw, 60 , 60);
                g.drawArc(commonx, commony, commonh, commonw, 180 , 60);
                g.drawArc(commonx, commony, commonh, commonw, 300 , 60);
            }
        }
    }
}

The original one : source code can be found at http://pastebin.com/HSNFx7Gq

How to draw 'biohazard' with swing


You can use the Arc2D class to draw each line by specifying the start and extent parameters in degrees.


Maybe this is actually quite easy (I'm not sure how the Swing API handles lines). Draw lines coming out from the center to the points on the circumference of a circle, and just skip those portions for line drawing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜