d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [java] Need Help Creating A Very Simple Paint Gui > Paying Fg For This Code.
Add Reply New Topic New Poll
Member
Posts: 3,379
Joined: Sep 2 2006
Gold: 15,870.70
Apr 15 2013 10:38pm
Need to write a program that creates a GUI that is basically a super simple paint application. I don't need it to create any fancy shapes or anything. I just need it to draw whenever the mouse is pressed.

Requirements
1. Background color needs to be white.
2. Includes the following buttons: Red (changes color to red), Blue (changes color to blue), Green (changes color to green), Clear (clears the drawing area).
3. Size of window should be (750, 500)

Paying 200fg for this to be done, it should be a very easy task for someone with coding experience.


This post was edited by Road2Death on Apr 15 2013 10:38pm
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Apr 16 2013 07:28am
I updated it to make the lines smooth
Code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Paint {

 public static Color currentColor = Color.RED;

 public static void main( String[] a ) {
   final DrawingFrame f = new DrawingFrame( );
   f.setTitle( "Paint" );
   f.setBounds( 0, 0, 750, 500 );
   f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   JPanel panel = new JPanel( );
   panel.setBackground( Color.WHITE );
   f.add( panel );
   JButton btnRed = new JButton( "Red" );
   btnRed.setBackground( Color.RED );
   btnRed.addActionListener( new ActionListener( ) {
     @Override
     public void actionPerformed( ActionEvent arg0 ) {
       currentColor = Color.RED;
     }
   } );
   panel.add( btnRed );

   JButton btnBlue = new JButton( "Blue" );
   btnBlue.setBackground( Color.BLUE );
   btnBlue.addActionListener( new ActionListener( ) {
     @Override
     public void actionPerformed( ActionEvent arg0 ) {
       currentColor = Color.BLUE;
     }
   } );
   panel.add( btnBlue );

   JButton btnGreen = new JButton( "Green" );
   btnGreen.setBackground( Color.GREEN );
   btnGreen.addActionListener( new ActionListener( ) {
     @Override
     public void actionPerformed( ActionEvent arg0 ) {
       currentColor = Color.GREEN;
     }
   } );
   panel.add( btnGreen );
   btnGreen.setVisible( true );

   JButton btnClear = new JButton( "Clear" );
   btnClear.addActionListener( new ActionListener( ) {
     @Override
     public void actionPerformed( ActionEvent arg0 ) {
       f.getGraphics( ).clearRect( 0, 0, 750, 500 );
       f.repaint( );
     }
   } );
   panel.add( btnClear );

   f.setVisible( true );
 }

 static class DrawingFrame extends JFrame {

   int lastX;

   int lastY;

   public DrawingFrame( ) {

     addMouseMotionListener( new MouseMotionListener( ) {

       @Override
       public void mouseDragged( MouseEvent e ) {

         Graphics g = getGraphics( );
         g.setColor( currentColor );

         g.drawLine( lastX, lastY, e.getX( ), e.getY( ) );

         lastX = e.getX( );
         lastY = e.getY( );

       }

       @Override
       public void mouseMoved( MouseEvent e ) {
         lastX = e.getX( );
         lastY = e.getY( );
       }

     } );
   }
 }

}


This post was edited by labatymo on Apr 16 2013 07:35am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll