Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Java paint application  (Read 3144 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: July 29, 2008, 04:01:30 AM »

Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. DrawLine class
  3.  *****************
  4. By: deAppel http://www.engineeringserver.com - http://www.javaforums.net
  5. Contact: info [@] engineeringserver.com
  6. Version: 29/07/2008
  7. "*****************
  8. Note: a very basic demo that shows how to draw on a JPanel like a paint application
  9. //----------------------------------------------------------------------*/
  10. import java.awt.Graphics;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13. import java.util.ArrayList;
  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16.  
  17. public class DrawLine extends JFrame implements MouseListener{
  18. int positionX1 = 0;
  19. int positionY1 = 0;
  20.  
  21. int positionX2 = 0;
  22. int positionY2 = 0;
  23. int maxLines = 1000;
  24. int currentLines = 0;
  25. int[][] drawLines = new int[maxLines][4];
  26.  
  27. ArrayList al = new ArrayList();
  28. public DrawLine(){
  29. setLayout(null);
  30. JPanel p = new JPanel();
  31. p.addMouseListener(this);
  32. p.setBounds(0,0,800,600);
  33. add(p);
  34.  
  35. setTitle("www.engineeringserver.com - Basic paint application");
  36. setSize(800,600);
  37. setResizable(false);
  38. setLocationRelativeTo(null);
  39. setVisible(true);
  40. }
  41.  
  42. public static void main(String[] args){
  43. DrawLine DL = new DrawLine();
  44. }
  45.  
  46. public void paint(Graphics g){
  47. super.paintComponents(g);
  48. for(int i = 0; i < al.size(); i++){
  49. g.drawLine(drawLines[i][0],drawLines[i][1],drawLines[i][2],drawLines[i][3]);
  50. }
  51. }
  52.  
  53. public void mouseClicked(MouseEvent e) {
  54. }
  55.  
  56. public void mouseEntered(MouseEvent e) {}
  57. public void mouseExited(MouseEvent e) {}
  58. public void mousePressed(MouseEvent e) {
  59. drawLines[currentLines][0] = positionX1 = e.getX();
  60. drawLines[currentLines][1] = positionY1 = e.getY();
  61. }
  62.  
  63. public void mouseReleased(MouseEvent e) {
  64. drawLines[currentLines][2] = positionX2 = e.getX();
  65. drawLines[currentLines][3] = positionY2 = e.getY();
  66. al.add(drawLines);
  67. currentLines++;
  68. repaint();
  69. }
  70. }
  71.  
Created by GeSHI 1.0.7.20

example
just a sad looking drawing because i've only supported the drawLines() function so i cannot draw any ovals yet but that can be easily done if you modify my code a bit.

http://img509.imageshack.us/img509/6924/paintmu3.png
Java paint application
« Last Edit: July 29, 2008, 04:03:36 AM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here