So, I have an interface I would like to use, and I need to put lines on a JPanel within a Frame. Because I need other controls in the Frame so if I set the frames focus to the lines I won't have the controls.

And also, I have another large application that already has this type of UI code style, so I have the same style in this small demo, so I can add it to my large application without any problems later once I've finished this demo.

This is meant to turn out to be a updateing line graph, which adds a new line every few seconds(bandwidth and lag time monitoring between a server).

I have the panel, and a line drawn. But my problem is falling into the hands of where it's drawing the line. The line seems to be about 10 pixels lower than I can make it, I want the line to start exactly at the top-left(0, 0) but for some reason it doesn't like to do this.

The panel is 240px long, and 50px up and down.
The line starts at 0, 0 and goes to 240, 50.
You will notice that it's off.
Why though?

import java.awt.*;
import javax.swing.*;

public class Graph extends JFrame {
	private JPanel frame;
	private JPanel panel;


	public Graph() {
		super();
		initializeComponent();
		this.setVisible(true);
	}

	private void initializeComponent() {
		frame = (JPanel)this.getContentPane();
		panel = new JPanel();

		frame.setLayout(null);
		frame.setBackground(new Color(0, 0, 0));
		frame.setForeground(new Color(255, 0, 0));
		
		addComponent(frame, panel, 10,10,240,50);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		panel.setBackground(new Color(50, 50, 50));
		panel.add( new DrawLine() );
		
		this.setTitle("Graph");
		this.setLocation(new Point(10, 10));
		this.setSize(new Dimension(270, 105));
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private void addComponent(Container container,Component c,int x,int y,int width,int height) {
		c.setBounds(x,y,width,height);
		container.add(c);
	}


	
	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		try {
			UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
		} catch (Exception ex) {  }
		new Graph();
	}
	
}


class DrawLine extends JPanel {
	
	public Dimension getPreferredSize() {
		return new Dimension(240, 50);
	}
	
	protected void paintComponent(Graphics g) {
 
		g.setColor( Color.red );
		// X Start, Y Start, X End, Y End
		// X = <---------->
		g.drawLine ( 0, 0, 240, 50 );
 
	}
}

Solved my own problem;

public Dimension getPreferredSize() { return new  Dimension(240, 50); }
public int getY() { return 0; } public int getX() { return 0; }

Lol. Gay.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.