Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.Toolkit;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.border.BevelBorder;
- // Please put your name here!
- public class StarsAndStripes {
- public static void drawFlag(int stars, int stripes, java.awt.Graphics g, int x, int y, int width, int height) {
- //Draw the background
- g.setColor(Color.WHITE);
- g.fillRect(x, y, width, height);
- //Draw some stripes
- int stripeHeight = height/stripes;
- g.setColor(Color.RED);
- //draw the last stripe
- int lastStripeDrawnY = 0;
- for(int i = y; i <= y + height - 2*stripeHeight; i= i + 2*stripeHeight) {
- g.fillRect(x, i, width, stripeHeight);
- lastStripeDrawnY = i;
- }
- //Draw Last stripe
- int lastStripeY = lastStripeDrawnY + 2*stripeHeight;
- int lastStripeHeight = y + height - lastStripeY;
- if(stripes%2!=0) {
- g.fillRect(x, lastStripeY, width, lastStripeHeight);
- }
- //Draw the star field
- //First, find the number of red stripes
- int numberOfRedStripes = (int)Math.ceil(stripes/2.0);
- //then, computer the star field height
- int starFieldHeight = numberOfRedStripes * stripeHeight;
- //Next, compute the star field width
- int starFieldWidth = starFieldHeight * width / height;
- //Draw the star field
- g.setColor(Color.BLUE);
- g.fillRect(x, y, starFieldWidth, starFieldHeight);
- //Draw the stars
- g.setColor(Color.WHITE);
- for (int cols = 1;cols <= stars;cols++) {
- //cols > rows
- // AND COLS < 2* rows
- // AND cols*rows == stars
- int rows = stars/cols;
- int size = starFieldHeight/rows;
- if(cols > rows && cols < 2*rows && cols*rows == stars) {
- for(int s = 0;s < cols;s++) {
- drawStar(g,x,y,size);
- x = x + size;
- }
- }
- }
- //drawStar(g,x,y,100);
- /*for (int cols = 1;cols <= stars;cols++) {
- //cols > rows
- // AND COLS < 2* rows
- // AND cols*rows == stars
- int rows = stars/cols;
- int size = rows/starFieldHeight;
- if(cols>rows && cols < 2*rows && cols*rows == stars) {
- for(int s = 0;s<= stars;s++) {
- drawStar(g,x,y,size);
- }
- }
- }
- */
- }
- public static void drawStar(java.awt.Graphics g, int x, int y, int size) {
- //x + 1/5 size
- int x1 = x + size/5;
- //y + size
- int y1 = y + size;
- //x + size
- int x2 = x + size;
- //y + 2/5*size
- int y2 = y + 2*size/5;
- //x
- int x3 = x;
- //y + 2/5*size
- int y3 = y + 2*size/5;
- //x + 4/5*size
- int x4 = x + 4*size/5;
- //y+size
- int y4 = y+size;
- //x + 1/2*size
- int x5 = x + size/2;
- //y
- int y5 = y;
- g.drawLine(x1,y1,x2,y2);
- g.drawLine(x2,y2,x3,y3);
- g.drawLine(x3,y3,x4,y4);
- g.drawLine(x4,y4,x5,y5);
- g.drawLine(x5,y5,x1,y1);
- //g.drawLine(x, y, x+size, y);
- //g.drawLine(x + size, y, x + size, y + size);
- //g.drawLine(x + size, y + size, x, y + size);
- //g.drawLine(x, y + size, x, y);
- }
- // Only alter the "drawFlag" part of the paintComponent
- // code to call it in different ways. You can also test
- // drawing multiple flags at once!
- public static void main(String[] args) {
- JFrame window = new JFrame("Graphics window");
- window.setLocationByPlatform(true);
- final JLabel coords = new JLabel(" ");
- @SuppressWarnings("serial")
- final JPanel panel = new JPanel() {
- protected void paintComponent(Graphics gx) {
- coords.setText(" ");
- Graphics2D g = (Graphics2D) gx;
- int width = getWidth();
- int height = getHeight();
- //System.out.println(height);
- g.setBackground(Color.BLACK); // To make sure you cover the base rectangle!
- g.clearRect(0, 0, width, height);
- g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g.setColor(Color.BLACK);
- // You could alter this code to try different flags!
- drawFlag(15, 13, g, 0, 0, width/2, height/2);
- drawFlag(20, 14, g, width/2, 0, width/2, height/2);
- drawFlag(24, 15, g, 0, height/2, width/2, height/2);
- drawFlag(48, 16, g, width/2, height/2, width/2, height/2);
- }
- };
- panel.addMouseMotionListener(new MouseMotionListener() {
- @Override
- public void mouseDragged(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- coords.setText(e.getX()+", "+e.getY());
- }
- });
- window.setLayout(new BorderLayout());
- Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
- window.setSize(d.width / 2, d.height / 2);
- JPanel coordPanel = new JPanel();
- coordPanel.setLayout(new BorderLayout());
- coordPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
- window.add(coordPanel, BorderLayout.SOUTH);
- coordPanel.add(coords, BorderLayout.WEST);
- window.setBackground(Color.WHITE); // To make sure you cover the base rectangle!
- panel.setBackground(Color.BLACK);
- window.add(panel, BorderLayout.CENTER);
- //window.setContentPane(panel);
- window.setVisible(true);
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement