|
Registered User
Join Date: Aug 2004
Posts: 1
|
2D graphic repaint logic
I encounter some problem in the LineGraph.java.
LineGraph.java will drawString in the Graph component.It have the predefined value in some other class(PMGraph2.java)
But i failed to pass the new value from ValueInput.java into the LineGraph.java to repaint.
Can anyone pls advice ...URGent
//LineGraph.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.util.*;
public class LineGraph extends JPanel implements Printable
{
private GraphValue graphValue;
private ArrayList entityList;
private GraphFrame gf;
private double scaleX;
private double scaleY;
private Dimension size;
private JTextField jtfOne;
private String custName;
public LineGraph(GraphValue graphValue, GraphFrame gf)
{
this.graphValue = graphValue;
this.gf = gf;
System.out.println("Here");
Dimension d = LineGraph.this.getSize();
entityList = (ArrayList)graphValue.getEntityList();
scaleX = 1;
scaleY = 1;
size = this.getSize();
size = new Dimension(490,440);
this.addComponentListener(new ComponentListener()
{
public void componentResized(ComponentEvent e)
{
Dimension d = LineGraph.this.getSize();
scaleY = d.getHeight()/size.getHeight();
scaleX = d.getWidth()/size.getWidth();
refresh();
System.out.println("This is ScaleY"+scaleY);//comment line
System.out.println("This is ScaleX"+scaleX);//Comment Line
}
public void componentMoved(ComponentEvent e){}
public void componentShown(ComponentEvent e){}
public void componentHidden(ComponentEvent e){}
});
}
public void paintComponent(Graphics g)
{
//Create a Graphics2D object
final Graphics2D g2 = (Graphics2D) g.create();
g2.scale(scaleX,scaleY);
Font font = g2.getFont();
//Calculate the highest height possible
double height = 0.0;
for(int i=0;i<entityList.size();i++)
{
GraphEntity gv = (GraphEntity)entityList.get(i);
if(gv.getValue()>height)
{
height = gv.getValue();
System.out.println("height"+height);
System.out.println("height.getValue"+gv.getValue() );
}
}
int largestValue = (int)height;
height = 380/height;
//height = 500/height;
//calculate the biggest gap posisble between the entity
int gap = 330/entityList.size();
//calculate color tone allowed to ensure that color used does not exceed 255 for GBcolor
int colorIndex = 255/entityList.size();
//Declare the x and y axis
int xAxis = 60;
//int yAxis = 400;//(default)
int yAxis = 500;
int y1 = yAxis;//500
int x1 = xAxis;//60
int y2 = yAxis;//120
int x2 = xAxis;//390
int z1 = 232;// for the yAxis Indicator Line
int z2 = 240;//for the yAxis Arrow Point to start drawing
int z3 = 220;//for the xAxis indicator line
int a1 = xAxis+170;
int a2 = 390;
int a3 = xAxis+380;//a3=440
for(int i=0;i<entityList.size();i++)
{
GraphEntity gv = (GraphEntity)entityList.get(i);
int drawFrom = (int)(gv.getValue()*height);
System.out.println("at loop of i"+gv.getValue());
System.out.println("The value of drawForm is :"+drawFrom);
g2.setColor(Color.black);
x2 += gap;//vectors object line part
y2 = yAxis - drawFrom;
//Draw the Object Jointing Line
/*if(i!=0)
{
g2.drawLine(x1,y1,x2,y2);
}*/
//Get Object Value and Name to plot and draw on graph
g2.setFont(new Font("Times New Roman",Font.PLAIN,10));
g2.drawString(""+gv.getValue(), x2+3, y2+10 );
g2.drawString(gv.getName(), x2+3, y2-5 );
g2.setFont(font);//make use of actionlistener to set font..to be edited
g2.setColor(new Color(255,(255-colorIndex*(1+i)),0));
g2.fillOval(x2-2,y2-2,5,5);
//g2.fillOval(240, 230, 5, 5);
//Draw the REd color Dotted line to the objects
/*for(int dot=xAxis ; dot<(x2);dot++)
{
//g2.drawLine(dot, (yAxis-drawFrom) ,(dot+2), (yAxis-drawFrom));
g2.drawLine(dot, (240-drawFrom) ,(dot+2), (100-drawFrom));
dot+=4;
}*/
g2.fillOval(280,280,1,1);
//Draw the Red color line for the vectors
int zeroXaxis=240;
int zeroYaxis=230;
g2.drawLine(zeroXaxis, zeroYaxis ,100, 130);//for the (-1, 1) axis
g2.drawLine(zeroXaxis, zeroYaxis ,380, 130);//for the ( 1, 1) axis
g2.drawLine(zeroXaxis, zeroYaxis ,100, 330);//for the (-1,-1) axis
g2.drawLine(zeroXaxis, zeroYaxis ,380, 330);//for the ( 1, -1) axis
g2.drawLine(380, 130 ,374, 130);//for the (-1, 1) axis
g2.drawLine(380, 130 ,380, 134);
/*g2.drawLine(zeroXaxis, zeroYaxis,300,180);
int test1= 300;
int test2= 180;
int test3= test1-6;
int test4= test2+6;
g2.drawLine(test1, test2 ,test3, test2);
g2.drawLine(test1, test2 ,test1, test4);*/
int testA=220;
int testB=260;
/*if (240<testA && 250>testB)
{
//User enter the testA and testB
g2.drawLine(zeroXaxis, zeroYaxis,testA,testB);//main vector line
int f1= testA;// f1 = testA = 250
int f2= testB;// f2 = testB = 180
int f3= f1-6; // f3 = 246
int f4= f2+6; // f4 = 186
g2.drawLine(f1, f2 ,f3, f2);//the left side of arrow Line
g2.drawLine(f1, f2 ,f1, f4);//the right side of arrow Line
}*/
/* if (240>testA && 250>testB)
{
g2.drawLine(zeroXaxis, zeroYaxis,testA,testB);//main vector line
int f1= testA;// f1 = testA = 220
int f2= testB;// f2 = testB = 150
int f3= f1+6; // f3 = 226
int f4= f2+6; // f4 = 156
g2.drawLine(200,140,200,146);
g2.drawLine(200,140,206,140);
//g2.drawLine(f1, f2 ,f4, f2);//the arrow Line
//g2.drawLine(f1, f2 ,f4, f3);//the arrow Line
}*/
/*if (240>testA && 250>testB)
{
g2.drawLine(zeroXaxis, zeroYaxis,testA,testB);//main vector line
int f1= testA;// f1 = testA = 220
int f2= testB;// f2 = testB = 150
int f3= f1+6; // f3 = 226
int f4= f2+6; // f4 = 156
g2.drawLine(200,140,200,146);
g2.drawLine(200,140,206,140);*/
x1 = x2;
y1 = y2;
//g2.drawString(""+("1"), x2+7, y2+12 );//object Value draw on graph
{
g2.setColor(Color.blue);
g2.setFont(new Font("Times New Roman",Font.PLAIN,10));
g2.drawString(""+(" 1"), 378, 250 );//Value draw on graph on X-Axis
g2.drawString(""+("-1"), 98, 250 );//Value Value draw on X-Axis
g2.drawString(""+(" 1"), 245, 130 );//Value Value draw on Y-Axis
g2.drawString(""+("-1"), 245,332 );//Value Value draw on Y-Axis
}
}
g2.setColor(Color.black);
int largestMarking = (int)(largestValue*height);
for(int i=1;i<6;i++)
{
int marking = (int)(largestMarking*i/2);
//Y-Axis Indicator Line
g2.drawLine(235, 130, 245, 130); //for the +1 side
g2.drawLine(235, 330, 245, 330); //for the -1 side
//X-Axis Indicator Line
g2.drawLine(100, 235, 100, 225);//for the +1 side
g2.drawLine(380, 235, 380, 225);//for the -1 side
//Do not deltree this line
//g2.drawString(""+(largestValue*i/5), xAxis-40, (yAxis-marking+4));//vectors indicating point
}
//Drawing the x and y axis
g2.drawLine(60,230,450,230);//X-Axis Main Line
g2.drawLine(240,80,240,400);//Y-Axis Main Line
//The Arrow Line for the Y-Axis
g2.drawLine(240,80,238,90);
g2.drawLine(240,80,242,90);
//The Arrow Line for the X-Axis
g2.drawLine(450,230,440,226);
g2.drawLine(450,230,440,234);
//Draw the name of the graph which is the Y Axis
//g2.rotate(-1.59, xAxis-50,yAxis-200);//Makes The rotation of the Title
//g2.drawString(graphValue.getName(), xAxis-50,yAxis-200);
//Indicate the Main Graph Title
//g2.setFont(new Font("Times New Roman",Font.PLAIN,14));
g2.drawString(graphValue.getEntityName(),180,50);
//Get the Titles and Draw on the graph
g2.setFont(new Font("Times New Roman",Font.PLAIN,8));
g2.drawString(graphValue.getTitle1(), 230,70);
g2.drawString(graphValue.getTitle2(), 400,240);
g2.drawString(graphValue.getTitle3(), 240,420);
g2.drawString(graphValue.getTitle4(), 70,240);
}
public void printGraph()
{
scaleX = 1;
scaleY = 1;
PrinterJob printJob = PrinterJob.getPrinterJob();
boolean print = printJob.printDialog();
if(print)
{
printJob.setPrintable(LineGraph.this);
try { printJob.print(); } catch (Exception PrintException) {}
}
toScale();
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
if (pageIndex >= 1)
{
return Printable.NO_SUCH_PAGE;
}
g.translate(80, 100);
paintComponent (g);
return Printable.PAGE_EXISTS;
}
public void toScale()
{
Dimension d = LineGraph.this.getSize();
scaleY = d.getHeight()/size.getHeight();
scaleX = d.getWidth()/size.getWidth();
refresh();
}
//refresh is called to ask the main graph frame to repaint
public void refresh()
{
gf.refresh();
}
public void refreshLg()
{
repaint();
}
public Graphics getG()
{
Graphics graphics = this.getGraphics();
graphics.setColor(new Color(102,102,153));
return graphics;
}
}
/////////////////////////////////////////////////////////////////
//PMGraph2.java
import java.awt.*; //for layout manager
import java.awt.event.*; //for event handling
import java.awt.print.*; //for printing
import javax.swing.*; //for swing components
import javax.swing.event.*;
public class PMGraph2 extends JFrame /*implements ActionListener*/
{
/**Declare Variables*/
private JMenu jmFile, jmFormat, jmFontSubMenu, jmSizeSubMenu, jmColourSubMenu, jmTypeSubMenu, jmStyleSubMenu, jmHelp;
private JMenuItem jmiNew, jmiOpen, jmiSave, jmiSaveAs, jmiPrint, jmiExit,
jmiFont,
jmi10, jmi12, jmi14, jmi16, jmi18, jmi20,
jmiColour,
jmiRed, jmiBlue, jmiGreen, jmiDefault,
jmiType,
jmiStyle,
jmiBold, jmiItalic, jmiUnderline, jmiNormal,
jmiAbout, jmiFAQ;
private JPanel jpGraph, jpEntry;
private JButton jbtNew, jbtOpen, jbtSave, jbtPrint, jbtBold, jbtItalic, jbtUnderline, jbtNormal, jbtLeft, jbtCenter, jbtRight, jbtJustify;
private LineGraph lineGraph;
private JToolBar toolbar;
private JComboBox fontCombo, fontSizeCombo;
//private GraphFrame graphFrame;
//private ImagePanel graphPanel;
private JMenuBar menuBar = new JMenuBar();
private PageFormat mPageFormat;
private Dimension size;
private double scaleX, scaleY;
/**Main Method*/
public static void main(String[] args)
{
try {
//Normal Java Look and Feel
/**UIManager.setLookAndFeel(
UIManager.getCrossPlatformLookAndFeelClassName()); */
//GTK Look and Feel
/**UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); */
//Motif Look and Feel
/**UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.motif.MotifLookAndFeel"); */
//Windows Look and Feel
/**UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFee l");*/
}
catch (Exception e) { }
PMGraph2 frame = new PMGraph2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.pack();
frame.setSize(700,550);
frame.setVisible(true);
//Container contentPane = frame.getContentPane();
}//end of main method
/**Default Constructor*/
public PMGraph2()
{
super("SwingPrinter v1.0");
PrinterJob pj = PrinterJob.getPrinterJob();
mPageFormat = pj.defaultPage();
Paper paper = new Paper();
double margin = 36; //half inch
paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2,
paper.getHeight() - margin * 2);
mPageFormat.setPaper(paper);
setTitle("Perceptual Mapping");
//Initialise LineGraph.java
//lineGraph = new LineGraph();
//Initialise JToolBar
toolbar = new JToolBar();
/**File Menu*/
jmFile = new JMenu("File");
jmFile.add(jmiNew = new JMenuItem("New"));
jmFile.add(jmiOpen = new JMenuItem("Open")); //Page 751 of Java Text
jmFile.add(jmiSave = new JMenuItem("Save"));
jmFile.add(jmiSaveAs = new JMenuItem("Save As"));
jmFile.addSeparator();
jmFile.add(jmiPrint = new JMenuItem("Print"));
jmFile.add(new FilePageSetupAction()).setAccelerator(KeyStroke.ge tKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK | Event.SHIFT_MASK));
jmFile.addSeparator();
jmFile.add(jmiExit = new JMenuItem("Exit"));
//Set Keyboard Accelerators
jmiNew.setAccelerator(KeyStroke.getKeyStroke(KeyEv ent.VK_N, ActionEvent.CTRL_MASK));
jmiOpen.setAccelerator(KeyStroke.getKeyStroke(KeyE vent.VK_O, ActionEvent.CTRL_MASK));
jmiSave.setAccelerator(KeyStroke.getKeyStroke(KeyE vent.VK_S, ActionEvent.CTRL_MASK));
jmiPrint.setAccelerator(KeyStroke.getKeyStroke(Key Event.VK_P, ActionEvent.CTRL_MASK));
//Assign image
jmiNew.setIcon(new ImageIcon("image/new.gif"));
jmiOpen.setIcon(new ImageIcon("image/open.gif"));
jmiSave.setIcon(new ImageIcon("image/save.gif"));
jmiSaveAs.setIcon(new ImageIcon("image/saveas.gif"));
jmiPrint.setIcon(new ImageIcon("image/print.gif"));
//set Keyboard Mnemonics
jmFile.setMnemonic('F');
jmiNew.setMnemonic('N');
jmiOpen.setMnemonic('O');
jmiSave.setMnemonic('S');
jmiSaveAs.setMnemonic('A');
jmiPrint.setMnemonic('P');
jmiExit.setMnemonic('X');
/**Format Menu*/
jmFormat = new JMenu("Format");
jmFormat.add(jmFontSubMenu = new JMenu("Font"));
jmFormat.add(jmFontSubMenu);
//Font Size SubMenu
jmFontSubMenu.add(jmSizeSubMenu = new JMenu("Size"));
jmSizeSubMenu.add(jmi10 = new JCheckBoxMenuItem("10", true));
jmSizeSubMenu.add(jmi12 = new JCheckBoxMenuItem("12"));
jmSizeSubMenu.add(jmi14 = new JCheckBoxMenuItem("14"));
jmSizeSubMenu.add(jmi16 = new JCheckBoxMenuItem("16"));
jmSizeSubMenu.add(jmi18 = new JCheckBoxMenuItem("18"));
jmSizeSubMenu.add(jmi20 = new JCheckBoxMenuItem("20"));
ButtonGroup btgSize = new ButtonGroup();
btgSize.add(jmi10);
btgSize.add(jmi12);
btgSize.add(jmi14);
btgSize.add(jmi16);
btgSize.add(jmi18);
btgSize.add(jmi20);
//Font Colour SubMenu
jmFontSubMenu.add(jmColourSubMenu = new JMenu("Colour"));
jmColourSubMenu.add(jmiRed = new JCheckBoxMenuItem("Red", false));
jmColourSubMenu.add(jmiGreen = new JCheckBoxMenuItem("Green", false));
jmColourSubMenu.add(jmiBlue = new JCheckBoxMenuItem("Blue", false));
jmColourSubMenu.addSeparator();
jmColourSubMenu.add(jmiDefault = new JCheckBoxMenuItem("Default", true));
ButtonGroup btgColour = new ButtonGroup();
btgColour.add(jmiRed);
btgColour.add(jmiGreen);
btgColour.add(jmiBlue);
btgColour.add(jmiDefault);
//Font Type SubMenu
jmFontSubMenu.add(jmTypeSubMenu = new JMenu("Type"));
//Font Style SubMenu
jmFontSubMenu.add(jmStyleSubMenu = new JMenu("Style"));
jmStyleSubMenu.add(jmiBold = new JCheckBoxMenuItem("Bold", false));
jmStyleSubMenu.add(jmiItalic = new JCheckBoxMenuItem("Italic", false));
jmStyleSubMenu.add(jmiUnderline = new JCheckBoxMenuItem("Underline", false)); //Page 138 of 2D
jmStyleSubMenu.addSeparator();
jmStyleSubMenu.add(jmiNormal = new JCheckBoxMenuItem("Default", true));
/*ButtonGroup btgStyle = new ButtonGroup();
btgStyle.add(jmiBold);
btgStyle.add(jmiItalic);
btgStyle.add(jmiUnderline);
btgStyle.add(jmiNormal);*/
//Set Keyboard Accelerators
jmiBold.setAccelerator(KeyStroke.getKeyStroke(KeyE vent.VK_B, ActionEvent.CTRL_MASK));
jmiItalic.setAccelerator(KeyStroke.getKeyStroke(Ke yEvent.VK_I, ActionEvent.CTRL_MASK));
jmiUnderline.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_U, ActionEvent.CTRL_MASK));
//Assign image
jmFontSubMenu.setIcon(new ImageIcon("image/font.gif"));
jmiBold.setIcon(new ImageIcon("image/bold.gif"));
jmiItalic.setIcon(new ImageIcon("image/italic.gif"));
jmiUnderline.setIcon(new ImageIcon("image/underline.gif"));
jmiNormal.setIcon(new ImageIcon("image/normal.gif"));
//Set Keyboard Mnemonics
jmFormat.setMnemonic('O');
jmFontSubMenu.setMnemonic('F');
jmSizeSubMenu.setMnemonic('S');
jmColourSubMenu.setMnemonic('C');
jmTypeSubMenu.setMnemonic('T');
jmStyleSubMenu.setMnemonic('Y');
jmiRed.setMnemonic('R');
jmiGreen.setMnemonic('G');
jmiBlue.setMnemonic('B');
jmiDefault.setMnemonic('D');
jmiBold.setMnemonic('B');
jmiItalic.setMnemonic('I');
jmiUnderline.setMnemonic('U');
jmiNormal.setMnemonic('D');
/**Help Menu*/
jmHelp = new JMenu("Help");
jmHelp.add(jmiAbout = new JMenuItem("About"));
jmHelp.add(jmiFAQ = new JMenuItem("FAQ"));
jmiAbout.setIcon(new ImageIcon("image/about.gif"));
jmiFAQ.setIcon(new ImageIcon("image/help.gif"));
jmHelp.setMnemonic('H');
jmiAbout.setMnemonic('A');
jmiFAQ.setMnemonic('F');
//Place the menu bar on the window
menuBar.add(jmFile);
menuBar.add(jmFormat);
menuBar.add(jmHelp);
setJMenuBar(menuBar);
/** Short Cut Buttons*/
jbtNew = new JButton();
jbtOpen = new JButton();
jbtSave = new JButton();
jbtPrint = new JButton();
jbtBold = new JButton();
jbtItalic = new JButton();
jbtUnderline = new JButton();
jbtNormal = new JButton();
jbtLeft = new JButton();
jbtCenter = new JButton();
jbtRight = new JButton();
jbtJustify = new JButton();
jbtNew.setIcon(new ImageIcon("image/new.gif"));
jbtOpen.setIcon(new ImageIcon("image/open.gif"));
jbtSave.setIcon(new ImageIcon("image/save.gif"));
jbtPrint.setIcon(new ImageIcon("image/print.gif"));
jbtBold.setIcon(new ImageIcon("image/bold.gif"));
jbtItalic.setIcon(new ImageIcon("image/italic.gif"));
jbtUnderline.setIcon(new ImageIcon("image/underline.gif"));
jbtNormal.setIcon(new ImageIcon("image/normal.gif"));
jbtLeft.setIcon(new ImageIcon("image/left.gif"));
jbtCenter.setIcon(new ImageIcon("image/center.gif"));
jbtRight.setIcon(new ImageIcon("image/right.gif"));
jbtJustify.setIcon(new ImageIcon("image/justify.gif"));
jbtNew.setToolTipText("New Data");
jbtOpen.setToolTipText("Open");
jbtSave.setToolTipText("Save");
jbtPrint.setToolTipText("Print");
jbtBold.setToolTipText("Bold");
jbtItalic.setToolTipText("Italic");
jbtUnderline.setToolTipText("Underline");
jbtNormal.setToolTipText("Normal");
jbtLeft.setToolTipText("Align Left");
jbtCenter.setToolTipText("Center");
jbtRight.setToolTipText("Align Right");
jbtJustify.setToolTipText("Justify");
/**JComboBox - fontCombo & fontSizeCombo*/
fontCombo = new JComboBox();
fontCombo.addItem("Helvetica");
fontCombo.addItem("Palatino");
fontCombo.addItem("Courier");
fontCombo.addItem("Times");
fontCombo.addItem("Times-Roman");
fontSizeCombo = new JComboBox();
fontSizeCombo.addItem("10");
fontSizeCombo.addItem("12");
fontSizeCombo.addItem("14");
fontSizeCombo.addItem("16");
fontSizeCombo.addItem("18");
fontSizeCombo.addItem("20");
/**Add Buttons and ComboBoxes into JToolBar*/
toolbar.add(jbtNew);
toolbar.add(jbtOpen);
toolbar.add(jbtSave);
toolbar.addSeparator();
toolbar.add(jbtPrint);
toolbar.addSeparator();
toolbar.add(fontCombo);
toolbar.add(fontSizeCombo);
toolbar.addSeparator();
toolbar.add(jbtBold);
toolbar.add(jbtItalic);
toolbar.add(jbtUnderline);
toolbar.add(jbtNormal);
toolbar.addSeparator();
toolbar.add(jbtLeft);
toolbar.add(jbtCenter);
toolbar.add(jbtRight);
toolbar.add(jbtJustify);
//toolbar.addSeparator();
/**
//Set Buttons Y Alignment
jbtNew.setAlignmentY(0.5f);
jbtOpen.setAlignmentY(0.5f);
jbtSave.setAlignmentY(0.5f);
jbtPrint.setAlignmentY(0.5f);
jbtBold.setAlignmentY(0.5f);
jbtItalic.setAlignmentY(0.5f);
jbtUnderline.setAlignmentY(0.5f);
jbtNormal.setAlignmentY(0.5f);
jbtLeft.setAlignmentY(0.5f);
jbtCenter.setAlignmentY(0.5f);
jbtRight.setAlignmentY(0.5f);
jbtJustify.setAlignmentY(0.5f);
//Set Buttons X Alignment
jbtNew.setAlignmentX(0.5f);
jbtOpen.setAlignmentX(0.5f);
jbtSave.setAlignmentX(0.5f);
jbtPrint.setAlignmentX(0.5f);
jbtBold.setAlignmentX(0.5f);
jbtItalic.setAlignmentX(0.5f);
jbtUnderline.setAlignmentX(0.5f);
jbtNormal.setAlignmentX(0.5f);
jbtLeft.setAlignmentX(0.5f);
jbtCenter.setAlignmentX(0.5f);
jbtRight.setAlignmentX(0.5f);
jbtJustify.setAlignmentX(0.5f);
*/
//Set JComboBox Preferred Size
fontCombo.setMaximumSize(fontCombo.getPreferredSiz e());
fontSizeCombo.setMaximumSize(fontSizeCombo.getPref erredSize());
//toolbar.putClientProperty("JToolBar.isRollover", Boolean.TRUE);
/**Implementation of Graph Panel*/
jpGraph = new JPanel(new GridLayout(1,1));
JDesktopPane jDesktopPane = new JDesktopPane();
jpGraph.add(jDesktopPane);
//All graphs value
GraphValue gv = new GraphValue("Y-Axis","X-Axis","Title2","title3","title5");
/*gv.addEntity("Honda",320);
gv.addEntity("Mitsubishi",315);
gv.addEntity("Mini Cooper",400);
gv.addEntity("BMW",300);
gv.addEntity("Toyota Corona",350);
gv.addEntity("Mazda",340);
gv.addEntity("Taxi",380);
gv.addEntity("0",0);*/
gv.addEntity("1",10);
gv.addEntity("2",20);
// gv.addEntity("3",30);
jDesktopPane.add(new GraphFrame(gv), JLayeredPane.DEFAULT_LAYER);
show();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(toolbar, BorderLayout.NORTH);
getContentPane().add(jpGraph, BorderLayout.CENTER);
//Register listeners
//jmiNew.addActionListener(this);
jmiBold.addActionListener(new FontStyle());
jmiItalic.addActionListener(new FontStyle());
jmiUnderline.addActionListener(new FontStyle());
jmiNormal.addActionListener(new FontStyle2());
jmiPrint.addActionListener(new Printing());
jbtPrint.addActionListener(new Printing());
repaint();
}//end of default constructor PMFrame()
public class FontStyle implements ActionListener{
public void actionPerformed(ActionEvent e){
if(jmiBold.isSelected() || jmiItalic.isSelected() || jmiUnderline.isSelected())
{
jmiNormal.setSelected(false);
//styles.
}
}
}//end of FontStyle()
public class FontStyle2 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(jmiNormal.isSelected())
{
jmiNormal.setSelected(true);
jmiBold.setSelected(false);
jmiItalic.setSelected(false);
jmiUnderline.setSelected(false);
}
}
}//end of FontStyle2()
public class Printing implements ActionListener{
public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
if ((e.getSource() instanceof JMenuItem)||(e.getSource() instanceof JButton))
{
System.out.println("PMGraph2 Printing()");
//lineGraph.printGraph();
PrinterJob pj = PrinterJob.getPrinterJob();
//ComponentPrintable cp = new ComponentPrintable(jpGraph);
PaperScale();
//pj.setPrintable(pj, mPageFormat);
if (pj.printDialog())
{
try
{
pj.print();
}
catch (PrinterException pe)
{
System.out.println(pe);
}
}
}
}
}//end of Printing()
/**protected void center()
{
System.out.println("center()");
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension us = getSize();
int x = (screen.width - us.width) / 2;
int y = (screen.height - us.height) /2;
setLocation(x,y);
}//end of center()*/
//class for shrinking the graph to fit the paper
protected void PaperScale()
{
//int gWidth = jpGraph.getWidth();
//int gHeight = jpGraph.getHeight();
size = this.getSize();
size = new Dimension(490,440);
scaleX = 1;
scaleY = 1;
//toScale();
if ((mPageFormat.getImageableX() < jpGraph.getWidth()) && (mPageFormat.getImageableY() < jpGraph.getHeight()))
{
System.out.println("Am I here @ the if statement?");
//Dimension paperSize = new Dimension(mPageFormat.getImageableX(), mPageFormat.getImageableY());
toScale();
//jpGraph.setSize(490,440);
repaint();
}
}
public void toScale()
{
System.out.println("toScale()");
Dimension d = this.getSize();
scaleY = d.getHeight()/size.getHeight();
scaleX = d.getWidth()/size.getWidth();
//center();
//lineGraph.gf.refresh();
}
//class for PageSetup options
public class FilePageSetupAction extends AbstractAction
{
public FilePageSetupAction()
{
super("Page Setup");
}
public void actionPerformed(ActionEvent e)
{
PrinterJob pj = PrinterJob.getPrinterJob();
mPageFormat = pj.pageDialog(mPageFormat);
}
}//end of FilePageSetupAction
}//end of Graph
//////////////////////////////////////////////////////////
//valueInput.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.util.*;
public class ValueInput extends JPanel implements Printable
{
private GraphValue graphValue;
private ArrayList entityList;
private GraphFrame gf;
private double scaleX;
private double scaleY;
private Dimension size;
private JLabel jl1, jl2, jl3, jl4, jl5;
private JTextField jtf1, jtf2, jtf3, jtf4, jtf5,jtf6;
private JButton jbtnEnter,jbtnClear;
private Tone tone;
private String custName;
private LineGraph lg;
public ValueInput(GraphValue graphValue, GraphFrame gf)
{
this.graphValue = graphValue;
this.gf = gf;
entityList = (ArrayList)graphValue.getEntityList();
scaleX = 1;
scaleY = 1;
size = this.getSize();
size = new Dimension(490,440);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(8, 1));
p1.add(new JLabel("Enter Title of Graph: "));
p1.add(jtf1 = new JTextField(10));
p1.add(new JLabel("Enter Title of T1 axis: "));
p1.add(jtf2 = new JTextField(10));
p1.add(new JLabel("Enter Title of T2 axis: "));
p1.add(jtf3 = new JTextField(10));
p1.add(new JLabel("Enter Title of T3 axis: "));
p1.add(jtf4 = new JTextField(10));
p1.add(new JLabel("Enter Title of T4 axis: "));
p1.add(jtf5 = new JTextField(10));
p1.add(new JLabel("Enter Value of Vectors: "));
p1.add(jtf6 = new JTextField(10));
p1.add(jbtnEnter = new JButton("Enter"));
p1.add(jbtnClear = new JButton("Clear"));
jbtnEnter.addActionListener(new Enter());
this.add(p1, BorderLayout.CENTER);//Add all the Label and TextField into the Panel
this.addComponentListener(new ComponentListener()
{
public void componentResized(ComponentEvent e)
{
//toScale();
}
public void componentMoved(ComponentEvent e){}
public void componentShown(ComponentEvent e){}
public void componentHidden(ComponentEvent e){}
});
}
public void paintComponent(Graphics g)
{
//Create a Graphics2D object
final Graphics2D g2 = (Graphics2D) g.create();
//g2.scale(scaleX,scaleY);
g2.setFont(new Font("Times New Roman",Font.PLAIN,10));
}
public void printGraph()
{
scaleX = 1;
scaleY = 1;
PrinterJob printJob = PrinterJob.getPrinterJob();
boolean print = printJob.printDialog();
if(print)
{
printJob.setPrintable(ValueInput.this);
try { printJob.print(); } catch (Exception PrintException) {}
}
toScale();
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
if (pageIndex >= 1)
{
return Printable.NO_SUCH_PAGE;
}
g.translate(80, 100);
paintComponent (g);
return Printable.PAGE_EXISTS;
}
public void toScale()
{
Dimension d = ValueInput.this.getSize();
scaleY = d.getHeight()/size.getHeight();
scaleX = d.getWidth()/size.getWidth();
refresh();
}
//refresh is called by this class only to repaint the MainFrame after scaling
private void refresh()
{
gf.refresh();
}
public class Enter implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==jbtnEnter)
{
String EntitiyTitle=(jtf1.getText().trim());
String t1=(jtf2.getText().trim());
String t2=(jtf3.getText().trim());
String t3=(jtf4.getText().trim());
String t4=(jtf5.getText().trim());
System.out.println(EntitiyTitle);
System.out.println(t1);
System.out.println(t2);
System.out.println(t3);
System.out.println(t4);
GraphValue gv = new GraphValue(""+EntitiyTitle,""+t1,""+t2,""+t3,""+t4 );
gv.addEntity("Tiger",10);
gv.addEntity("Guiness",20);
gv.addEntity("Heneiken",30);
gf.refresh();
lg.refreshLg();
lg.refresh();
lg.getG();
}//end of if
}//end of actionPerformed
}//end of Enter
}
__________________
|