0

Hi everyone, I was writing a program to show visually some vectors I was implementing with Java but I ran into an issue. My program has 3 classes, a driver, a Vector class and a VectorDisplay. The driver and vector class are self explanatory, but here is the code.
Driver:

import phys.*;
import java.util.*;

public class Driver
{
    public static Vector1 vect1 = new Vector1(50, 70);

    public static void dispVectStats()
    {
        vect1.dispVect();
        vect1.drawVect();
    }

    public static void main(String[] args)
    {
        System.out.print("\f");
        dispVectStats();
    }
}

Vector:

package phys;

import java.util.*;


public class Vector1
{
    private int x;
    private int y;

    public Vector1(int xTemp, int yTemp)
    {
        x = xTemp;
        y = yTemp;
    }

    public void drawVect()
    {
        VectorGraph vgraph = new VectorGraph(x, y);
        vgraph.init();
        vgraph.repaint();
    }

    public void dispVect()
    {
        System.out.print("X: " + x + "\n");
        System.out.print("Y: " + y + "\n");
    }
}
`

Vector Display:

package phys;
import java.awt.*;
import javax.swing.*;
import java.util.*;

public class VectorGraph extends JPanel
{
    private int xLength = 500;
    private int yLength = 500;

    private int xi1 = xLength/2;
    private int yi1 = yLength/2;
    private int xf1;
    private int yf1;

    public VectorGraph()
    {
        super();
    }

    public VectorGraph(int xF, int yF)
    {
        super();
        xf1 = xF;
        yf1 = yF;
    }


    public void paint(Graphics g)
    {
        super.paint(g);
        g.drawLine(0, yLength/2, xLength, yLength/2); 
        g.drawLine(xLength/2, 0, xLength/2, yLength); 
        g.drawLine(xi1, yi1, xf1, yf1); 
        System.out.print(xf1);
    }

    public void init()
    {
        JFrame frame = new JFrame("Vectors");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(xLength, yLength);

        VectorGraph panel = new VectorGraph();
        frame.setContentPane(panel);          
        frame.setVisible(true);  
    }
}

When I run the driver the code executes with no errors and it draws a line on the JFrame from 250 to 0 meaning that xf1 and yf1 are both 0 when the paint code is run (Also evidenced in the trace command which outputs 0) but in the constructor I set xf1 and yf1 to 50 and 70. What is happening that xf1 and yf1 both are resetting to 0?

Thanks for any help in advance.

2
Contributors
3
Replies
5
Views
4 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • It's line 44 that's worng. When you init your VectorGraph instead of adding that VectorGraph to the JFrame you create a second VectorGraph (with default values of 0) and add that instead. Your correctly constructed VectorGraph never gets displayed. Read More

1

It's line 44 that's worng.
When you init your VectorGraph instead of adding that VectorGraph to the JFrame you create a second VectorGraph (with default values of 0) and add that instead. Your correctly constructed VectorGraph never gets displayed.

Edited by JamesCherrill

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.