Basic Paint Application with JAVA

Yayınlandı: Şubat 3, 2013 / Programlama
Etiketler:, , ,

Czesc (Hello) 🙂
As you know i have been in Poland for 6 months as an Erasmus student. You know for every plus there is a minus and I dont have enough time for coding but in any case i am a student and i have to do my homeworks =P So, this is my first Java app that i did myself to get good mark from Object Programming lecture.

It is designeg to draw Line with 5 different colours and it has two buttons. One of them is Clear to delete all draw pad and the other one is for saving picture.

Here is the code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;

public class PaintVolkan
{
public static void main(String[] args)
{
PaintWindow frame = new PaintWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}

}

class PaintWindow extends JFrame
{
public PaintWindow()
{
setTitle("Paint it");
setSize(300, 300);

panel = new JPanel();
drawPad = new PadDraw();

panel.setPreferredSize(new Dimension(32, 68));

//Creates a new container
Container content = this.getContentPane();
content.setLayout(new BorderLayout());

//sets the panel to the left, padDraw in the center
content.add(panel, BorderLayout.WEST);
content.add(drawPad, BorderLayout.CENTER);

//add the color buttons:
makeColorButton(Color.BLUE);
makeColorButton(Color.MAGENTA);
makeColorButton(Color.RED);
makeColorButton(Color.GREEN);
makeColorButton(Color.BLACK);

//creates the clear button
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
drawPad.clear();
}
});
panel.add(clearButton);

//creates the save button
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
BufferedImage img = new BufferedImage(drawPad.getWidth(), drawPad.getHeight(), BufferedImage.TYPE_INT_RGB);
drawPad.paint(img.getGraphics());
try{
ImageIO.write(img, "png", new File("Screen.png"));
System.out.println("saved");
}
catch (Exception c) {
System.out.println("not saved" + c.getMessage());

}
}
});
panel.add(saveButton);
}

/*
* makes a button that changes the color
* @param color the color used for the button
*/
public void makeColorButton(final Color color)
{
JButton tempButton = new JButton();
tempButton.setBackground(color);
tempButton.setPreferredSize(new Dimension(16, 16));
panel.add(tempButton);
tempButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
drawPad.changeColor(color);
}
});
}

private JPanel panel;
private PadDraw drawPad;
}

class PadDraw extends JComponent
{
//this is gonna be your image that you draw on
Image BufferedImage;
//this is what we'll be using to draw on
Graphics2D graphics2D;
//these are gonna hold our mouse coordinates
int currentX, currentY, oldX, oldY;

public PadDraw()
{
setDoubleBuffered(false);
addMouseListener(new MouseAdapter()
{
//if the mouse is pressed it sets the oldX & oldY
//coordinates as the mouses x & y coordinates
public void mousePressed(MouseEvent e)
{
oldX = e.getX();
oldY = e.getY();
}
});

addMouseMotionListener(new MouseMotionAdapter()
{
//while the mouse is dragged it sets currentX & currentY as the mouses x and y
//then it draws a line at the coordinates
//it repaints it and sets oldX and oldY as currentX and currentY
public void mouseDragged(MouseEvent e)
{
currentX = e.getX();
currentY = e.getY();

graphics2D.drawLine(oldX, oldY, currentX, currentY);
repaint();

oldX = currentX;
oldY = currentY;
}
});
}

//this is the painting bit
//if it has nothing on it then
//it creates an image the size of the window
//sets the value of Graphics as the image
//sets the rendering
//runs the clear() method
//then it draws the image
public void paintComponent(Graphics g)
{
if(BufferedImage == null)
{
BufferedImage = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D)BufferedImage.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

clear();
}

g.drawImage(BufferedImage, 0, 0, null);
}

//this is the clear
//it sets the colors as white
//then it fills the window with white
//thin it sets the color back to black
public void clear()
{
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
graphics2D.setPaint(Color.black);
repaint();
}

public void changeColor(Color theColor)
{
graphics2D.setPaint(theColor);
repaint();
}
}

 

 
Görsel

Reklamlar

Bir Cevap Yazın

Aşağıya bilgilerinizi girin veya oturum açmak için bir simgeye tıklayın:

WordPress.com Logosu

WordPress.com hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

Google+ fotoğrafı

Google+ hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

Twitter resmi

Twitter hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

Facebook fotoğrafı

Facebook hesabınızı kullanarak yorum yapıyorsunuz. Çıkış  Yap /  Değiştir )

w

Connecting to %s