Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.17 KB | None | 0 0
  1. package code.renderer;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.KeyEventDispatcher;
  9. import java.awt.KeyboardFocusManager;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.KeyEvent;
  13. import java.awt.image.BufferedImage;
  14. import java.io.File;
  15.  
  16. import javax.swing.BorderFactory;
  17. import javax.swing.Box;
  18. import javax.swing.BoxLayout;
  19. import javax.swing.JButton;
  20. import javax.swing.JComponent;
  21. import javax.swing.JFileChooser;
  22. import javax.swing.JFrame;
  23. import javax.swing.JPanel;
  24. import javax.swing.JSlider;
  25. import javax.swing.border.Border;
  26.  
  27. /**
  28.  * A simple GUI, similar to the one in assignments 1 and 2, that you can base
  29.  * your renderer off. It is abstract, and there are three methods you need to
  30.  * implement: onLoad, onKeyPress, and render. There is a method to get the
  31.  * ambient light level set by the sliders. You are free to use this class as-is,
  32.  * modify it, or ignore it completely.
  33.  *
  34.  * @author tony
  35.  */
  36. public abstract class GUI {
  37.  
  38.     /**
  39.      * Is called when the user has successfully selected a model file to load,
  40.      * and is passed a File representing that file.
  41.      */
  42.     protected abstract void onLoad(File file);
  43.  
  44.     /**
  45.      * Is called every time the user presses a key. This can be used for moving
  46.      * the camera around. It is passed a KeyEvent object, whose methods of
  47.      * interest are getKeyChar() and getKeyCode().
  48.      */
  49.     protected abstract void onKeyPress(KeyEvent ev);
  50.  
  51.     /**
  52.      * Is called every time the drawing canvas is drawn. This should return a
  53.      * BufferedImage that is your render of the scene.
  54.      */
  55.     protected abstract BufferedImage render();
  56.  
  57.     /**
  58.      * Forces a redraw of the drawing canvas. This is called for you, so you
  59.      * don't need to call this unless you modify this GUI.
  60.      */
  61.     public void redraw() {
  62.         frame.repaint();
  63.     }
  64.  
  65.     /**
  66.      * Returns the values of the three sliders used for setting the ambient
  67.      * light of the scene. The returned array in the form [R, G, B] where each
  68.      * value is between 0 and 255.
  69.      */
  70.     public int[] getAmbientLight() {
  71.         return new int[] { red.getValue(), green.getValue(), blue.getValue() };
  72.     }
  73.  
  74.     public static final int CANVAS_WIDTH = 600;
  75.     public static final int CANVAS_HEIGHT = 600;
  76.  
  77.     // --------------------------------------------------------------------
  78.     // Everything below here is Swing-related and, while it's worth
  79.     // understanding, you don't need to look any further to finish the
  80.     // assignment up to and including completion.
  81.     // --------------------------------------------------------------------
  82.  
  83.     private JFrame frame;
  84.     private final JSlider red = new JSlider(JSlider.HORIZONTAL, 0, 255, 128);
  85.     private final JSlider green = new JSlider(JSlider.HORIZONTAL, 0, 255, 128);
  86.     private final JSlider blue = new JSlider(JSlider.HORIZONTAL, 0, 255, 128);
  87.  
  88.     private static final Dimension DRAWING_SIZE = new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT);
  89.     private static final Dimension CONTROLS_SIZE = new Dimension(150, 600);
  90.  
  91.     private static final Font FONT = new Font("Courier", Font.BOLD, 36);
  92.  
  93.     public GUI() {
  94.         initialise();
  95.     }
  96.  
  97.     @SuppressWarnings("serial")
  98.     private void initialise() {
  99.         // make the frame
  100.         frame = new JFrame();
  101.         frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS));
  102.         frame.setSize(new Dimension(DRAWING_SIZE.width + CONTROLS_SIZE.width, DRAWING_SIZE.height));
  103.         frame.setResizable(false);
  104.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  105.  
  106.         // set up the drawing canvas, hook it into the render() method, and give
  107.         // it a nice default if render() returns null.
  108.         JComponent drawing = new JComponent() {
  109.             protected void paintComponent(Graphics g) {
  110.                 BufferedImage image = render();
  111.                 if (image == null) {
  112.                     g.setColor(Color.WHITE);
  113.                     g.fillRect(0, 0, DRAWING_SIZE.width, DRAWING_SIZE.height);
  114.                     g.setColor(Color.BLACK);
  115.                     g.setFont(FONT);
  116.                     g.drawString("IMAGE IS NULL", 50, DRAWING_SIZE.height - 50);
  117.                 } else {
  118.                     g.drawImage(image, 0, 0, null);
  119.                 }
  120.             }
  121.         };
  122.         // fix its size
  123.         drawing.setPreferredSize(DRAWING_SIZE);
  124.         drawing.setMinimumSize(DRAWING_SIZE);
  125.         drawing.setMaximumSize(DRAWING_SIZE);
  126.         drawing.setVisible(true);
  127.  
  128.         // set up the load button
  129.         final JFileChooser fileChooser = new JFileChooser();
  130.         JButton load = new JButton("Load");
  131.         load.addActionListener(new ActionListener() {
  132.             public void actionPerformed(ActionEvent ev) {
  133.                 // set up the file chooser
  134.                 fileChooser.setCurrentDirectory(new File("."));
  135.                 fileChooser.setDialogTitle("Select input file");
  136.                 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  137.  
  138.                 // run the file chooser and check the user didn't hit cancel
  139.                 if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
  140.                     File file = fileChooser.getSelectedFile();
  141.                     onLoad(file);
  142.                     redraw();
  143.                 }
  144.             }
  145.         });
  146.         // we have to put the button in its own panel to ensure it fills the
  147.         // full width of the control bar.
  148.         JPanel loadpanel = new JPanel(new BorderLayout());
  149.         loadpanel.setMaximumSize(new Dimension(1000, 25));
  150.         loadpanel.setPreferredSize(new Dimension(1000, 25));
  151.         loadpanel.add(load, BorderLayout.CENTER);
  152.  
  153.         // set up the sliders for ambient light. they were instantiated in
  154.         // the field definition, as for some reason they need to be final to
  155.         // pull the set background trick.
  156.         red.setBackground(new Color(230, 50, 50));
  157.         green.setBackground(new Color(50, 230, 50));
  158.         blue.setBackground(new Color(50, 50, 230));
  159.  
  160.         JPanel sliderparty = new JPanel();
  161.         sliderparty.setLayout(new BoxLayout(sliderparty, BoxLayout.PAGE_AXIS));
  162.         sliderparty.setBorder(BorderFactory.createTitledBorder("Ambient Light"));
  163.  
  164.         sliderparty.add(red);
  165.         sliderparty.add(green);
  166.         sliderparty.add(blue);
  167.  
  168.         // this is not a best-practices way of doing key listening; instead you
  169.         // should use either a KeyListener or an InputMap/ActionMap combo. but
  170.         // this method neatly avoids any focus issues (KeyListener) and requires
  171.         // less effort on your part (ActionMap).
  172.         KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
  173.         manager.addKeyEventDispatcher(new KeyEventDispatcher() {
  174.             @Override
  175.             public boolean dispatchKeyEvent(KeyEvent ev) {
  176.                 if (ev.getID() == KeyEvent.KEY_PRESSED) {
  177.                     onKeyPress(ev);
  178.                     redraw();
  179.                 }
  180.                 return true;
  181.             }
  182.         });
  183.  
  184.         // make the panel on the right, fix its size, give it a border!
  185.         JPanel controls = new JPanel();
  186.         controls.setPreferredSize(CONTROLS_SIZE);
  187.         controls.setMinimumSize(CONTROLS_SIZE);
  188.         controls.setMaximumSize(CONTROLS_SIZE);
  189.         controls.setLayout(new BoxLayout(controls, BoxLayout.PAGE_AXIS));
  190.         Border edge = BorderFactory.createEmptyBorder(5, 5, 5, 5);
  191.         controls.setBorder(edge);
  192.  
  193.         controls.add(loadpanel);
  194.         controls.add(Box.createRigidArea(new Dimension(0, 15)));
  195.         controls.add(sliderparty);
  196.         // if i were going to add more GUI components, i'd do it here.
  197.         controls.add(Box.createVerticalGlue());
  198.  
  199.         // put it all together.
  200.         frame.add(drawing);
  201.         frame.add(controls);
  202.  
  203.         frame.pack();
  204.         frame.setVisible(true);
  205.     }
  206. }
  207.  
  208. // code for comp261 assignments
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement