PDA

View Full Version : Deviding the center of jfraem into two parts



HANI27
September 6th, 2010, 04:47 PM
Hi guys

I have a Java programm that allow the user to draw a doodle and save it. the program also, allow the user to have a look to some templates such as Camel, Bear in order to help him/her find out how to draw them.
the program works fine but my problem is that when the user press the button to show the templates, the templates will be displayed in external window not in my JFrame window. What I have tried to do is to divide the center of my JFrame into two parts: one for drawing and one for showing the templates.

I have been trying this way many times and I have been searching around but I could not find how I can do it.
Here is my code of painting and showing the templates. It will be very very very helpfull if you can help. I have started hate the JAVA BECAUSE OF THIS CODE :(


import java.awt.*;

import java.awt.event.*;


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.IOException;

import java.io.InputStreamReader;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.List;

import java.util.Vector;

import java.awt.Point;

import javax.imageio.ImageIO;

import javax.swing.event.*;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class PaintPrush extends JFrame implements MouseListener, ActionListener, MouseMotionListener{

int xvalue = -10, yvalue = -10;
Painter p ;

private Panel top, drawingArea, bottom;

private JTextField text1, text2, text3, text4, text5;

private JLabel myLabel;

private JComboBox templates;

private JButton open, msPaint, show, draw, clear, save, exit;

private UsersAccount ua;
BufferedImage image;
Process process;
File inputFile;
List<Point> displayList = new ArrayList<Point>();
String pathname = "data.dat";


public PaintPrush (UsersAccount a){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));

addMouseListener(this);
addMouseMotionListener(this);
setDefaultCloseOperation (EXIT_ON_CLOSE);
setTitle ("Drwoing Tool");

setSize (1000,600);
ua = a;
layoutComponents();
}

private void layoutComponents(){
setLayout(new BorderLayout());
top = new Panel();
open = new JButton("Open My doodle");
top.add(open);
open.addActionListener(this);

msPaint = new JButton("Open msPaint");
top.add(msPaint);
msPaint.addActionListener(this);

draw = new JButton("Creat my doodle");
top.add(draw);
draw.addActionListener(this);


//line = new JButton("Line");

//top.add(line);

//line.addActionListener(this);


clear = new JButton("Clear");
top.add(clear);
clear.addActionListener(this);

save = new JButton("Save my doodle");
top.add(save);
save.addActionListener(this);

exit = new JButton("Exit");
top.add(exit);
exit.addActionListener(this);


add(top,BorderLayout.NORTH);

bottom = new Panel();
myLabel = new JLabel("Select template");
bottom.add(myLabel);

templates = new JComboBox ();
templates.addItem("Armadillo");
templates.addItem("Bear");
templates.addItem("Camel");
templates.addItem("Cartons");
templates.addItem("Dragonhead");
templates.addItem("Fish");
templates.addItem("Hedgehog");
templates.addItem("Historic Ornament Rosettes");
templates.addItem("Leaf forms");
templates.addItem("Parrot");
templates.addItem("Rose");
templates.addItem("Sheep");
templates.addItem("Stick");
templates.addItem("Swan");





templates.addActionListener(this);
bottom.add(templates);
show = new JButton("Show the template");
bottom.add(show);
show.addActionListener(this);


add(bottom,BorderLayout.SOUTH);
//drawingArea = new JPanel();


//drawingArea.setPreferredSize(new Dimension(400, 0));

//drawingArea.setBackground(Color.WHITE);



 
//add(drawingArea,BorderLayout.CENTER);

setVisible(true);

}


public void actionPerformed (ActionEvent e){

String template = templates.getSelectedItem().toString();
if (e.getSource()== open)
{
String name = JOptionPane.showInputDialog(null, "Enter your name");
try {
FileInputStream fis = new FileInputStream(name+".dat");
ObjectInputStream ois = new ObjectInputStream(fis);
displayList = (Vector) (ois.readObject());
ois.close();
fis.close();
repaint();
} catch (Exception ex) {
System.out.println("Trouble reading display list vector");

}
}
else if (e.getSource()== msPaint)
{
try {
Runtime.getRuntime().exec("C:\\Windows\\System32\\msPaint.exe");
} catch (IOException e1) {
// TODO Auto-generated catch block

e1.printStackTrace();
}
}
else if (e.getSource()== show)
{
// File input = new File(template+".jpg");

// try {

// image = ImageIO.read(input);


//} catch (IOException e1) {

// TODO Auto-generated catch block

//e1.printStackTrace();

// }


try {
process = new ProcessBuilder("mspaint",template+".jpg").start();
} catch (IOException e1) {
//TODO Auto-generated catch block

e1.printStackTrace();
}


}
//else if (e.getSource()== draw)

//{

// addMouseMotionListener


//(


// new MouseMotionAdapter()


// {//




// }


//);

// setSize( 500,500 );


// setVisible( true );



//}

else if (e.getSource()== save)
{
try {
FileOutputStream fos = new FileOutputStream(ua.name+".dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(displayList);
oos.flush();
oos.close();
fos.close();
} catch (Exception ex) {
System.out.println("Trouble writing display list vector");
}
}


else if (e.getSource()== clear)
{
displayList = new Vector();
repaint();
}
else if (e.getSource()== exit)
{
JOptionPane.showMessageDialog(null, "Good Bay","Result summary", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}

public void paint ( Graphics g )

{
g.setColor(Color.white);
//System.out.print(getSize().height);

// g.fillRect(0, 0, 600, 600);

// g.setColor(Color.gray);

// g.fillRect(getSize().width-400,getSize().height-550, 600, 600);


g.fillRect(0, 0, getSize().width, getSize().height);
g.setColor(Color.red);
int i = 0;
while (i < displayList.size()) {
Point p0 = (Point) (displayList.get(i++));

g.fillOval(p0.x, p0.y, 10,10);

//g.drawImage( image, 50, 50, null);

}
//g.drawOval( xvalue, yvalue, 10, 10 );

// g.drawLine(prevX, prevY, x, y);


}

@Override

public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub


}

@Override

public void mouseEntered(MouseEvent event) {
// TODO Auto-generated method stub


}

@Override

public void mouseExited(MouseEvent event) {
// TODO Auto-generated method stub


}

@Override

public void mousePressed(MouseEvent event) {
// TODO Auto-generated method stub

Point p = new Point(event.getX(), event.getY());
displayList.add(p);

}

@Override

public void mouseReleased(MouseEvent event) {
// TODO Auto-generated method stub

Point p = new Point(event.getX(), event.getY());
displayList.add(p);
repaint();
}

public void mouseDragged( MouseEvent event )

{
Point p = new Point(event.getX(), event.getY());
displayList.add(p);
repaint();
//xvalue = event.getX();



// yvalue = event.getY();

// repaint();



}

@Override

public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub


}
}