Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. package asd;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.imageio.ImageIO;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.math.*;
  11.  
  12. public class Transforms2D extends JPanel {
  13.  
  14.     private class Display extends JPanel {
  15.         protected void paintComponent(Graphics g) {
  16.             super.paintComponent(g);
  17.             Graphics2D g2 = (Graphics2D) g;
  18.             g2.translate(300, 300);  // Moves (0,0) to the center of the display.
  19.             int whichTransform = transformSelect.getSelectedIndex();
  20.  
  21.  
  22. //          // TODO Apply transforms here, depending on the value of whichTransform!
  23. //          int[] xpoints = new int[5];
  24. //          int[] ypoints = new int[5];
  25. //
  26. //          for(int i = 1 ; i<=5;i++)
  27. //          {
  28. //              xpoints[i-1]= (int) (150*Math.sin((2*Math.PI/5)*i));
  29. //          }
  30. //
  31. //          for(int i = 1 ; i<=5;i++)
  32. //          {
  33. //              ypoints[i-1]= (int) (150*Math.cos((2*Math.PI/5)*i));
  34. //          }
  35. //          Polygon polygon = new Polygon(xpoints,ypoints,5);
  36. //
  37. //          switch(whichTransform)
  38. //          {
  39. //              case 0:
  40. //              g2.drawPolygon(polygon);
  41. //              break;
  42. //          case 1:
  43. //              g2.scale(0.5,0.5);
  44. //              g2.drawPolygon(polygon);
  45. //             break;
  46. //          case 2:
  47. //              g2.rotate(Math.toRadians(45));
  48. //              g2.drawPolygon(polygon);
  49. //              break;
  50. //              case 3:
  51. //
  52. //                  int[] bufor = xpoints;
  53. //                  xpoints = ypoints;
  54. //                  ypoints = bufor;
  55. //                  polygon = new Polygon(xpoints,ypoints,5);
  56. //                  g2.drawPolygon(polygon);
  57. //                  break;
  58. //          }
  59.  
  60.             switch (whichTransform) {
  61.                 case 0:
  62.                     g2.drawImage(pic, -200, -150, null);
  63.                     break;
  64.                 case 1:
  65.                     g2.scale(0.5, 0.5);
  66.                     g2.drawImage(pic, -200, -150, null);
  67.                     break;
  68.                 case 2:
  69.                     g2.rotate(Math.toRadians(45));
  70.                     g2.drawImage(pic, -200, -150, null);
  71.                     break;
  72.                 case 3:
  73.                     g2.scale(0.5, -1.0);
  74.                     g2.drawImage(pic, -200, -150, null);
  75.                     break;
  76.                 case 4:
  77.                     g2.shear(0,2.0);
  78.                     g2.drawImage(pic, -200, -150, null);
  79.                     break;
  80.             }
  81.  
  82.  
  83.         }
  84.     }
  85.  
  86.     private Display display;
  87.     private BufferedImage pic;
  88.     private JComboBox<String> transformSelect;
  89.  
  90.     public Transforms2D() throws IOException {
  91.         pic = ImageIO.read(new File("shuttle.jpg"));
  92.         display = new Display();
  93.         display.setBackground(Color.YELLOW);
  94.         display.setPreferredSize(new Dimension(600, 600));
  95.         transformSelect = new JComboBox<String>();
  96.         transformSelect.addItem("None");
  97.         for (int i = 1; i < 10; i++) {
  98.             transformSelect.addItem("No. " + i);
  99.         }
  100.         transformSelect.addActionListener(new ActionListener() {
  101.             public void actionPerformed(ActionEvent e) {
  102.                 display.repaint();
  103.             }
  104.         });
  105.         setLayout(new BorderLayout(3, 3));
  106.         setBackground(Color.GRAY);
  107.         setBorder(BorderFactory.createLineBorder(Color.GRAY, 10));
  108.         JPanel top = new JPanel();
  109.         top.setLayout(new FlowLayout(FlowLayout.CENTER));
  110.         top.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
  111.         top.add(new JLabel("Transform: "));
  112.         top.add(transformSelect);
  113.         add(display, BorderLayout.CENTER);
  114.         add(top, BorderLayout.NORTH);
  115.     }
  116.  
  117.  
  118.     public static void main(String[] args) throws IOException {
  119.         JFrame window = new JFrame("2D Transforms");
  120.         window.setContentPane(new Transforms2D());
  121.         window.pack();
  122.         window.setResizable(false);
  123.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  124.         Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  125.         window.setLocation((screen.width - window.getWidth()) / 2, (screen.height - window.getHeight()) / 2);
  126.         window.setVisible(true);
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement