JavaPaint

This programm was written by me. Pretty bad coding but it works to “draw” a litte bit. Color implementation is still in work.. or not..

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
 
import java.io.*;
 
//Made by Lunetikk
import javax.imageio.*;
 
import javax.swing.*;
import javax.swing.JOptionPane.*;
 
 
public class JavaPaintv3 extends JFrame implements MouseMotionListener,
                                                   KeyListener, ActionListener// Anfang Attribute
// Ende Attribute
 {
  JButton exit_button;
  JButton dicker_button;
  JButton duenner_button;
  JButton neu_button;
  JButton save_button;
  int x = 0;
  int y = 0;
  int xold = 0;
  int yold = 0;
  int dicke = 2;
  Image img = null;
 
  // Anfang Methoden
  public JavaPaintv3() {
    setTitle("JavaPaintv3");
 
    neu_button = new JButton("New");
    neu_button.addActionListener(this);
    add(neu_button);
 
    dicker_button = new JButton("Line +");
    dicker_button.addActionListener(this);
    add(dicker_button);
 
    duenner_button = new JButton("Line -");
    duenner_button.addActionListener(this);
    add(duenner_button);
 
    save_button = new JButton("Save");
    save_button.addActionListener(this);
    add(save_button);
 
    exit_button = new JButton("Quit");
    exit_button.addActionListener(this);
    add(exit_button);
 
    setLayout(new FlowLayout());
 
    pack();
  }
 
  public void paint(Graphics g) {
    if (img == null) {
      img = createImage(getWidth(), getHeight());
    }
 
    Graphics gi = img.getGraphics();
    Graphics2D g2 = (Graphics2D) gi;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(dicke));
 
    gi.drawLine(xold, yold, x, y);
    xold = x;
    yold = y;
 
    g.drawImage(img, 0, 0, this);
  }
 
  public void mouseMoved(MouseEvent e) //Mausbewegung wird gezeigt
   {
    xold = e.getX();
    yold = e.getY();
  }
 
  public void mouseDragged(MouseEvent e) //Mausbewegung bei geklickter Taste wird gezeigt
   {
    System.out.println(e);
    x = e.getX();
    y = e.getY();
    repaint();
  }
 
  public void keyPressed(KeyEvent e) //Window wieder geöffnet nach minimieren
   {
    char Taste = e.getKeyChar();
    System.out.println(e);
 
    if (Taste == 'q') {
      System.exit(0);
      dispose();
    }
 
    if (Taste == 'n') {
      img = createImage(getWidth(), getHeight());
      repaint();
    }
 
    if (Taste == '+') {
      dicke++;
    }
 
    if (Taste == '-') {
      if (dicke != 1) {
        dicke--;
      }
    }
 
    if (Taste == 's') {
      BufferedImage bimg = new BufferedImage(getWidth(), getHeight(),
                                             BufferedImage.TYPE_INT_RGB);
      Graphics g = bimg.getGraphics();
      g.drawImage(img, 0, 0, this);
 
      try {
        ImageIO.write(bimg, "png", new File("C:/Test/bild.png"));
      } catch (IOException ex) {
        System.out.println("Fehler:" + ex.getMessage());
      }
    }
  }
 
  public void keyReleased(KeyEvent e) //Window wieder geöffnet nach minimieren
   {
    System.out.println(e);
  }
 
  public void keyTyped(KeyEvent e) //Window wieder geöffnet nach minimieren
   {
    System.out.println(e);
  }
 
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == exit_button) {
      System.exit(0);
    }
 
    if (e.getSource() == neu_button) {
      img = createImage(getWidth(), getHeight());
      repaint();
      x = 0;
      y = 0;
      xold = 0;
      yold = 0;
 
      //       farbe = Color.BLACK;
    }
 
    if (e.getSource() == dicker_button) {
      dicke++;
    }
 
    if (e.getSource() == duenner_button) {
      if (dicke != 1) {
        dicke--;
      }
    }
 
    if (e.getSource() == save_button) {
      BufferedImage bimg = new BufferedImage(getWidth(), getHeight(),
                                             BufferedImage.TYPE_INT_RGB);
      Graphics g = bimg.getGraphics();
      g.drawImage(img, 0, 0, this);
 
      try {
        ImageIO.write(bimg, "png", new File("bild.png"));
      } catch (IOException ex) {
        System.out.println("Fehler:" + ex.getMessage());
      }
    }
  }
 
  public static void main(String[] args) {
    JavaPaintv3 b = new JavaPaintv3();
 
    b.setSize(500, 500);
    b.addMouseMotionListener(b);
    b.addKeyListener(b);
    b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    b.setVisible(true);
 
    JPanel panel = new JPanel();
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel("Before using the Programm click the New button twice."));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel("Line + makes your line bigger"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel("Line - makes your line smaller"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel("Save will save a picture called bild.png next to your JavaPaintv3.exe"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel("Quit will close JavaPaintv3"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel("You can close this Infobox now and start drawing! Have fun :)"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
    panel.add(new JLabel("Made by Lunetikk"));
    panel.add(new JLabel(">                                                                                                                                                                         <"));
 
    //    JButton closeinfo = new JButton ("Close");
    //    closeinfo.addActionListener (this);
    //    panel.add (closeinfo, "Center");
    //    content_panel.add (panel);
 
    //         close_button = new JButton("Quit");
    //         close_button.addActionListener(this);
    //         add(close_button);
    JDialog dialog = new JDialog(b, "JavaPaintv3 Info", false);
    dialog.add(panel);
 
    dialog.setSize(500, 500);
    b.setVisible(true);
    dialog.setVisible(true);
  }
 
  // Anfang Komponenten
  // Ende Komponenten
  //  public void InfoShowMessageDialog() {
  //    JOptionPane.showMessageDialog(this, "", "JavaPaintv3.exe", JOptionPane.ERROR_MESSAGE);
  //  }
 
  // Ende Methoden
}