I have been trying to debug this for hours. The program is supposed to be a grapher that graphs coordinates, but i cannot get anything to display not even a random line, but if i put a print statement there it works. It is a problem with the paintComponent Method. When I out print statement before g.drawLine then it prints, but it doesn't draw any lines even if i put a random line with coordinates (1,3), (2,4).
import java.awt.*;
import java.util.*;
import javax.swing.*;
public abstract class XYGrapher
{
abstract public Coordinate xyStart();
abstract public double xRange();
abstract public double yRange();
abstract public Coordinate getPoint(int pointNum);
public class Paint extends JPanel
{
public void paintGraph(Graphics g, int xPixel1, int yPixel1, int xPixel2, int yPixel2)
{
super.paintComponent(g);
g.setColor(Color.black);
g.drawLine(xPixel1, yPixel1, xPixel2, yPixel2);
}
public void paintXAxis(Graphics g, int xPixel, int pixelsWide, int pixelsHigh)
{
super.paintComponent(g);
g.setColor(Color.green);
g.drawLine(xPixel, 0, xPixel, pixelsHigh);
}
public void paintYAxis(Graphics g, int yPixel, int pixelsWide, int pixelsHigh)
{
super.paintComponent(g);
g.setColor(Color.green);
g.drawLine(0, yPixel, pixelsWide, yPixel);
}
}
public void drawGraph(int xPixelStart, int yPixelStart, int pixelsWide, int pixelsHigh)
{
JFrame frame = new JFrame();
Paint panel = new Paint();
panel.setPreferredSize(new Dimension(pixelsWide, pixelsHigh));
panel.setMinimumSize(new Dimension(pixelsWide, pixelsHigh));
panel.setMaximumSize(new Dimension(pixelsWide, pixelsHigh));
frame.setLocation(frame.getToolkit().getScreenSize().width / 2 - pixelsWide / 2, frame.getToolkit().getScreenSize().height / 2 - pixelsHigh / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(panel);
frame.pack();
frame.setVisible(true);
double xRange = xRange();
double yRange = yRange();
Coordinate xyStart = xyStart();
int xPixel = xPixelStart - (int) (xyStart.getX() * (pixelsWide / xRange));
int yPixel = yPixelStart + (int) ((xyStart.getY() + yRange) * (pixelsHigh / yRange));
System.out.println(xPixel + " " + yPixel);
if(yPixel > 0 && (yPixel < pixelsHigh))
{
System.out.println("y");
panel.paintYAxis(panel.getGraphics(), yPixel, pixelsWide, pixelsHigh);
}
if(xPixel > 0 && (xPixel < pixelsHigh))
{
System.out.println("x");
panel.paintXAxis(panel.getGraphics(), xPixel, pixelsWide, pixelsHigh);
}
for(int i = 0; i>=0; i++)
{
Coordinate point1 = getPoint(i);
Coordinate point2 = getPoint(i+1);
if(point2 == null)
{
break;
}
else
{
if(point1.drawFrom() && point2.drawTo())
{
int xPixel1 = (int) (xPixelStart + (point1.getX() - xyStart.getX()) * (pixelsWide / xRange));
int yPixel1 = (int) (yPixelStart + (xyStart.getY() + yRange-point1.getY()) * (pixelsHigh / yRange));
int xPixel2 = (int) (xPixelStart + (point2.getX() - xyStart.getX()) * (pixelsWide / xRange));
int yPixel2 = (int) (yPixelStart + (xyStart.getY() + yRange - point2.getY()) * (pixelsHigh / yRange));
panel.paintGraph(panel.getGraphics(), xPixel1, yPixel1, xPixel2, yPixel2);
}
}
}
frame.pack();
}
}
This is how i am testing it is supposed to be a square, but nothing shows up.
public class GrapherTester extends XYGrapher
{
public Coordinate xyStart()
{
return new Coordinate(-2,2);
}
public double xRange()
{
return 4;
}
public double yRange()
{
return 4;
}
public Coordinate getPoint(int pointNum)
{
switch(pointNum)
{
case 0: return new Coordinate(-1,-1);
case 1: return new Coordinate(1,-1);
case 2: return new Coordinate(1,1);
case 3: return new Coordinate(-1,1);
case 4: return new Coordinate(-1,-1);
}
return null;
}
public static void main(String[] args)
{
new GrapherTester().drawGraph(100, 100, 500, 500);
}
}
Coordinate class so if any of you want to run and try it out. That is all you would need.
public class Coordinate
{
float x;
float y;
boolean drawTo;
boolean drawFrom;
Coordinate(double x, double y)
{
this.x = (float) x;
this.y = (float) y;
drawFrom = true;
drawTo = true;
}
Coordinate(double x, double y, boolean drawFrom, boolean drawTo)
{
this.x = (float) x;
this.y = (float) y;
this.drawFrom = drawFrom;
this.drawTo = drawTo;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public boolean drawTo()
{
return drawTo;
}
public boolean drawFrom()
{
return drawFrom;
}
}