Search Tutorials
Here I, Ritom make many Coding tutorials for example Beginner tutorials, Simple apps recreation, Minecraft modding and much more
Featured
- Get link
- X
- Other Apps
Labels
Simple Paint
Simple Paint
Do you want to create something new In java.
Today We are going to learn how to recreate Paint in Java
ScreenShots :-
Lets Get Started :-
1. Create a new Eclipse project. If you don't know how you can check our last tutorial.
2. Create the Main class and a Panel class.
3. Create main method inside main and copy this code inside Main class
JFrame frame = new JFrame("Simple Paint"); // Creating Instance of our Frame
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // if we close our frame the //program will stop too
Dimension windowSize = new Dimension((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()-200,(int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight()-200)); // Getting Size of Computer window
frame.setSize(windowSize); // set frame size to computer window size
frame.setMinimumSize(new Dimension(500,500)); // Minimum size for our window
frame.setLocationRelativeTo(null); // Making our frame visible at the center of the frame.
If you don't know how to create class or main method check our last tutorial
4. The Panel class(Created in 2nd Step) should extends JPanel implement MouseListener, MouseMotionListener
5. Add our Panel to The frame. At the END your main method will look like this :-
6. Add this to code to your panel constructor
setBackground(Color.WHITE); // Set background Color of Panel
Dimension windowSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width-300,Toolkit.getDefaultToolkit().getScreenSize().height-300) ; // Set the Size of our Panel this should smaller than frame size
setPreferredSize(windowSize); // Set the Size
addMouseListener(this); // Add a mouse Listener Should we can check if we are clicking mouse buttons
addMouseMotionListener(this); // Add Mouse Motion Listener so we can check if where we are clicking the mouse on
7. After implementing you will have some methods edit those as we did here
Graphics2D g; // Add this Variable for drawing
private void setUpDrawingGraphics() // Add this method to draw with our Graphics (g variable)
{
g = (Graphics2D) getGraphics();
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
prevX = x;
prevY = y;
setUpDrawingGraphics();
g.drawLine(prevX, prevY, x, y);
}
public void mouseDragged(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
g.drawLine(prevX, prevY, x, y);
prevX = x;
prevY = y;
}
@Override
public void mouseMoved(MouseEvent e) {} @Override public void mouseClicked(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {}@Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} // We do not need these methods but we can't delete them too
And That's it you created Paint in Java.
To Learn more about java Programming follow our blog.
Comment What you want next.
And Sorry I was not able to post something yesterday.
Popular Posts
Start coding with Java with low end pc
- Get link
- X
- Other Apps
Comments
Post a Comment