4 Replies - 985 Views - Last Post: 29 November 2011 - 08:46 PM Rate Topic: -----

#1 N1G3L2  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 25-October 11

Ideas for Paint Program

Posted 29 November 2011 - 12:08 AM

Hello everyone,

I am making a simple paint program and was looking for a few ideas. I have the basic paint program done, but one of the requirements of my assignment is to use at least one array. I am having trouble coming up with ideas that could use an array. I was thinking maybe some way for the user to change the size of the brush?? I'm not too familiar with arrays, so any ideas would be helpful. So far, my program runs fine and is just a basic paint program where the user can choose from a number of different colors, and draw lines by dragging the mouse... I don't want to go too overboard on the project, since I am still learning and this is for an intro class. Any ideas are appreciated. Here is what I have so far in case anyone wants to check it out...

PaintPanel class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PaintPanel{
	public static void main(String[] args){
		Icon iconB = new ImageIcon("blue.gif");
		Icon iconM = new ImageIcon("magenta.gif");
		Icon iconR = new ImageIcon("red.gif");
		Icon iconBl = new ImageIcon("black.gif");
		Icon iconG1 = new ImageIcon("lightgray.gif");
		Icon iconG = new ImageIcon("green.gif");
		Icon iconY = new ImageIcon("yellow.gif");
		Icon iconO = new ImageIcon("orange.gif");
		Icon iconC = new ImageIcon("cyan.gif");
		Icon iconP = new ImageIcon("pink.gif");
		
		JFrame frame = new JFrame("Nigel's Paint Program"); // creates frame with title
		Container content = frame.getContentPane(); // creates container
		content.setLayout(new BorderLayout()); //set layout
		
		final Painter paintPad = new Painter(); //creates Paint class (the program)	
		content.add(paintPad, BorderLayout.CENTER); //sets the drawPad in the center
		
		JPanel panel = new JPanel(); //creates a JPanel
		panel.setPreferredSize(new Dimension(120, 10));
		panel.setMinimumSize(new Dimension(120, 10));    // panel size
		panel.setMaximumSize(new Dimension(120, 10));
		
		JButton clearButton = new JButton("Clear All"); //"Clear All" button
		clearButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.clear();
			}
		});
		//this is the clear button, which clears the screen.  This pretty
		//much attaches an action listener to the button and when the
		//button is pressed it calls the clear() method
		
		JButton redButton = new JButton(iconR);
		//creates the red button and sets the icon we created for red
		redButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.red();
			}
		});
		//when pressed it will call the red() method.  So on and so on =]
		
		JButton blackButton = new JButton(iconBl);
		//same thing except this is the black button
		blackButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.black();
			}
		});
		
		JButton grayButton = new JButton(iconG1);
		//gray button
		grayButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.gray();
			}
		});
		
		JButton magentaButton = new JButton(iconM);
		//magenta button
		magentaButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.magenta();
			}
		});
		
		JButton pinkButton = new JButton(iconP);
		//orange button
		pinkButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.pink();
			}
		});
		
		JButton blueButton = new JButton(iconB);
		//blue button
		blueButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.blue();
			}
		});
		
		JButton cyanButton = new JButton(iconC);
		//same thing except this is the cyan button
		cyanButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.cyan();
			}
		});
		
		JButton greenButton = new JButton(iconG);
		//green button
		greenButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.green();
			}
		});
		
		JButton yellowButton = new JButton(iconY);
		//yellow button
		yellowButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.yellow();
			}
		});
		
		JButton orangeButton = new JButton(iconO);
		//orange button
		orangeButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				paintPad.orange();
			}
		});
		
		blackButton.setPreferredSize(new Dimension(16, 16));
		grayButton.setPreferredSize(new Dimension(16, 16));
		magentaButton.setPreferredSize(new Dimension(16, 16));
		pinkButton.setPreferredSize(new Dimension(16, 16));
		redButton.setPreferredSize(new Dimension(16, 16));
		blueButton.setPreferredSize(new Dimension(16, 16));     //button sizes
		greenButton.setPreferredSize(new Dimension(16,16));
		yellowButton.setPreferredSize(new Dimension(16,16));
		orangeButton.setPreferredSize(new Dimension(16,16));
		cyanButton.setPreferredSize(new Dimension(16,16));
		
		panel.add(blackButton);
		panel.add(grayButton);
		panel.add(blueButton);
		panel.add(cyanButton);
		panel.add(greenButton);
		panel.add(redButton);
		panel.add(magentaButton);
		panel.add(pinkButton);      // panel buttons
		panel.add(orangeButton);
		panel.add(yellowButton);
		panel.add(clearButton);
		
		content.add(panel, BorderLayout.WEST); //sets the panel to the left
		
		frame.setSize(700, 500); //size of the frame
		frame.setResizable(false); // frame is NOT resizable
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes on exit
		frame.setVisible(true); //makes it visible
	}
}


And the Painter class:
import java.awt.*;
import java.awt.event.*;

import javax.swing.JComponent;

public class Painter extends JComponent{
	Image image; //image that you draw on
	Graphics2D graphics2D; //this is what we'll be using to draw on
	int currentX, currentY, oldX, oldY; //mouse coordinates

	/**
	 * Constructor. If the mouse is pressed, oldX and oldY
	 * are set as the mouses x and y coordinates
	 */
	public Painter(){
		setDoubleBuffered(false);
		addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				oldX = e.getX();
				oldY = e.getY();
			}
		});
		
		/**
		 * While the mouse is clicked and dragged, currentX and currentY
		 * are set as the mouses x and y coordinates and draws a line.
		 * Then it sets oldX and oldY as currentX and currentY
		 */
		addMouseMotionListener(new MouseMotionAdapter(){
			public void mouseDragged(MouseEvent e){
				currentX = e.getX();
				currentY = e.getY();
				if(graphics2D != null)
				graphics2D.drawLine(oldX, oldY, currentX, currentY);
				repaint();
				oldX = currentX;
				oldY = currentY;
			}
		});
	}
	
	/**
	 * Creates the image to paint on
	 */
	public void paintComponent(Graphics g){
		if(image == null){
			image = createImage(getSize().width, getSize().height);
			graphics2D = (Graphics2D)image.getGraphics();
			clear();

		}
		g.drawImage(image, 0, 0, null);
	}
	
	/**
	 * Clears the image to paint on
	 */
	public void clear(){
		graphics2D.setPaint(Color.white);
		graphics2D.fillRect(0, 0, getSize().width, getSize().height);
		graphics2D.setPaint(Color.black);
		repaint();
	}
	
	public void red(){
		graphics2D.setPaint(Color.red);
		repaint();
	}

	public void black(){
		graphics2D.setPaint(Color.black);
		repaint();
	}
	
	public void gray(){
		graphics2D.setPaint(Color.lightGray);
		repaint();
	}
	
	public void cyan(){
		graphics2D.setPaint(Color.cyan);
		repaint();
	}
	
	public void pink(){
		graphics2D.setPaint(Color.pink);
		repaint();
	}
	
	public void magenta(){
		graphics2D.setPaint(Color.magenta);
		repaint();
	}

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

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

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

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

}


Is This A Good Question/Topic? 0
  • +

Replies To: Ideas for Paint Program

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Ideas for Paint Program

Posted 29 November 2011 - 01:19 AM

After reading your introductory paragraph, I took one look at the first code block and all of those ImageIcons immediately jumped out at me as wanting to be in an array. All of the JButtons, and JPanels as well could easily be put into an array.

Quote

I'm not too familiar with arrays, so any ideas would be helpful.


Perhaps you should read up on them then.

Try TriggaMike's tutorial on arrays.
Was This Post Helpful? 2
  • +
  • -

#3 N1G3L2  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 25-October 11

Re: Ideas for Paint Program

Posted 29 November 2011 - 08:33 PM

Awesome thanks. I thought that would be a good idea also, but wasn't too sure if it was possible to use an array for the ImageIcons. Thanks for the idea and the link. I will definitely check it out =)
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Ideas for Paint Program

Posted 29 November 2011 - 08:36 PM

Java already comes with a bunch of Color constants, found in the java.awt.Color class. You might find it easier to use the JColorChooser class as well.

Lastly, having methods for each color really is overdoing it. Take a look at the arrays tutorial, then think about how you could simplify with an array. A JColorChooser should further simplify things.

I think you might be biting off a lot if you aren't comfortable with arrays. Learning more is always good, but get a good handle on the basics (OOP, methods, arrays, ArrayLists, Strings) before tackling Swing. Trust me- if you don't have a good handle on OOP, working with Swing can get messy and overwhelming quickly. :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Ideas for Paint Program

Posted 29 November 2011 - 08:46 PM

instead of

void red() {
void back() {
void green() {
... what will you do with 600 colors

better to have
void setColor(Color c) {
    graphics2D.setPaint(c);
}



Hint:
- put you ImageIcon in an array
- put your Button in an array

your code will pss from 151 to 64 lines
for your other class from 114 to 68 lines

:^:

*Editor 64 and 68 lines the actual figures :)

This post has been edited by pbl: 29 November 2011 - 09:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1