Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to save in java??

Status
Not open for further replies.

nim180

IS-IT--Management
Aug 11, 2005
161
AU
hi everyone,

before i start forgive me im not a very good java programmer so this question might be simple for yee java experts.
I have a program which allows me to create different shapes at random. i also put a save button in called "SaveButton" and an Open button in called "OpenButton". I dont have a clue how to save or open in java, so what i want to do is when the random shapes appear i want to be able to click on the save button and save the file and then be able to to open the file again by clicking on open file????? does anyone know how to go about doing this, any help would be great. thanks

nim180
 
if u want i can paste the code for you??
 
this is the main code which i will be using


// Program randomly draws shapes.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;

public class TestDraw extends Frame implements ActionListener{


DrawToolCanvas myCanvas;
Label label;

private int x1;
private int x2;
private int y1;
private int y2;
public int value;

public TestDraw () {
//super("TestDraw");
// setSize (400,400);
// setVisible(true);
// initialize components
myCanvas = new DrawToolCanvas();
Panel south = getSouthPanel(myCanvas);

//configure frame
setTitle("Draw Shape Frame");
addWindowListener(closer);
add(getNorthPanel(), "North");
add(myCanvas, "Center");
add(south, "South");
setSize(400,400);
setLocation(100,100);
setVisible(true);

String user_number;


user_number = JOptionPane.showInputDialog("Enter the number of shapes you would like");

value = Integer.parseInt(user_number);

}

public void actionPerformed(ActionEvent e)
{
Button button = (Button)e.getSource();
String ac = button.getActionCommand();
int inc = 0;
if(ac.equals("Smaller"))
inc = -1;
if(ac.equals("Larger"))
inc = 1;
myCanvas.adjustSize(inc);
label.setText("size: " + myCanvas.size);
}

private Panel getNorthPanel()
{
Button
Smaller = new Button("Smaller"),
Larger = new Button("Larger");
Smaller.setActionCommand("Smaller");
Larger.setActionCommand("Larger");
Smaller.addActionListener(this);
Larger.addActionListener(this);
label = new Label("size: " + myCanvas.size, Label.CENTER);
Panel panel = new Panel(new GridLayout(1,0));
panel.add(Smaller);
panel.add(label);
panel.add(Larger);
return panel;
}

private Panel getSouthPanel(ActionListener l)
{
Panel myPanel = new Panel();
Button square = new Button("square");
square.setActionCommand("square");
Button circle = new Button("circle");
circle.setActionCommand("circle");
Button line = new Button("line");
line.setActionCommand("line");
Button randomshapes = new Button("random shapes");
randomshapes.setActionCommand("random shapes");
myPanel.add(square);
myPanel.add(circle);
myPanel.add(line);
myPanel.add(randomshapes);
square.addActionListener(l);
circle.addActionListener(l);
line.addActionListener(l);
randomshapes.addActionListener(l);
return myPanel;
}





public void paint (Graphics g) {


Graphics2D g2d = (Graphics2D) g;


int numShapes = 20;
int shapeType;

MyLine ml;
MyOval mo;
MyRectangle mr;

super.paint ( g2d );
// create and draw 20 shapes
for ( int i = 1; i <= numShapes; i++ ) {

// pick random shape type
shapeType = ( int ) ( Math.random() * 3 ) + 1;

// pick random coordinates
x1 = ( int ) ( Math.random() * 300 );
y1 = ( int ) ( Math.random() * 300 );
x2 = ( int ) ( Math.random() * 300 );
y2 = ( int ) ( Math.random() * 300 );

if (x2 < x1) {
int temp = x1;
x1 = x2;
x2 = temp;
}
if (y2 < y1) {
int temp = y1;
y1 = y2;
y2 = temp;
}

switch ( shapeType ) {

case 1:
/* Create a new MyLine object using the randomly chosen coordinates
as arguments to the constructor and assign the new object to a
MyLine variable */
ml = new MyLine(x1,y1,x2,y2);

/* Write a statement that tells the MyLine object to draw itself. */
ml.draw(g2d);
break;

case 2:
/* Create a new MyOval object using the randomly chosen coordinates
as arguments to the constructor and assign the new object to a
MyOval variable */
mo = new MyOval(x1,y1,x2,y2);

/* Write a statement that tells the MyOval object to draw itself. */
mo.draw(g2d);
break;

case 3:
/* Create a new MyRectangle object using the randomly chosen coordinates
as arguments to the constructor and assign the new object to a
Myrectangle variable */
mr = new MyRectangle(x1,y1,x2,y2);

/* Write a statement that tells the MyOval object to draw itself. */
mr.draw(g2d);
break;

} // end switch
} // end for
}

public static void main ( final String [] args ) {

TestDraw drawFrame = new TestDraw ();

//drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private WindowListener closer = new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
};

} //end class TestDraw



class DrawToolCanvas extends Canvas implements MouseListener, ActionListener
{


int x, y;
int size;
int delta;
boolean drawCircle;
boolean drawLine;
boolean drawSquare;
boolean drawRandom;
List shapes;

public DrawToolCanvas()
{
size = 100;
delta = 10;
x = -size;
y = -size;
drawCircle = false;
drawLine = false;
drawSquare = false;
drawRandom = false;
shapes = new ArrayList();
addMouseListener(this);

}

public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for(int j = 0; j < shapes.size(); j++)
{

Shape s = (Shape)shapes.get(j);
Color color = Color.red;
if(s instanceof Ellipse2D)
color = Color.green;
g2.setPaint(color);
g2.draw(s);

}


}
public void adjustSize(int inc)
{
size += inc * delta;
}

public void actionPerformed(ActionEvent event)
{

if (event.getActionCommand().equals("square")){
drawSquare = true;
drawCircle = false;
drawLine = false;
drawRandom = false;
}
else
if (event.getActionCommand().equals("line")){
drawLine = true;
drawCircle = false;
drawSquare = false;
drawRandom = false;
}
else
if (event.getActionCommand().equals("random shapes")){
drawRandom = true;
}
else

if (event.getActionCommand().equals("circle")){
drawCircle = true;
drawRandom = false;
}

}

public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
if(drawRandom)
{
int randomnumber = (int) Math.round((Math.random()*10)/3);
switch (randomnumber)
{
case 0:
shapes.add(new Ellipse2D.Double(p.x, p.y, size, size));
break;
case 1:
shapes.add(new Rectangle2D.Double(p.x, p.y, size, size));
break;
case 2:
shapes.add(new Line2D.Double(p.x, p.y, p.x+size, p.y+size));
break;
case 3:
shapes.add(new Ellipse2D.Double(p.x, p.y, size, size));
break;
}
}
else
if(drawCircle)
shapes.add(new Ellipse2D.Double(p.x, p.y, size, size));
else
if(drawSquare)
shapes.add(new Rectangle2D.Double(p.x, p.y, size, size));
else
if(drawLine)
shapes.add(new Line2D.Double(p.x, p.y, p.x+size, p.y+size));

repaint();

}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}
 
previously i had inputed a save and open button but took it out because i did not have a clue how to go bout using save and open in java
 
Posting long reams of code is not going to answer the previous questions, which I will attempt to reiterate :

How would you like to save the Shape objects ? As images or as coordinates ?

How do you envisage redisplaying the saved data ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hm.
You define MyOval, MyLine and MyRectangle, but later you add your new Shapes to the shapes, but Ellipse2D, Line2D and Rectangle2D.

Add your types.
Add a method 'write' to each of your types.
Add the save-button.

Your MyXYZs implement the common Interface MyShape?
It should contain two methods:
Code:
public paint (Graphics2D g2d);
public write ();

When save is hit, iterate over your list:
Code:
for (MyShape myShape : shapes)
{
        myShape.write ();
}
Start with
System.out.println ("Line;" + p1.x + ";" + p1.y + ...)
as Implementation - later you will write to a file.


seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top