简体   繁体   中英

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

在此输入图像描述

您可以使用Arc2D类通过以度为单位指定startextent参数来绘制每一行。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM