loops - Resizeable Number Triangle in Java GUI -


I have this assignment for Java class which I am taking. I was assigned to do this:

Write a program that displays the numbers shown below. The number of lines in the display is changed to fit the window as the size of the window.

This is displayed in GUI (without large space between lines):

1

12

1234 P> 12345

1234567

The larger the number of the counting and the number of lines that is going up, the more I am using the GUI Expand window.

Now in my code:

  import javax swing *. Import java.awt. *; Public class resizing GUI JPNL (public Input width = 600; public intensity height = 200; public int x_coord = 10; public int y_coord = 40; public static final int point_ size = 12; public font font object = new font ("sanssirf" Font.plain, point_size); public integer maximum number = 16; public recycling (super); setSize (width, height);} public zero color (graphics g) {superpant (h) ); G.setFont (fontObject); X_coord = 10; Y_coord = 40; Int lineCount = 0; int line = 1; MaxLines = (this.gethight () - 40) / 10; while (line count  10) {x_coord = X_coord + 5;} line = 0; lineCount ++; Y_coord = y_coord + 10;}}} public static zero main (string [] args) {JFrame frame = new JFrame (); Frame.setTitle ("Resizeable GUI") ; Frame.Set size (600,200); Frame.getContentPane (). Add (New ResizingGUI ()); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.setVisible (true); }}   

I think this solution might be, but the nested loop is throwing me for a loop (yes) if someone has suggested a way to fix the nested loop or (More likely) How to achieve your goal, I would be very grateful.

, basically, you have lines and loops for the number columns (which is the current line number) Want a loop for the number ...

  int line = 0; While (line & lt; maxLines) {int col = 0; While (col & lt; line) {// ... col ++; } Line ++; }   

You are not also resetting back to x_coord in the initial state, which means that each new row will start at the end of the last line ... Even below this

You should also:

  • Override paintComponent instead of Paint .
  • Override getPreferredSize instead of using setSize for more details. This will work better with layout managers
  • Use FontMetrics to determine how many pixels you want to adjust for each row / column Take a look for more details

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -