...

Help with java - graphics problem

Chud
01-07-2005, 09:00 PM
Hi guys,

The overall plan of the design is to have the 2 shapes saved to file, loaded from file, draggable around the screen, and resizeable and stuff....

but for now, i need to find out why the hell the jframe that's created only shows the buttons but not the shapes >.<

here's my code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import javax.swing.JApplet;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;


class FileReader
{

public void saveData(String [][] Saver)
{
try
{
BufferedWriter bw = null;
bw = new BufferedWriter(new FileWriter("test.txt"));

int i=0, j=0;
for(i=0;i<2;i++)
{
for(j=0;j<12;j++)
{
bw.write( Saver[i][j] + " " ) ;
}
bw.write("\n");
}
bw.flush();
bw.close();
}
catch (IOException W)
{
JOptionPane.showMessageDialog(null, "ERROR!");
}
catch (SecurityException seW)
{
JOptionPane.showMessageDialog (null, "Security Violation in saveData()");
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////

public String loadData()
{
String s = "";

try
{

RandomAccessFile r = new RandomAccessFile( "test.txt","r" );
String text="";

while( ( text = r.readLine() ) != null )
{
s+=text;
s+="\n";
}
}
catch( IOException R )
{
JOptionPane.showMessageDialog(null, "ERROR!" );
}

return s;
}

}




public class shapes extends JFrame
{

static FileReader File = new FileReader();
static String [][] Shape_Array = new String [2][12] ;
static String [][] Saver = new String [2][12] ;
static JButton Save_Button, Undo_Button, Load_Button, Update_Button;
static int t;

public shapes()
{
String s = File.loadData();

///////////////////////////////////////////////////////////////

int x=0,z=0;
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens())
{
for(x=0;x<2;x++)
{
for(z=0;z<12;z++)
{
Shape_Array[x][z]= st.nextToken();
Saver[x][z] = Shape_Array[x][z];
t=0; //reset t
t = Shape_Array[x][z].length();
t = t - 1;
Shape_Array[x][z] = Shape_Array[x][z].substring(0, t);
}
}
}

////////////////////////////////////////////////////////

System.out.print("Current data in the data.txt file: \n");
x=0;
z=0;
for(x=0;x<2;x++)
{
if(x==1)
{
System.out.print("\n");
}
for(z=0;z<12;z++)
{
System.out.print(Saver[x][z] + " ");
}
}

//////////////////////////////////////////////////////

Container c = getContentPane();
c.setLayout( new FlowLayout() );


Save_Button = new JButton( "Save Button");
Save_Button.setFont(new Font("Serif", Font.PLAIN, 30));
c.add( Save_Button, BorderLayout.CENTER);
Save_Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
save();
}
});


Load_Button = new JButton( "Load Button");
Load_Button.setFont(new Font("Serif", Font.PLAIN, 30));
c.add( Load_Button );
Load_Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Load();
}
});


Update_Button = new JButton( "Update Button");
Update_Button.setFont(new Font("Serif", Font.PLAIN, 30));
c.add( Update_Button);
Update_Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Update();
}
});


Undo_Button = new JButton( "Undo Button");
Undo_Button.setFont(new Font("Serif", Font.PLAIN, 30));
c.add( Undo_Button);
Undo_Button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Undo();
}
});


c.addMouseListener(new MouseAdapter()
{ public void mouseClicked(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
System.out.println("You clicked at position " + x + " by " + y + " in the window");
}
});

}


/////////////////////////////////////////////////////

public static void main(String[] args)
{
shapes window = new shapes();
window.setSize( 850, 650 );
window.show();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

////////////////////////////////////////////////////

public void Draw(Graphics g)
{

int Xpos1 = Integer.parseInt(Shape_Array[0][3]), Ypos1 = Integer.parseInt(Shape_Array[0][4]);
int Length1 = Integer.parseInt(Shape_Array[0][6]), Hieght1 = Integer.parseInt(Shape_Array[0][7]);
g.setColor(Color.red);
g.drawRect(Xpos1 , Ypos1, Length1, Hieght1);


int Xpos2 = Integer.parseInt(Shape_Array[1][3]), Ypos2 = Integer.parseInt(Shape_Array[1][4]);
int Length2 = Integer.parseInt(Shape_Array[1][6]), Hieght2 = Integer.parseInt(Shape_Array[1][7]);
g.setColor(Color.blue);
g.fillOval(Xpos2 , Ypos2, Length2, Hieght2);

}


/////////////////////////////////////////////////////////////

static public void Undo()
{
// will keep the previous data of the shape as mentioned in (1- f.) above.
JOptionPane.showMessageDialog(null, "Sorry, not working yet as there is nothing to undo" );
}


static public void Update()
{
// will updates the current shape. This is to be called upon every change of the object such as change a colour, change a position or change in size.
}

static public void Load()
{
// will updates the current shape. This is to be called upon every change of the object such as change a colour, change a position or change in size.
try
{
File.loadData();
JOptionPane.showMessageDialog(null, "Shapes loaded from data.txt" );
}
catch(Exception load)
{
JOptionPane.showMessageDialog(null, "ERROR! unable to Load." );
}
}

static public void save()
{
// will call a saveData in the FileReader. A button will be used to call this method.
try
{
File.saveData(Saver);
JOptionPane.showMessageDialog(null, "Shapes saved to data.txt" );
}
catch(Exception sav)
{
JOptionPane.showMessageDialog(null, "ERROR! unable to save." );
}
}
}


there's a lot of misc crap in my code too, had to put it all in to make sure that theres not one part of it thats breaking it somehow...

If i change name of Draw(Graphics g) to paint(Graphics g) then the objects are drawn but the buttons aren;t drawn straight away. If u click in theid location then they're then drawn... which confuses me as to why...

Any help on the matter is greatly appriciated....

thanx
-Chud



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum