skip to Main Content

I need to figure out how to draw the Jamaica 🇯🇲 flag in Java on visual studio code.

Flag of Jamaica, from Wikipedia

I cannot figure out how to draw the flag. All I know is that I must used this header:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;

public class JamaicaComponent extends JComponent {
    public void paintComponent(Graphics g) 

I have tried this code, but it doesn’t work as expected

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;

public class JamaicaComponent extends JComponent 
{
    public void paintComponent(Graphics g)
    {
        //jamaica flat
        g.setColor(Color.GREEN);
        g.fillRect(10, 10, 180, 60);
        g.setColor(Color.YELLOW);
        g.fillRect(10, 70, 180, 60);
        g.setColor(Color.BLACK);
        g.fillRect(10, 130, 180, 60);
    }
    
}

What the code renders:

enter image description here

2

Answers


  1. Fill with green. Draw two black triangles. Then two yellow lines of appropriate thickness.

    public void paintComponent(Graphics graphics) {
        Graphics2D g = (Graphics2D)graphics;
        
        int width = getWidth();
        int height = getHeight();
        
        // Fill with green
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, width, height);
        
        // 2 black triangles
        g.setColor(Color.BLACK);
        g.fillPolygon(new int[] { 0, width / 2, 0 }, new int[] { 0, height / 2, height }, 3);
        g.fillPolygon(new int[] { width, width / 2, width }, new int[] { 0, height / 2, height }, 3);
    
        // Note: you could draw the 2 triangles as a single sideways hourglass polygon:
        //g.fillPolygon(new int[] { 0, width, width, 0 }, new int[] { 0, height, 0, height }, 4);
        
        // 2 yellow lines
        int lineWidth = height / 9;  // Just a guess. Try different values.
        g.setColor(Color.YELLOW);
        g.setStroke(new BasicStroke(lineWidth));
        g.draw(new Line2D.Float(0, 0, width, height));
        g.draw(new Line2D.Float(width, 0, 0, height));
    }
    
    

    enter image description here

    Login or Signup to reply.
  2. Rectangle-only solution.

    Just draw the X and Y axes in yellow, and color the four quadrants of the plane alternatingly in black and green. With appropriate isometric projection, that is:

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JComponent;
    import javax.swing.SwingUtilities;
    import static java.lang.Math.*;
    
    public class JamaicaComponent extends JComponent 
    {
        public void paintComponent(Graphics g)
        {
            int w = 800;
            int h = 400;
            Color green = new Color(0x009B3A);
            Color black = Color.BLACK;
            Color gold = new Color(0xFED100);
            for (int r = 0; r < h; r++) {
              for (int c = 0; c < w; c++) {
                double rx = c / (double) w - 0.5;
                double ry = r / (double) h - 0.5;
                double x = (rx - ry) / sqrt(2);
                double y = (rx + ry) / sqrt(2);
                double d = min(abs(x), abs(y));
                double stripeWidth = sqrt(17) / 24 / sqrt(5);
                g.setColor(
                  d < stripeWidth ? gold : (x * y) < 0 ? green : black 
                );
                g.fillRect(c, r, 1, 1);
              }
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("Jamaica Flag");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new JamaicaComponent());
            frame.setSize(200, 200);
            frame.setVisible(true);
        }
    }
    

    generated flag image

    Certainly a good way to memorize flags.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search