Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Start from the given class, and create a NewPanel class to draw a figure like be

ID: 664002 • Letter: S

Question

Start from the given class, and create a NewPanel class to draw a figure like below: (Need about a dozen lines)
import javax.swing.*;
import
java.awt.Graphics;
import java.awt.Color;

public class drawFlag extends
JFrame {
public
drawFlag() {
    add(new
NewPanel());
}

public static void
main(String[] args) {
    drawFlag frame = new drawFlag();
    frame.setTitle("Flag");
    frame.setSize(200, 200);
    frame.setLocationRelativeTo(null); // Center the frame   
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);    
}
}

Explanation / Answer

I would use inheritance.

Create an abstract Flag class something like this.

I would use inheritance.

Create an abstract Flag class something like this.

  package com.ggl.flag.model;    import java.awt.Dimension;  import java.awt.Graphics;    public abstract class Flag {        protected Dimension dimension;        protected String name;        public Flag(String name) {          this.name = name;      }        public void setDimension(Dimension dimension) {          this.dimension = dimension;      }        public void setDimension(int width, int height) {          this.dimension = new Dimension(width, height);      }        public Dimension getDimension() {          return dimension;      }        public String getName() {          return name;      }        public abstract void draw(Graphics g);    }  
  package com.ggl.flag.model;    import java.awt.Color;  import java.awt.Graphics;    public class ItalianFlag extends Flag {        private Color   green;      private Color   white;      private Color   red;        public ItalianFlag() {          super("Italian");          // Use new Color(r, g, b) for a more precise color match          this.green = Color.GREEN;          this.white = Color.WHITE;          this.red = Color.RED;      }        @Override      public void draw(Graphics g) {          // Flag dimensions are 3 x 2          int colorWidth = dimension.width / 3;          g.setColor(green);          g.fillRect(0, 0, colorWidth, dimension.height);          g.setColor(white);          g.fillRect(colorWidth, 0, colorWidth + colorWidth, dimension.height);          g.setColor(red);          g.fillRect(colorWidth + colorWidth, 0, colorWidth + colorWidth                  + colorWidth, dimension.height);      }    }  
  package com.ggl.flag.view;    import java.awt.Graphics;    import javax.swing.JPanel;    import com.ggl.flag.model.Flag;    public class DrawingPanel extends JPanel {      private static final long   serialVersionUID    = -1489106965551164891L;        private Flag flag;        public DrawingPanel(Flag flag) {          this.flag = flag;          this.setPreferredSize(flag.getDimension());      }        @Override      protected void paintComponent(Graphics g) {          super.paintComponent(g);          flag.draw(g);      }    }  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote