Advertisement
Guest User

Untitled

a guest
Feb 9th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.awt.Toolkit;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseMotionListener;
  10.  
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.border.BevelBorder;
  15.  
  16. // Please put your name here!
  17.  
  18. public class StarsAndStripes {
  19.     public static void drawFlag(int stars, int stripes, java.awt.Graphics g, int x, int y, int width, int height) {
  20.         //Draw the background
  21.         g.setColor(Color.WHITE);
  22.         g.fillRect(x, y, width, height);
  23.        
  24.         //Draw some stripes
  25.         int stripeHeight = height/stripes;
  26.         g.setColor(Color.RED);
  27.         //draw the last stripe
  28.         int lastStripeDrawnY = 0;
  29.         for(int i = y; i <= y + height - 2*stripeHeight; i= i + 2*stripeHeight) {
  30.             g.fillRect(x, i, width, stripeHeight);
  31.             lastStripeDrawnY = i;
  32.         }
  33.         //Draw Last stripe 
  34.         int lastStripeY = lastStripeDrawnY + 2*stripeHeight;
  35.         int lastStripeHeight = y + height - lastStripeY;
  36.         if(stripes%2!=0) {
  37.         g.fillRect(x, lastStripeY, width, lastStripeHeight);
  38.         }
  39.        
  40.         //Draw the star field
  41.         //First, find the number of red stripes
  42.         int numberOfRedStripes = (int)Math.ceil(stripes/2.0);
  43.         //then, computer the star field height
  44.         int starFieldHeight = numberOfRedStripes * stripeHeight;
  45.         //Next, compute the star field width
  46.         int starFieldWidth = starFieldHeight * width / height;
  47.         //Draw the star field
  48.         g.setColor(Color.BLUE);
  49.         g.fillRect(x, y, starFieldWidth, starFieldHeight);
  50.    
  51.         //Draw the stars
  52.         g.setColor(Color.WHITE);
  53.         for (int cols = 1;cols <= stars;cols++) {
  54.             //cols > rows
  55.             // AND COLS < 2* rows
  56.             // AND cols*rows == stars  
  57.             int rows = stars/cols;
  58.             int size = starFieldHeight/rows;
  59.                 if(cols > rows && cols < 2*rows &&  cols*rows == stars) {  
  60.                     for(int s = 0;s < cols;s++) {
  61.                         drawStar(g,x,y,size);
  62.                         x = x + size;          
  63.                         }  
  64.                 }
  65.         }
  66.        
  67.         //drawStar(g,x,y,100);
  68.         /*for (int cols = 1;cols <= stars;cols++) {
  69.             //cols > rows
  70.             // AND COLS < 2* rows
  71.             // AND cols*rows == stars  
  72.             int rows = stars/cols;
  73.             int size = rows/starFieldHeight;
  74.             if(cols>rows && cols < 2*rows && cols*rows == stars) {
  75.                 for(int s = 0;s<= stars;s++) {
  76.                     drawStar(g,x,y,size);
  77.                 }
  78.        
  79.             }
  80.         }
  81.         */
  82.     }
  83.        
  84.  
  85.     public static void drawStar(java.awt.Graphics g, int x, int y, int size) {
  86.        
  87.         //x + 1/5 size
  88.         int x1 = x + size/5;
  89.         //y + size
  90.         int y1 = y + size;
  91.         //x + size
  92.         int x2 = x + size;
  93.         //y + 2/5*size
  94.         int y2 = y + 2*size/5;
  95.         //x
  96.         int x3 = x;
  97.         //y + 2/5*size
  98.         int y3 = y + 2*size/5;
  99.         //x + 4/5*size
  100.         int x4 = x + 4*size/5;
  101.         //y+size
  102.         int y4 = y+size;
  103.         //x + 1/2*size
  104.         int x5 = x + size/2;
  105.         //y
  106.         int y5 = y;
  107.         g.drawLine(x1,y1,x2,y2);
  108.         g.drawLine(x2,y2,x3,y3);
  109.         g.drawLine(x3,y3,x4,y4);
  110.         g.drawLine(x4,y4,x5,y5);
  111.         g.drawLine(x5,y5,x1,y1);
  112.         //g.drawLine(x, y, x+size, y);
  113.         //g.drawLine(x + size, y, x + size, y + size); 
  114.         //g.drawLine(x + size, y + size, x, y + size);
  115.         //g.drawLine(x, y + size, x, y);
  116.     }
  117.  
  118.     // Only alter the "drawFlag" part of the paintComponent
  119.     // code to call it in different ways. You can also test
  120.     // drawing multiple flags at once!
  121.     public static void main(String[] args) {
  122.         JFrame window = new JFrame("Graphics window");
  123.         window.setLocationByPlatform(true);
  124.         final JLabel coords = new JLabel(" ");
  125.         @SuppressWarnings("serial")
  126.         final JPanel panel = new JPanel() {
  127.  
  128.             protected void paintComponent(Graphics gx) {
  129.                 coords.setText(" ");
  130.                 Graphics2D g = (Graphics2D) gx;
  131.                 int width = getWidth();
  132.                 int height = getHeight();
  133.                 //System.out.println(height);
  134.                 g.setBackground(Color.BLACK); // To make sure you cover the base rectangle!
  135.                 g.clearRect(0, 0, width, height);
  136.                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  137.                 g.setColor(Color.BLACK);
  138.  
  139.                 // You could alter this code to try different flags!
  140.                 drawFlag(15, 13, g, 0, 0, width/2, height/2);
  141.                 drawFlag(20, 14, g, width/2, 0, width/2, height/2);
  142.                 drawFlag(24, 15, g, 0, height/2, width/2, height/2);
  143.                 drawFlag(48, 16, g, width/2, height/2, width/2, height/2);
  144.             }
  145.         };
  146.         panel.addMouseMotionListener(new MouseMotionListener() {
  147.  
  148.  
  149.             @Override
  150.             public void mouseDragged(MouseEvent e) {
  151.                 // TODO Auto-generated method stub
  152.                
  153.             }
  154.  
  155.             @Override
  156.             public void mouseMoved(MouseEvent e) {
  157.                 coords.setText(e.getX()+", "+e.getY());            
  158.             }
  159.            
  160.         });
  161.         window.setLayout(new BorderLayout());
  162.         Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  163.         window.setSize(d.width / 2, d.height / 2);
  164.  
  165.         JPanel coordPanel = new JPanel();
  166.         coordPanel.setLayout(new BorderLayout());
  167.         coordPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
  168.         window.add(coordPanel, BorderLayout.SOUTH);
  169.         coordPanel.add(coords, BorderLayout.WEST);
  170.        
  171.         window.setBackground(Color.WHITE); // To make sure you cover the base rectangle!       
  172.         panel.setBackground(Color.BLACK);
  173.         window.add(panel, BorderLayout.CENTER);
  174.         //window.setContentPane(panel);
  175.         window.setVisible(true);
  176.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement