Simple graphics practice on previous Java code
I am leaning Java basics. I wrote code to produce the following output
http://img4.imageshack.us/img4/3339/appc.png
the image on the right. I have a problem to make things touch each other at the middle of the screen. i. e. line going from the left upper corner touch lines going from the right bottom corner. Also, the output should have the same look when resizing the window. My code( Java pastebin - collaborative debugging tool )and ( Java pastebin - collaborative debugging tool ). I am not so good in math. Can anyone here help me to get the required output?
Thanks in advance
Sorry I forgot to paste the code
Code :
//Code Test Class
import javax.swing.JFrame;
public class DrawPanelTest
{
public static void main( String args[] )
{
// create a panel that contains our drawing
DrawPanel panel = new DrawPanel();
// create a new frame to hold the panel
JFrame application = new JFrame();
// set the frame to exit when it is closed
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.add( panel ); // add the panel to the frame
application.setSize( 250, 250 ); // set the size of the frame
application.setVisible( true ); // make the frame visible
} // end main
} // end class DrawPanelTest
Code :
//code Draw panel
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel extends JPanel
{
// draws an X from the corners of the panel
public void paintComponent( Graphics g )
{
// call paintComponent to ensure the panel displays correctly
super.paintComponent( g );
int width = getWidth(); // total width
int height = getHeight(); // total height
int i = 0, j = 0, k = 0;
for ( i = 1; i <= 15 ; i++)
{
g.drawLine( 0, 0, j, height - k);
j += width / 15;
k += height / 15;
}
j = 0;
k = 0;
for ( i = 1; i <= 15 ; i++)
{
g.drawLine( 0, height, j, k);
j += width / 15;
k += height /15;
}
j = 0;
k = 0;
for ( i = 1; i <= 15 ; i++)
{
g.drawLine( width, 0, width - j, height - k);
j += width / 15;
k += height / 15;
}
j = 0;
k = 0;
for ( i = 1; i <= 15 ; i++)
{
g.drawLine( width, height, width - j, k);
j += width / 15;
k += height / 15;
}
} // end method paintComponent
} // end class DrawPanel
Re: Help with simple graphics practice
Welcome to the forums :)
The lines do meet in that picture :S, please use code tags, [ code ][/ code ] without the spaces also would you be as kind as to not use lines numbers -_-
Chris
Re: Help with simple graphics practice
Is the image on the left your example but you need to make it look like the image on the right?
Re: Help with simple graphics practice
The required output is the image on the right
Re: Help with simple graphics practice
I just compiled the code you submitted amrawad_85 and I get the same result as the image on the right!
I don't see a problem here?