Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw 'biohazard' with swing

Tags:

java

shapes

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 ?

like image 667
mike_hornbeck Avatar asked Apr 25 '10 21:04

mike_hornbeck


3 Answers

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

enter image description here

like image 141
bragboy Avatar answered Nov 10 '22 06:11

bragboy


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

like image 35
SLaks Avatar answered Nov 10 '22 05:11

SLaks


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.

like image 1
Chris Dennett Avatar answered Nov 10 '22 07:11

Chris Dennett