Guest User

Untitled

a guest
Jun 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 20.39 KB | None | 0 0
  1. import java.io.*;
  2. import javax.swing.*;
  3. import javax.swing.event.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.io.*;
  7. import javax.imageio.ImageIO;
  8. import java.awt.Robot;
  9. import java.awt.AWTException;
  10. import java.awt.Rectangle;
  11. import java.awt.image.BufferedImage;
  12. import java.awt.Frame;
  13.  
  14. import java.text.SimpleDateFormat;
  15. import java.util.Date;
  16.  
  17. /**
  18.  * @author Marcel
  19.  * @version 1.0
  20.  */
  21. public class Malprogramm extends JFrame
  22. {
  23.     public PadDraw drawPad;
  24.     public ButtonGroup modusGroup;
  25.     public JMenuItem oval;
  26.     public JSlider slider;
  27.  
  28.     // Main - Methode
  29.     public static void main(String[] args){
  30.         Malprogramm malen = new Malprogramm("Pinselfix");
  31.         malen.pack();
  32.         malen.setSize(1350, 680);
  33.         malen.setResizable(false);
  34.         malen.setVisible(true);
  35.     }
  36.  
  37.     // Konstruktor
  38.     public Malprogramm(String title)
  39.     {
  40.         // Superklasse "erben"
  41.         super(title);
  42.  
  43.         drawPad = new PadDraw();
  44.         add(drawPad, BorderLayout.EAST);
  45.  
  46.         //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  47.         // Menubar erstellen
  48.         JMenuBar mbar = new JMenuBar();
  49.         setJMenuBar(mbar);
  50.  
  51.         // Menu "Datei" erstellen und dem Menubar hinzufügen
  52.         JMenu datei = new JMenu("Datei");
  53.         JMenuItem neu = new JMenuItem("Neu", new ImageIcon("neu.jpg"));
  54.         JMenuItem speichern = new JMenuItem("Speichern", new ImageIcon("speichern.jpg" ));
  55.         JMenuItem laden = new JMenuItem("Laden", new ImageIcon("laden.jpg" ));
  56.         JMenuItem beenden = new JMenuItem("Beenden", new ImageIcon("beenden.jpg" ));
  57.         mbar.add(datei);
  58.         datei.add(neu);
  59.         datei.add(speichern);
  60.         datei.add(laden);
  61.         datei.add(beenden);
  62.  
  63.         // Menu "Farben" erstellen und dem Menubar hinzufügen
  64.         JMenu farben = new JMenu("Farben");
  65.         JMenuItem gelb = new JMenuItem("Gelb");
  66.         JMenuItem blau = new JMenuItem("Blau");
  67.         JMenuItem rot = new JMenuItem("Rot");
  68.         JMenuItem schwarz = new JMenuItem("Schwarz");
  69.         JMenu mischfarben =  new JMenu("Mischfarben");
  70.         JMenuItem orange = new JMenuItem("Orange");
  71.         JMenuItem rosa = new JMenuItem("Rosa");
  72.         JMenuItem gruen = new JMenuItem("Grün");
  73.         mbar.add(farben);
  74.         farben.add(gelb);
  75.         farben.add(blau);
  76.         farben.add(rot);
  77.         farben.add(schwarz);
  78.         mischfarben.add(orange);
  79.         mischfarben.add(rosa);
  80.         mischfarben.add(gruen);
  81.         farben.add(mischfarben);
  82.  
  83.         // Menu "Formen" erstellen und dem Menubar hinzufügen
  84.         JMenu formen = new JMenu("Formen");
  85.         JMenuItem oval = new JMenuItem("Oval");
  86.         JMenuItem rechteck = new JMenuItem("Rechteck");
  87.         JMenuItem dreieck = new JMenuItem("Dreieck");
  88.         JMenu fgroesse = new JMenu("Formengröße");
  89.         JMenuItem kreisa = new JMenuItem("Kreisgröße");
  90.         JMenuItem rechtecka = new JMenuItem("Rechteckgröße");
  91.         JMenuItem dreiecka = new JMenuItem("Dreieckgröße");
  92.         formen.add(oval);
  93.         formen.add(rechteck);
  94.         formen.add(dreieck);
  95.         formen.add(fgroesse);
  96.         fgroesse.add(kreisa);
  97.         fgroesse.add(rechtecka);
  98.         fgroesse.add(dreiecka);
  99.         mbar.add(formen);
  100.  
  101.         // Menu "Text" erstellen und dem Menubar hinzufügen
  102.         JMenu text = new JMenu("Text");
  103.         JMenuItem textHinzufuegen = new JMenuItem("Text hinzufuegen");
  104.         JMenu schriftStil = new JMenu("Schriftstil");
  105.         JMenuItem schriftGroesse = new JMenuItem("Schriftgröße");
  106.         JMenuItem fett = new JMenuItem("Fett");
  107.         JMenuItem kursiv = new JMenuItem("Kursiv");
  108.         JMenuItem fettkursiv = new JMenuItem("Fett & Krusiv");
  109.         text.add(textHinzufuegen);
  110.         text.add(schriftStil);
  111.         text.add(schriftGroesse);
  112.         schriftStil.add(fett);
  113.         schriftStil.add(kursiv);
  114.         schriftStil.add(fettkursiv);
  115.         mbar.add(text);
  116.  
  117.         // Menu "Sonstiges" erstellen und Menubar hinzufügen
  118.         JMenu sonstiges = new JMenu("Sonstiges");
  119.         JMenuItem bildEinfugen = new JMenuItem("Bild einfügen");
  120.         sonstiges.add(bildEinfugen);
  121.         mbar.add(sonstiges);
  122.  
  123.         // Container erstellen
  124.         Box p1box = new Box(BoxLayout.Y_AXIS);
  125.         Box p2box = new Box(BoxLayout.Y_AXIS);
  126.         Box p3box = new Box(BoxLayout.Y_AXIS);
  127.  
  128.         // Slider erstellen
  129.         slider = new JSlider(JSlider.HORIZONTAL, 0, 25, 1);
  130.         slider.setPreferredSize(new Dimension(100, 50));
  131.         slider.setMajorTickSpacing(5);
  132.         slider.setMinorTickSpacing(1);
  133.         slider.setPaintTicks(true);
  134.         slider.setPaintLabels(true);
  135.         slider.setVisible(true);
  136.  
  137.         // Buttons erstellen
  138.         JButton breiteauswahlbox = new JButton("Breiteauswahlbox");
  139.         JButton hintergrundfarbe = new JButton("Hintergrundfarbe");
  140.  
  141.         // Radiobuttons mit Buttongroups erstellen
  142.         JRadioButton normal = new JRadioButton("Normal", true);
  143.         JRadioButton radieren = new JRadioButton("Radieren", false);
  144.         modusGroup = new ButtonGroup();
  145.         modusGroup.add(normal);
  146.         modusGroup.add(radieren);
  147.  
  148.         JRadioButton durchsichtig = new JRadioButton("Durchsichtig", true);
  149.         JRadioButton gefuellt =  new JRadioButton("Gefüllt", false);
  150.         ButtonGroup fullGroup = new ButtonGroup();
  151.         fullGroup.add(durchsichtig);
  152.         fullGroup.add(gefuellt);
  153.  
  154.         // Komponenten dem Container hinzufügen
  155.         p1box.add(Box.createRigidArea(new Dimension(10, 50)));
  156.         p1box.add(new JLabel("Linienbreite auswählen:"));
  157.         p1box.add(slider);
  158.         p1box.add(Box.createRigidArea(new Dimension(50, 20)));
  159.         p1box.add(p2box);
  160.         p2box.add(breiteauswahlbox);
  161.         p2box.add(Box.createRigidArea(new Dimension(50, 60)));
  162.         p2box.add(p3box);
  163.         p3box.add(new JLabel("Stiftmodus wählen:"));
  164.         p3box.add(normal);
  165.         p3box.add(radieren);
  166.         p3box.add(Box.createRigidArea(new Dimension(50, 30)));
  167.         p3box.add(new JLabel("Füllmuster wählen:"));
  168.         p3box.add(durchsichtig);
  169.         p3box.add(gefuellt);
  170.         p3box.add(Box.createRigidArea(new Dimension(50, 100)));
  171.         p3box.add(hintergrundfarbe);
  172.  
  173.         // Container dem Frame hinzufügen
  174.         add(p1box, BorderLayout.WEST);
  175.  
  176.         //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  177.         // Komponenten registrieren
  178.         normal.setActionCommand("Normal");
  179.         radieren.setActionCommand("Radieren");
  180.  
  181.         neu.addActionListener(new ActionListener(){
  182.                 public void actionPerformed(ActionEvent e){
  183.                     drawPad.clear();
  184.                 }
  185.             });
  186.         speichern.addActionListener(new ActionListener(){
  187.                 public void actionPerformed(ActionEvent e){
  188.                     drawPad.speichern();
  189.                 }
  190.             });
  191.         laden.addActionListener(new ActionListener(){
  192.                 public void actionPerformed(ActionEvent e){
  193.  
  194.                 }
  195.             });
  196.         beenden.addActionListener(new ActionListener(){
  197.                 public void actionPerformed(ActionEvent e){
  198.                     System.exit( 0 );
  199.                 }
  200.             });
  201.         gelb.addActionListener(new ActionListener(){
  202.                 public void actionPerformed(ActionEvent e){
  203.                     drawPad.gelb();
  204.                 }
  205.             });
  206.         blau.addActionListener(new ActionListener(){
  207.                 public void actionPerformed(ActionEvent e){
  208.                     drawPad.blau();
  209.                 }
  210.             });
  211.         rot.addActionListener(new ActionListener(){
  212.                 public void actionPerformed(ActionEvent e){
  213.                     drawPad.rot();
  214.                 }
  215.             });
  216.         schwarz.addActionListener(new ActionListener(){
  217.                 public void actionPerformed(ActionEvent e){
  218.                     drawPad.schwarz();
  219.                 }
  220.             });
  221.         orange.addActionListener(new ActionListener(){
  222.                 public void actionPerformed(ActionEvent e){
  223.                     drawPad.orange();
  224.                 }
  225.             });
  226.         rosa.addActionListener(new ActionListener(){
  227.                 public void actionPerformed(ActionEvent e){
  228.                     drawPad.rosa();
  229.                 }
  230.             });
  231.         gruen.addActionListener(new ActionListener(){
  232.                 public void actionPerformed(ActionEvent e){
  233.                     drawPad.gruen();
  234.                 }
  235.             });
  236.         oval.addActionListener(new ActionListener(){
  237.                 public void actionPerformed(ActionEvent e){
  238.                     drawPad.oval();
  239.                 }
  240.             });
  241.         rechteck.addActionListener(new ActionListener(){
  242.                 public void actionPerformed(ActionEvent e){
  243.                     drawPad.rechteck();
  244.                 }
  245.             });
  246.         dreieck.addActionListener(new ActionListener(){
  247.                 public void actionPerformed(ActionEvent e){
  248.                     drawPad.dreieck();
  249.                 }
  250.             });
  251.         kreisa.addActionListener(new ActionListener(){
  252.                 public void actionPerformed(ActionEvent e){
  253.                     drawPad.kreisa();
  254.                 }
  255.             });
  256.         rechtecka.addActionListener(new ActionListener(){
  257.                 public void actionPerformed(ActionEvent e){
  258.                     drawPad.rechtecka();
  259.                 }
  260.             });
  261.         textHinzufuegen.addActionListener(new ActionListener(){
  262.                 public void actionPerformed(ActionEvent e){
  263.                     drawPad.textHinzufuegen();
  264.                 }
  265.             });
  266.         fett.addActionListener(new ActionListener(){
  267.                 public void actionPerformed(ActionEvent e){
  268.                     drawPad.schriftStilFett();
  269.                 }
  270.             });
  271.         kursiv.addActionListener(new ActionListener(){
  272.                 public void actionPerformed(ActionEvent e){
  273.                     drawPad.schriftStilKursiv();
  274.                 }
  275.             });
  276.         fettkursiv.addActionListener(new ActionListener(){
  277.                 public void actionPerformed(ActionEvent e){
  278.                     drawPad.fettkursiv();
  279.                 }
  280.             });
  281.         schriftGroesse.addActionListener(new ActionListener(){
  282.                 public void actionPerformed(ActionEvent e){
  283.                     drawPad.schriftGroesse();
  284.                 }
  285.             });
  286.  
  287.         bildEinfugen.addActionListener(new ActionListener(){
  288.                 public void actionPerformed(ActionEvent e){
  289.                     drawPad.bildEinfuegen();
  290.                 }
  291.             });
  292.  
  293.         slider.addChangeListener(new ChangeListener(){
  294.                 public void stateChanged(ChangeEvent e){
  295.                     drawPad.slider();
  296.                 }
  297.             });
  298.  
  299.         breiteauswahlbox.addActionListener(new ActionListener(){
  300.                 public void actionPerformed(ActionEvent e){
  301.                     drawPad.strichdicke();
  302.                 }
  303.             });
  304.         //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  305.  
  306.         // Programm ordnungsgemaeß schließen
  307.         setDefaultCloseOperation( EXIT_ON_CLOSE );
  308.  
  309.     }
  310.  
  311.     public void getSlider() {
  312.         JSlider b = this.slider;
  313.         int i = 0;
  314.     }
  315.  
  316.     class PadDraw extends JComponent{
  317.         Image image;
  318.  
  319.         Graphics2D graphics2D;
  320.  
  321.         // Mausposititon speichern
  322.         int currentX, currentY, oldX, oldY, x, y;
  323.  
  324.         // Konstruktor
  325.         public PadDraw(){
  326.  
  327.             // Akutalisiert die Mauskoordinaten auf Mausklick
  328.             addMouseListener(new MouseAdapter(){
  329.                     public void mousePressed(MouseEvent e){
  330.                         oldX = e.getX();
  331.                         oldY = e.getY();
  332.                     }
  333.                 });
  334.  
  335.             // Wenn die Maus gedrückt und geogen wird, wird eien Linie dorthin gezeichnet
  336.             addMouseMotionListener(new MouseMotionAdapter(){
  337.                     public void mouseDragged(MouseEvent e){
  338.                         String label;
  339.                         ButtonModel aktuell;
  340.  
  341.                         aktuell = modusGroup.getSelection();
  342.  
  343.                         if(aktuell == null)
  344.                             return;
  345.  
  346.                         label = aktuell.getActionCommand();
  347.  
  348.                         currentX = e.getX();
  349.                         currentY = e.getY();
  350.                        
  351.                         // Hat hier eigentlich nichts verloren; ich müsste aber sonst deinen Code umschreiben, wozu mir grad die Zeit fehlt ^^
  352.                         if(label.equals("Normal")){
  353.                             graphics2D.setPaint(Color.black);
  354.                         }
  355.                         if(label.equals("Radieren")){
  356.                             graphics2D.setPaint(Color.white);
  357.                         }
  358.                        
  359.                         graphics2D.drawLine(oldX, oldY, currentX, currentY);
  360.                         repaint();
  361.                         oldX = currentX;
  362.                         oldY = currentY;
  363.                     }
  364.                 });
  365.         }
  366.  
  367.         // Zeichenfläche erstellen
  368.         public void paintComponent(Graphics g){
  369.             if(image == null){
  370.                 image = createImage(getSize().width, getSize().height);
  371.                 graphics2D = (Graphics2D)image.getGraphics();
  372.                 graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  373.                 clear();
  374.  
  375.             }
  376.             g.drawImage(image, 0, 0, null);
  377.         }
  378.  
  379.         // Befehle für ActionListener
  380.         public void speichern() {     // Bildschirm Speichern
  381.  
  382.             try {
  383.                 Thread.sleep(600); // warten
  384.             } catch(Exception e){System.out.println(e);}
  385.  
  386.             Robot r;
  387.             try{
  388.                 r = new Robot();
  389.             } catch(AWTException e){
  390.                 throw new IllegalArgumentException("No robot");
  391.             }
  392.  
  393.             // Screenshotbereich erstellen
  394.             int width = drawPad.getWidth();
  395.             int height = drawPad.getHeight();
  396.  
  397.             Rectangle rect = new Rectangle( // X, Y Position
  398.                     (drawPad.getBounds().width - width), (drawPad.getBounds().height -height)
  399.                         // Größe
  400.                 ,width, height);
  401.  
  402.             // Namen der Datei (Screen + aktuelles Datum + aktuelle Zeit)
  403.             SimpleDateFormat hour = new SimpleDateFormat("HH,mm,ss");
  404.             SimpleDateFormat day = new SimpleDateFormat("dd.MM.yyyy");
  405.  
  406.             String zeit = hour.format(new Date());
  407.  
  408.             String name = String.format("Screen %s %s.png", day.format(new Date()), zeit);
  409.             //name =  System.getProperty("user.home")+name; // Home Verzeichnis
  410.  
  411.             // Screenshot erzeugen
  412.             BufferedImage bufferedImage = r.createScreenCapture(rect);
  413.             try {
  414.                 ImageIO.write(bufferedImage, "png", new File(name));
  415.                 System.out.printf("%s OK",name);
  416.             } catch( IOException ex) {
  417.                 ex.printStackTrace();
  418.                 System.out.printf("%s Fehler",name);
  419.             }
  420.         }
  421.  
  422.         public void gelb(){
  423.             graphics2D.setPaint(Color.yellow);
  424.         }
  425.  
  426.         public void blau(){
  427.             graphics2D.setPaint(Color.blue);
  428.         }
  429.  
  430.         public void rot(){
  431.             graphics2D.setPaint(Color.red);
  432.         }
  433.  
  434.         public void schwarz(){
  435.             graphics2D.setPaint(Color.black);
  436.         }
  437.  
  438.         public void orange(){
  439.             graphics2D.setPaint(Color.orange);
  440.         }
  441.  
  442.         public void rosa(){
  443.             graphics2D.setPaint(Color.pink);
  444.         }
  445.  
  446.         public void gruen(){
  447.             graphics2D.setPaint(Color.green);
  448.         }
  449.  
  450.         public void oval(){
  451.             graphics2D.drawOval(oldX, oldY, 50, 50);
  452.             repaint();
  453.         }
  454.  
  455.         public void rechteck(){
  456.             graphics2D.drawRect(oldX, oldY, 50, 50);
  457.             repaint();
  458.         }
  459.  
  460.         public void dreieck(){
  461.  
  462.         }
  463.  
  464.         public void textHinzufuegen(){
  465.             int messageType = JOptionPane.QUESTION_MESSAGE;
  466.             JFrame frame = new JFrame();
  467.             String answer = JOptionPane.showInputDialog(frame,
  468.                     "Welcher Text?",
  469.                     "Texteingabebox", messageType);
  470.             graphics2D.drawString(answer ,oldX, oldY);
  471.             repaint();
  472.         }
  473.  
  474.         public void schriftStilFett(){
  475.             graphics2D.setFont(new Font("TimesRoman", Font.BOLD, 12));
  476.             repaint();
  477.         }
  478.  
  479.         public void schriftStilKursiv(){
  480.             graphics2D.setFont(new Font("TimesRoman", Font.ITALIC, 12));
  481.             repaint();
  482.         }
  483.  
  484.         public void fettkursiv(){
  485.             graphics2D.setFont(new Font("TimesRoman", Font.BOLD + Font.ITALIC, 12));
  486.             repaint();
  487.         }
  488.  
  489.         public void schriftGroesse(){
  490.             int messageType = JOptionPane.QUESTION_MESSAGE;
  491.             JFrame frame = new JFrame();
  492.             String answer = JOptionPane.showInputDialog(frame,
  493.                     "Welche Größe?",
  494.                     "Schriftgrößeeingabebox", messageType);
  495.             int groesse = Integer.parseInt(answer);
  496.             graphics2D.setFont(new Font("TimesRoman" , Font.PLAIN, groesse));
  497.             repaint();
  498.         }
  499.  
  500.         public void kreisa(){
  501.             int messageType = JOptionPane.QUESTION_MESSAGE;
  502.             JFrame frame = new JFrame();
  503.             String answer = JOptionPane.showInputDialog(frame,
  504.                     "Welche Größe?",
  505.                     "Kreisgrößeauswahlbox", messageType);
  506.             int dicke = Integer.parseInt(answer);
  507.             String answer2 = JOptionPane.showInputDialog(frame,
  508.                     "Welche Größe?",
  509.                     "Kreisgrößeauswahlbox", messageType);
  510.             int dicke2 = Integer.parseInt(answer2);
  511.             graphics2D.drawOval(oldX, oldY, dicke, dicke2);
  512.             repaint();
  513.         }
  514.  
  515.         public void rechtecka(){
  516.             int messageType = JOptionPane.QUESTION_MESSAGE;
  517.             JFrame frame = new JFrame();
  518.             String answer = JOptionPane.showInputDialog(frame,
  519.                     "Welche Größe?",
  520.                     "Kreisgrößeauswahlbox", messageType);
  521.             int dicke = Integer.parseInt(answer);
  522.             String answer2 = JOptionPane.showInputDialog(frame,
  523.                     "Welche Größe?",
  524.                     "Kreisgrößeauswahlbox", messageType);
  525.             int dicke2 = Integer.parseInt(answer2);
  526.             graphics2D.drawRect(oldX, oldY, dicke, dicke2);
  527.             repaint();
  528.         }
  529.  
  530.         public void slider(){
  531.             JSlider b = slider;
  532.             int value = b.getValue();
  533.             BasicStroke basic = new BasicStroke(value);
  534.             graphics2D.setStroke(basic);
  535.         }
  536.  
  537.         public void strichdicke(){
  538.             int messageType = JOptionPane.QUESTION_MESSAGE;
  539.             JFrame frame = new JFrame();
  540.             String answer = JOptionPane.showInputDialog(frame,
  541.                     "Welche Linienbreite?",
  542.                     "Linienbreiteauswahlbox", messageType);
  543.             float dicke = Float.parseFloat(answer);
  544.             BasicStroke basic = new BasicStroke(dicke);
  545.             graphics2D.setStroke(basic);
  546.         }
  547.  
  548.         public void dreiecka(){
  549.  
  550.         }
  551.  
  552.         public void bildEinfuegen(){
  553.  
  554.         }
  555.  
  556.         public void clear(){
  557.             graphics2D.setPaint(Color.white);
  558.             graphics2D.fillRect(0, 0, getSize().width, getSize().height);
  559.             graphics2D.setPaint(Color.black);
  560.             repaint();
  561.         }
  562.  
  563.         // liefert die minimale Größe des Images
  564.         public Dimension getMinimumSize() {
  565.             return new Dimension(1100, 600);
  566.         }
  567.  
  568.         // Lieblingsgröße auf Minimalgröße setzen
  569.         public Dimension getPreferredSize() {
  570.             return getMinimumSize();
  571.         }
  572.  
  573.     }
  574. }
Add Comment
Please, Sign In to add comment