Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.39 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.BufferedImage;
  4.  
  5. import javax.swing.*;
  6.  
  7.  
  8. public class SelbstBrot implements ActionListener, MouseListener, MouseMotionListener, MouseWheelListener{
  9.    
  10.     private final int DRAW_BROT = 0;
  11.     private final int DRAG_SQUARE = 1;
  12.     private final int ZOOM_IN = 2;
  13.     private final int ZOOM_OUT = 3;
  14.     private int modus = DRAW_BROT;
  15.     public final int WINDOW_SIZE = 500;
  16.     private int mousePressedX = 0;
  17.     private int mousePressedY = 0;
  18.     private int mouseCurrentX = 0;
  19.     private int mouseCurrentY = 0;
  20.     BrotZeichnen b;
  21.     JFrame frame;
  22.     Container brotContainer;
  23.    
  24.    
  25.    
  26.     public SelbstBrot()
  27.     {
  28.         b = new BrotZeichnen();
  29.        
  30.         frame = new JFrame("SelbstBrot");
  31.            
  32.         brotContainer = new Container(){
  33.             public void paint(Graphics g) {
  34.                 switch (modus) {
  35.                 case DRAG_SQUARE:
  36.                     g.drawImage(b.getCurrentBrot(), 0, 0, null);
  37.                     drawSquare(g);
  38.                     break;
  39.                 case ZOOM_IN:
  40.                     drawSquare(g);
  41.                     // TODO warum wird square erst bei drawImage geschrieben?
  42.                     b.drawBread (b.getCurrentBrot());
  43.                     g.drawImage(b.getCurrentBrot(), 0, 0, null);
  44.                     break;
  45.                 case ZOOM_OUT:
  46.                     b.drawBread (b.getCurrentBrot());
  47.                     g.drawImage(b.getCurrentBrot(), 0, 0, null);
  48. //                    drawSquare(g);
  49.                     break;
  50.                 case DRAW_BROT:
  51.                     b.drawBread (b.getCurrentBrot());
  52.                     g.drawImage(b.getCurrentBrot(), 0, 0, null);
  53.                     break;
  54.                 }
  55.             }};
  56.         brotContainer.setSize(WINDOW_SIZE, WINDOW_SIZE);
  57.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  58.  
  59.         frame.setSize(500, 520);
  60.         frame.setJMenuBar(createMenuBar());
  61.         frame.add(brotContainer);
  62.         frame.setVisible(true);
  63.         brotContainer.addMouseListener(this);
  64.         brotContainer.addMouseMotionListener(this);
  65.         brotContainer.addMouseWheelListener(this);
  66.         b.setCurrentBrot(new BufferedImage(500,500, BufferedImage.TYPE_INT_RGB ));
  67.     }
  68.    
  69.    
  70.    
  71.    
  72.     /**
  73.      * Detects the drawing mode and draws a square into the specified Graphics accordingly.
  74.      * @param brot
  75.      */
  76.     private void drawSquare(Graphics brot) {
  77.         int x = 0, y = 0, sideLength = 0;
  78.         switch (modus) {
  79.             case DRAG_SQUARE:
  80.                 x = mousePressedX;
  81.                 y = mousePressedY;
  82.                 sideLength = Math.max(mouseCurrentX-mousePressedX, mouseCurrentY-mousePressedY);
  83.                 if (sideLength < 0)
  84.                     sideLength = 0;
  85.                 break;
  86.             case ZOOM_IN:
  87.                 x = mousePressedX - WINDOW_SIZE/20;
  88.                 y = mousePressedY - WINDOW_SIZE/20;
  89.                 sideLength = WINDOW_SIZE/10;
  90. //                System.out.println("fail2");
  91.                 break;
  92.             case ZOOM_OUT:
  93.                 // TODO frame does not work if checkValues modifies xpixel and ypixel
  94.                 x = WINDOW_SIZE/2 - WINDOW_SIZE/20;
  95.                 y = WINDOW_SIZE/2 - WINDOW_SIZE/20;
  96.                 sideLength = WINDOW_SIZE/10;
  97.                 break;
  98.         }
  99. //        System.out.println(x);
  100.         brot.setColor(Color.white);
  101.         brot.drawRect(x, y, sideLength, sideLength);
  102.     }
  103.    
  104.    
  105.    
  106.     @Override
  107.     public void mousePressed(MouseEvent e) {
  108.         mousePressedX = e.getX();
  109.         mousePressedY = e.getY();
  110.     }
  111.    
  112.    
  113.    
  114.     @Override
  115.     public void mouseReleased(MouseEvent e) {
  116.         modus = DRAW_BROT;
  117.         switch (e.getButton()) {
  118.             case MouseEvent.BUTTON1: //left mouse button: square dragged, now zoom
  119.                 int xabstand = e.getX() - mousePressedX;
  120.                 if (xabstand > 0) {
  121.                    
  122.                     b.setXpixel(b.getXpixel() + mousePressedX*b.getPixelsize());
  123.                     b.setYpixel(b.getYpixel() + mousePressedY*b.getPixelsize());
  124.                    
  125.                     double verhaeltnis = WINDOW_SIZE / xabstand;
  126.                     b.setPixelsize(b.getPixelsize()/verhaeltnis);
  127.                     brotContainer.repaint();
  128.                 }
  129.                 break;
  130.             case MouseEvent.BUTTON2: //double-click with mouse wheel: reset
  131.                 if (e.getClickCount() == 2) {
  132.                     resetValuesAndRepaint();
  133.                 }
  134.                 break;
  135.             case MouseEvent.BUTTON3: //right mouse button dragged: drag
  136.                 int xAbstand = mousePressedX-e.getX();
  137.                 int yAbstand = mousePressedY-e.getY();
  138.                 if (xAbstand != 0 || yAbstand != 0) {
  139.                     b.setXpixel (b.getXpixel() + xAbstand*b.getPixelsize());
  140.                     b.setYpixel (b.getYpixel() + yAbstand*b.getPixelsize());
  141.                     brotContainer.repaint();
  142.                 }
  143.                 break;
  144.         }
  145.     }
  146.    
  147.    
  148.    
  149.     @Override
  150.     public void mouseWheelMoved(MouseWheelEvent e) {
  151.         mousePressedX = e.getX();
  152.         mousePressedY = e.getY();
  153.         double neueMittex = b.getXpixel() + mousePressedX * b.getPixelsize();
  154.         double neueMittey = b.getYpixel() + mousePressedY * b.getPixelsize();
  155.  
  156.         if (e.getWheelRotation() < 0) {
  157.             b.setPixelsize(b.getPixelsize()/10);
  158.             modus = ZOOM_IN;
  159.         }
  160.         else {
  161.             b.setPixelsize(b.getPixelsize()*10);
  162.             modus = ZOOM_OUT;
  163.         }
  164.        
  165.         b.setXpixel(neueMittex - WINDOW_SIZE/2 * b.getPixelsize());
  166.         b.setYpixel(neueMittey - WINDOW_SIZE/2 * b.getPixelsize());
  167.         checkValues();
  168.         brotContainer.repaint();
  169.     }
  170.    
  171.    
  172.    
  173.     @Override
  174.     public void mouseDragged(MouseEvent e) {
  175.         if (e.getModifiersEx() == InputEvent.BUTTON1_DOWN_MASK) {
  176.             mouseCurrentX = e.getX();
  177.             mouseCurrentY = e.getY();
  178.             modus = DRAG_SQUARE;
  179.             brotContainer.repaint();
  180.         }
  181.     }
  182.    
  183.    
  184.    
  185.     @Override
  186.     public void actionPerformed(ActionEvent e) {
  187.         modus = DRAW_BROT;
  188.         String command = e.getActionCommand();
  189.         if (command.equals("red")) {
  190.             b.setColorDivisors(1, 4, 4);
  191.             brotContainer.repaint();
  192.         }
  193.         else if (command.equals("blue")) {
  194.             b.setColorDivisors(4, 2, 1);
  195.             brotContainer.repaint();
  196.         }
  197.         else if (command.equals("green")) {
  198.             b.setColorDivisors(4, 1, 2);
  199.             brotContainer.repaint();
  200.         }
  201.         else if (command.equals("freaky")) {
  202.             b.setColorDivisors(0.1, 0.2, 0.3);
  203.             brotContainer.repaint();
  204.         }
  205.         else if (command.equals("custom")) {
  206.             final JDialog dialog = new JDialog(frame, "Coustom color", true);
  207.             dialog.setLayout(new GridLayout(4, 2));
  208.             dialog.setLocationRelativeTo(brotContainer);
  209.             Label redLabel = new Label("Red:");
  210.             Label greenLabel = new Label("Green:");
  211.             Label blueLabel = new Label("Blue:");
  212.             final TextField redField = new TextField(3);
  213.             final TextField greenField = new TextField(3);
  214.             final TextField blueField = new TextField(3);
  215.            
  216.             JButton ok = new JButton("ok");
  217.             ok.addActionListener(new ActionListener() {
  218.                 public void actionPerformed(ActionEvent arg0) {
  219.                     try {b.setColorDivisors(Double.valueOf(redField.getText()),
  220.                     Double.valueOf(greenField.getText()),
  221.                     Double.valueOf(blueField.getText()));}
  222.                     catch (Exception ex) {}
  223.                    
  224.                     dialog.setVisible(false);
  225.                     paint();
  226.                 }});
  227.             JButton cancel = new JButton("cancel");
  228.             cancel.addActionListener(new ActionListener() {
  229.                 public void actionPerformed(ActionEvent arg0) {
  230.                     dialog.setVisible(false);
  231.                 }
  232.             });
  233.             dialog.add(redLabel);
  234.             dialog.add(redField);
  235.             dialog.add(greenLabel);
  236.             dialog.add(greenField);
  237.             dialog.add(blueLabel);
  238.             dialog.add(blueField);
  239.             dialog.add(ok);
  240.             dialog.add(cancel);
  241.             dialog.pack();
  242.             dialog.setVisible(true);
  243.         }
  244.         else if (command.equals("Get coordinates")) {
  245.             System.out.println(b.getXpixel());
  246.             System.out.println(b.getYpixel());
  247.             System.out.println(b.getPixelsize());
  248.             brotContainer.repaint();
  249.         }
  250.     }
  251.    
  252.    
  253.    
  254.     public void checkValues() {
  255.         if (b.getXpixel() < -2.0)  b.setXpixel(-2.0);
  256.         if (b.getYpixel() < -1.5)  b.setYpixel(-1.5);
  257.         if (b.getPixelsize() > 0.00625) b.setPixelsize(0.00625);
  258.         if (b.getXpixel()+WINDOW_SIZE*b.getPixelsize() > 1.2)b.setXpixel(1.2 - WINDOW_SIZE*b.getPixelsize());
  259.         if (b.getYpixel()+WINDOW_SIZE*b.getPixelsize() > 1.7)b.setYpixel(1.7 - WINDOW_SIZE*b.getPixelsize());
  260.     }
  261.    
  262.     public void paint() {
  263.         brotContainer.repaint();
  264.     }
  265.    
  266.     public void resetValuesAndRepaint() {
  267.         b.setXpixel(-2.0);
  268.         b.setYpixel(-1.5);
  269.         b.setPixelsize(0.00625);
  270.         brotContainer.repaint();
  271.     }
  272.    
  273.    
  274.    
  275.     public JMenuBar createMenuBar() {
  276.         JMenuBar menuBar = new JMenuBar();
  277.         JMenu farbe = new JMenu("Farbe");
  278.         menuBar.add(farbe);
  279.        
  280.         ButtonGroup group = new ButtonGroup();
  281.         JRadioButtonMenuItem yellowButton = new JRadioButtonMenuItem("yellow");
  282.         yellowButton.setSelected(true);
  283.         JRadioButtonMenuItem redButton = new JRadioButtonMenuItem("red");
  284.         JRadioButtonMenuItem blueButton = new JRadioButtonMenuItem("blue");
  285.         JRadioButtonMenuItem greenButton = new JRadioButtonMenuItem("green");
  286.         JRadioButtonMenuItem freakyButton = new JRadioButtonMenuItem("freaky");
  287.         JRadioButtonMenuItem customButton = new JRadioButtonMenuItem("custom");
  288.         group.add(yellowButton);
  289.         group.add(redButton);
  290.         group.add(blueButton);
  291.         group.add(greenButton);
  292.         group.add(freakyButton);
  293.         group.add(customButton);
  294.        
  295.         yellowButton.addActionListener(this);
  296.         redButton.addActionListener(this);
  297.         blueButton.addActionListener(this);
  298.         greenButton.addActionListener(this);
  299.         freakyButton.addActionListener(this);
  300.         customButton.addActionListener(this);
  301.        
  302.         farbe.add(yellowButton);
  303.         farbe.add(redButton);
  304.         farbe.add(blueButton);
  305.         farbe.add(greenButton);
  306.         farbe.add(freakyButton);
  307.         farbe.add(customButton);
  308.        
  309.         JMenuItem getCoordinates = new JMenuItem("Get coordinates");
  310.         menuBar.add(getCoordinates);
  311.         getCoordinates.addActionListener(this);
  312.        
  313.        
  314.         return menuBar;
  315.     }
  316.    
  317.    
  318.    
  319.     public void mouseEntered(MouseEvent e) {}
  320.     public void mouseExited(MouseEvent e) {}
  321.     public void mouseClicked(MouseEvent e) {}
  322.     public void mouseMoved(MouseEvent e) {}
  323.  
  324.    
  325.     public static void main (String args[])
  326.     {
  327.         new SelbstBrot();
  328.     }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement