Results 1 to 5 of 5
Thread: problem in paint program
- 06-21-2013, 11:17 AM #1
Member
- Join Date
- Jun 2013
- Posts
- 2
- Rep Power
- 0
problem in paint program
I have made a paint program....I want to add a selection tool which selects a part of the image and moves it when i dragg my mouse......This feature is there in ms Paint...
I dont know how to code this......I will put up my program ....Please provide me with the code I want to complete my program.....
Thank You
here is my code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
public class paint{
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 iconW = new ImageIcon("white.gif");
Icon iconGr = new ImageIcon("grey.gif");
Icon iconY = new ImageIcon("yellow.gif");
Icon iconG = new ImageIcon("green.gif");
Icon iconO = new ImageIcon("orange.gif");
Icon iconC = new ImageIcon("cyan.gif");
Icon iconP = new ImageIcon("pink.gif");
//image icons for different colors.
Icon iconCl = new ImageIcon("close.gif");
//image icon for close button
Icon iconLi = new ImageIcon("line.jpg");
Icon iconFr = new ImageIcon("filledrectangle.jpg");
Icon iconHr = new ImageIcon("hollowrectangle.jpg");
Icon iconFo = new ImageIcon("filledoval.jpg");
Icon iconHo = new ImageIcon("emptyoval.jpg");
Icon iconPe = new ImageIcon("pencil.jpg");
Icon iconEr = new ImageIcon("eraser.jpg");
Icon iconPo = new ImageIcon("polygon.jpg");
Icon iconCle = new ImageIcon("clear.jpg");
JFrame frame = new JFrame("Paint It");
//Creates a frame with a title of "Paint it"
Container content = frame.getContentPane();
//Creates a new container
content.setLayout(new BorderLayout());
//sets the layout
final PadDraw drawPad= new PadDraw();
//creates a new padDraw, which is pretty much the paint program
content.add(drawPad, BorderLayout.CENTER);
//sets the padDraw in the center
JPanel panel = new JPanel();
//creates a JPanel
panel.setPreferredSize(new Dimension(150,700));
panel.setMinimumSize(new Dimension(150,700));
panel.setMaximumSize(new Dimension(150,700));
JPanel panel2 =new JPanel();
panel2.setPreferredSize(new Dimension(300, 30));
panel2.setMinimumSize(new Dimension(300, 30));
panel2.setMaximumSize(new Dimension(300, 30));
JPanel panel3 = new JPanel();
panel3.setPreferredSize(new Dimension(150,700 ));
panel3.setMinimumSize(new Dimension(150, 700));
panel3.setMaximumSize(new Dimension(150, 700));
//This sets the size of the panels
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem save = new JMenuItem("Save");
file.add(save);
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.save();
}
});
JMenuItem open = new JMenuItem("Open");
file.add(open);
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.open();
}
});
JMenuItem close2 = new JMenuItem("Close");
file.add(close2);
close2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
JButton clearButton = new JButton(iconCle);
//creates the clear button and sets the text as "Clear"
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.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 eraseButton = new JButton(iconEr);
//creates the eraser button and sets the text as "Eraser"
eraseButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.erase();
}
});
JButton selectButton = new JButton("Select");
//creates the clear button and sets the text as "Clear"
selectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.select();
}
});
JRadioButton thinButton = new JRadioButton("Thin Line");
thinButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.thin();
}
});
JRadioButton mediumButton = new JRadioButton("Medium Line");
mediumButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.medium();
}
});
JRadioButton thickButton = new JRadioButton("Thick Line");
thickButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.thick();
}
});
//the above three buttons are for the thickness
JButton rectButton = new JButton(iconHr);
rectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.rect();
}
});
JButton rect2Button = new JButton(iconFr);
rect2Button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.rect2();
}
});
JButton ovalButton = new JButton(iconHo);
ovalButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.oval();
}
});
JButton oval2Button = new JButton(iconFo);
oval2Button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.oval2();
}
});
JButton penButton = new JButton(iconPe);
penButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.pen();
}
});
JButton lineButton = new JButton(iconLi);
lineButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.line();
}
});
JButton polygonButton = new JButton(iconPo);
polygonButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.polygon();
}
});
//the above buttons are for various operations in paint
JButton closeButton = new JButton(iconCl);
//creates the close button
closeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
// the buttons below are for various colors
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){
drawPad.red();
}
});
//when pressed it will call the red() method. So on and so forth =]
JButton blackButton = new JButton(iconBl);
//same thing except this is the black button
blackButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.black();
}
});
JButton magentaButton = new JButton(iconM);
//magenta button
magentaButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.magenta();
}
});
JButton blueButton = new JButton(iconB);
//blue button
blueButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.blue();
}
});
JButton greenButton = new JButton(iconG);
//green button
greenButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.green();
}
});
JButton greyButton = new JButton(iconGr);
//grey button
greyButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.grey();
}
});
JButton whiteButton = new JButton(iconW);
//white button
whiteButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.white();
}
});
JButton yellowButton = new JButton(iconY);
//yellow button
yellowButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.yellow();
}
});
JButton orangeButton = new JButton(iconO);
//orange button
orangeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.orange();
}
});
JButton cyanButton = new JButton(iconC);
//cyan button
cyanButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.cyan();
}
});
JButton pinkButton = new JButton(iconP);
//pink button
pinkButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.pink();
}
});
blackButton.setPreferredSize(new Dimension(16,16));
magentaButton.setPreferredSize(new Dimension(16, 16));
redButton.setPreferredSize(new Dimension(16, 16));
blueButton.setPreferredSize(new Dimension(16, 16));
greenButton.setPreferredSize(new Dimension(16,16));
greyButton.setPreferredSize(new Dimension(16,16));
whiteButton.setPreferredSize(new Dimension(16,16));
yellowButton.setPreferredSize(new Dimension(16,16));
orangeButton.setPreferredSize(new Dimension(16,16));
cyanButton.setPreferredSize(new Dimension(16,16));
pinkButton.setPreferredSize(new Dimension(16,16));
clearButton.setPreferredSize(new Dimension(140,120));
eraseButton.setPreferredSize(new Dimension(140,120));
lineButton.setPreferredSize(new Dimension(140,120));
rectButton.setPreferredSize(new Dimension(140,115));
rect2Button.setPreferredSize(new Dimension(140,115));
penButton.setPreferredSize(new Dimension(140,120));
ovalButton.setPreferredSize(new Dimension(140,115));
oval2Button.setPreferredSize(new Dimension(140,115));
polygonButton.setPreferredSize(new Dimension(140,115));
closeButton.setPreferredSize(new Dimension(20,20));
selectButton.setPreferredSize(new Dimension(130,50));
//sets the sizes of the buttons
ButtonGroup lineOption = new ButtonGroup( );
lineOption.add(thinButton);
lineOption.add(mediumButton);
lineOption.add(thickButton);
panel2.add(blackButton);
panel2.add(greyButton);
panel2.add(whiteButton);
panel2.add(redButton);
panel2.add(greenButton);
panel2.add(blueButton);
panel2.add(cyanButton);
panel2.add(magentaButton);
panel2.add(pinkButton);
panel2.add(orangeButton);
panel2.add(yellowButton);
panel.add(clearButton);
panel.add(eraseButton);
panel.add(penButton);
panel.add(lineButton);
panel.add(thickButton);
panel.add(mediumButton);
panel.add(thinButton);
panel3.add(closeButton);
panel3.add(selectButton);
panel3.add(rectButton);
panel3.add(rect2Button);
panel3.add(ovalButton);
panel3.add(oval2Button);
panel3.add(polygonButton);
//adds the buttons to the panels
content.add(panel, BorderLayout.WEST);
//sets the panel to the left
content.add(panel2, BorderLayout.NORTH);
content.add(panel3, BorderLayout.EAST);
frame.setSize(1030, 735);
//sets the size of the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
//makes it so you can close
frame.setVisible(true);
//makes it so you can see it
}
}
class PadDraw extends JComponent{
Image image;
//this is gonna be your image that you draw on
Graphics2D graphics2D;
//this is what we'll be using to draw on
public int currentX, currentY, oldX, oldY,x1,y1,x2,y2,w,h,a1,a2,b1,b2,c1,d1,c2,d2,e1,f1 ,e2,f2,r1,s1,ch,m1,n1,fl,k1,k2=1,cc1,dd1,cc2,dd2,a a1,aa2,bb1,bb2;
//these are gonna hold our mouse coordinates
//Now for the constructors
public void pen()
{
k1=1;
setDoubleBuffered(false);
cursorchange();
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
oldX = e.getX();
oldY = e.getY();
}
});
//if the mouse is pressed it sets the oldX & oldY
//coordinates as the mouses x & y coordinates
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
currentX = e.getX();
currentY = e.getY();
if(graphics2D != null && k1==1)
graphics2D.drawLine(oldX, oldY, currentX,currentY);
repaint();
oldX = currentX;
oldY = currentY;
repaint();
}
});
}
public void line()
{
k1=2;
setDoubleBuffered(false);
cursorchange();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
x1=m.getX();
y1=m.getY();
}
public void mouseReleased(MouseEvent m)
{
x2=m.getX();
y2=m.getY();
if(graphics2D != null && k1==2)
graphics2D.drawLine(x1,y1, x2,y2);
repaint();
}
});
}
public void rect()
{
k1=3;
setDoubleBuffered(false);
cursorchange();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
a1=m.getX();
b1=m.getY();
repaint();
}
public void mouseReleased(MouseEvent m)
{
a2=m.getX();
b2=m.getY();
if (a1>a2)
{
int z=a1;
a1=a2;
a2=z;
}
if (b1>b2)
{
int z=b1;
b1=b2;
b2=z;
}
w=a2-a1;
h=b2-b1;
if(graphics2D != null && k1==3)
graphics2D.drawRect(a1,b1, w,h);
repaint();
}
});
}
public void rect2()
{
k1=4;
setDoubleBuffered(false);
cursorchange();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
aa1=m.getX();
bb1=m.getY();
repaint();
}
public void mouseReleased(MouseEvent m)
{
aa2=m.getX();
bb2=m.getY();
if (aa1>aa2)
{
int z=aa1;
aa1=aa2;
aa2=z;
}
if (bb1>bb2)
{
int z=bb1;
bb1=bb2;
bb2=z;
}
w=aa2-aa1;
h=bb2-bb1;
if(graphics2D != null && k1==4)
graphics2D.fillRect(aa1,bb1, w,h);
repaint();
}
});
}
public void oval()
{
k1=5;
setDoubleBuffered(false);
cursorchange();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
c1=e.getX();
d1=e.getY();
repaint();
}
public void mouseReleased(MouseEvent e)
{
c2=e.getX();
d2=e.getY();
if (c1>c2)
{
int z=c1;
c1=c2;
c2=z;
}
if (d1>d2)
{
int z=d1;
d1=d2;
d2=z;
}
w=c2-c1;
h=d2-d1;
if(graphics2D != null && k1==5)
graphics2D.drawOval(c1,d1,w,h);
repaint();
}
});
}
public void oval2()
{
k1=6;
setDoubleBuffered(false);
cursorchange();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
cc1=m.getX();
dd1=m.getY();
repaint();
}
public void mouseReleased(MouseEvent m)
{
cc2=m.getX();
dd2=m.getY();
if (cc1>cc2)
{
int z=cc1;
cc1=cc2;
cc2=z;
}
if (dd1>dd2)
{
int z=dd1;
dd1=dd2;
dd2=z;
}
w=cc2-cc1;
h=dd2-dd1;
if(graphics2D != null && k1==6)
graphics2D.fillOval(cc1,dd1, w,h);
repaint();
}
});
}
public void polygon()
{
k1=8;
k2=1;
setDoubleBuffered(false);
cursorchange();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
x1=m.getX();
y1=m.getY();
}
final int r1=x1;
final int s1=y1;
public void mouseReleased(MouseEvent m)
{
x2=m.getX();
y2=m.getY();
if (x2==r1 && y2==s1)
{ graphics2D.drawLine(m1,n1, r1,s1);
return;
}
else
{
if(graphics2D != null && k1==8 && k2==1)
graphics2D.drawLine(x1,y1, x2,y2);
if(graphics2D != null && k1==8 && k2!=1)
graphics2D.drawLine(m1,n1, x2,y2);
repaint();
m1=x2;
n1=y2;
k2++;
}
}
});
}
//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 paintComponent(Graphics g){
if(image == null){
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D)image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANT IALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
//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 clear()
{
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
graphics2D.setPaint(Color.black);
repaint();
}
//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 erase()
{
k1=7;
cursorchange();
graphics2D.setPaint(Color.white);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
e1 = e.getX();
f1 = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
e2 = e.getX();
f2 = e.getY();
if(graphics2D != null && k1==7)
graphics2D.fillRect(e1, f1, 27, 27);
repaint();
e1 = e2;
f1 = f2;
}
});
}
public void cursorchange()
{
if (k1==7)
{
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("eraser.gif");
Point hotSpot = new Point(0,0);
Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Eraser");
setCursor(cursor);
}
else
setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR) );
}
// changing the cursor icons
public void open()
{
JFileChooser fileChooser = new JFileChooser();
if ( fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION ) {
String path = fileChooser.getSelectedFile().getAbsolutePath();
BufferedImage loadedImage = null;
try {
loadedImage = ImageIO.read( new File(path) );
} catch(IOException e) {
}
graphics2D.drawImage(loadedImage, 0, 0, null);
repaint();
}
}
//To open a file
public void save() {
JFileChooser fileChooser = new JFileChooser();
if ( fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION ) {
String path = fileChooser.getSelectedFile().getAbsolutePath();
BufferedImage imageToSave = createImage();
try {
ImageIO.write(imageToSave, "jpg", new File(path));
} catch(IOException e) {}
}
}
// To save a file
public BufferedImage createImage() {
BufferedImage returnImage = new BufferedImage( getSize().width, getSize().height, BufferedImage.TYPE_INT_RGB );
Graphics2D g = returnImage.createGraphics();
g.drawImage( image, 0, 0, null );
g.dispose();
return returnImage;
}
public void select()
{
}
public void thick(){
graphics2D.setStroke(new BasicStroke (7));
}
public void thin(){
graphics2D.setStroke(new BasicStroke (1));
}
public void medium(){
graphics2D.setStroke(new BasicStroke (4));
}
//thickness
// various colors
public void red(){
graphics2D.setPaint(Color.red);
repaint();
}
//this is the red paint
public void black(){
graphics2D.setPaint(Color.black);
repaint();
}
//black paint
public void magenta(){
graphics2D.setPaint(Color.magenta);
repaint();
}
//magenta paint
public void blue(){
graphics2D.setPaint(Color.blue);
repaint();
}
//blue paint
public void green(){
graphics2D.setPaint(Color.green);
repaint();
}
//green paint
public void yellow(){
graphics2D.setPaint(Color.yellow);
repaint();
}
//yellow paint
public void grey(){
graphics2D.setPaint(Color.gray);
repaint();
//grey paint
}
public void white(){
graphics2D.setPaint(Color.white);
repaint();
}
//white paint
public void orange(){
graphics2D.setPaint(Color.orange);
repaint();
}
//orange paint
public void cyan(){
graphics2D.setPaint(Color.cyan);
repaint();
}
//cyan paint
public void pink(){
graphics2D.setPaint(Color.pink);
repaint();
}
//pink paint
}
//end of program............!!
- 06-21-2013, 11:22 AM #2
Re: problem in paint program
http://www.java-forums.org/forum-gui...w-members.html
BB Code List - Java Programming Forum - Learn Java Programming
How to ask questions the smart way
Please provide me with the code I want to complete my program
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 06-21-2013, 11:40 AM #3
Member
- Join Date
- Jun 2013
- Posts
- 2
- Rep Power
- 0
Re: problem in paint program
Dude I just provided the code for better understanding......!!
If you can help see to it else learn to reply properly...!!
- 06-21-2013, 12:59 PM #4
Re: problem in paint program
Contributed posts on this forum (per 21/6/2013):
db: 10,111
CompAkS: 2
So chill, and read what he says: "this is not a Codefactory". You ask for code to be created for you, which is not going to happen. If you didn't mean to ask for that, then you should have asked a different question.
If you want to maintain a selection, you could create a Selection class which inherits from Rectangle and is decorated with a (Buffered)Image. When the user uses the tool, store the x, y, width and height and store a copy of the subimage in the Selection object.
Sort of.
-
Re: problem in paint program
You're asking volunteers to help you on their free time. The onus is on you to ask your question properly and in a way that makes it easy for others to help you, and in a way that doesn't ask others to "Please provide me with the code I want to complete my program.....". If these rules aren't good for you, please feel free to ask this question elsewhere.
Similar Threads
-
Paint program
By aarti in forum AWT / SwingReplies: 14Last Post: 05-06-2011, 07:55 AM -
paint program
By dewdadamnthang in forum New To JavaReplies: 4Last Post: 03-30-2011, 12:40 PM -
Help with paint program
By michcan in forum Java 2DReplies: 1Last Post: 02-04-2011, 07:26 AM -
Java Paint Program problem (JPanel)
By KilKidd in forum Advanced JavaReplies: 6Last Post: 11-20-2010, 05:31 AM -
Paint Program Help
By ngiannini in forum AWT / SwingReplies: 12Last Post: 05-10-2010, 05:24 PM
Bookmarks