Select Menu

Combined Post

Mag Posts

Theme images by konradlew. Powered by Blogger.

Blogger templates

Blogger news

Facebook

Advertising

Blogroll

Popular Posts

Combined Posts 2

Mag Post 2

2 Column Post

Simple Post

Simple Post 2

New Carousel

Video Posts

» » Create a DO NOT ENTER Sign using java!


SLVIKI 6:15 PM 0


This is a simple graphical user interface program that display some colourful circles and rectangles as a don not enter sign. Here AWT and SWING java libraries are used to create the layout of the program and colourful shapes inside the layout.


Here is the code for the program



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * A panel maintaining a picture of a do not enter sign.
 */
 public class DoNotEnterSign extends JPanel {

    /**
     * Called by the runtime system whenever the panel needs painting.
     * TODO: This code has too many literals sprinkled about.
     */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Point center = new Point(getWidth() / 2, getHeight() / 2);
        int radius = Math.min(getWidth() / 2, getHeight() / 2) - 5;
        int innerRadius = (int)(radius * 0.9);

        g.setColor(Color.WHITE);
        g.fillOval(center.x - radius, center.y - radius,
                   radius * 2, radius * 2);
        g.setColor(Color.RED);
        g.fillOval(center.x - innerRadius, center.y - innerRadius,
                   innerRadius * 2, innerRadius * 2);
        g.setColor(Color.WHITE);
        int barWidth = (int)(innerRadius * 1.4);
        int barHeight = (int)(innerRadius * 0.35);
        g.fillRect(center.x - barWidth/2, center.y - barHeight/2,
                   barWidth, barHeight);
    }

    /**
     * A tester method that embeds the panel in a frame so you can "run"
     * it as an application.  Because we have nothing better to do, we
     * show off a bit of color manipulation.
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame("A simple graphics program");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new DoNotEnterSign();
        panel.setBackground(Color.GREEN.darker());
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}


Here is the output of the above program





«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply