Forum Stats

  • 3,826,566 Users
  • 2,260,666 Discussions
  • 7,896,999 Comments

Discussions

Java applet to draw grid

810103
810103 Member Posts: 23
edited Dec 6, 2010 6:04AM in New To Java
Hi, I have an exam Tuesday and an assignment Ive done before is to draw a chessboard pattern, but a past question has been to draw a grid pattern with red horizontal lines and blue vertical ones.

It must display an input dialog asking the user how many lines to draw along each side and the pattern should size itself to fit exactly with the applet area.

It says to use setColor(Color c) and fillRect(x1,y1,x2,y2): draws lines between (x1, y1) and (x2,y2)

Here's my code for the chessboard:
import java.awt.*;
import java.applet.*;

public class Q2_chessboard extends Applet {
	public void paint(Graphics g) 
	{

		int row, column, x, y;
		
		//for every row on the board
		for (row = 0;  row < 8;  row++ ) 
		{
			//for every column on the board
			for (column = 0;  column < 8;  column++) 
			{
				//Coordinates
				x = column * 20;
				y = row * 20;
				
				//square is red if row and col are either both even or both odd.
				if ( (row % 2) == (column % 2) ) 
					g.setColor(Color.red);
				
				else
					g.setColor(Color.black);
				g.fillRect(x, y, 20, 20);
			} 
		} 
	}
}
But Ive tried for ages and cant turn this code into the code needed for the past question.

I know I have to use:
		//Gets size of Applet
		int appletHeight = getSize().height;
		int appletWidth = getSize().width;
But cant wrap my head around it where to use it.

I had replaced the numerical values of row and column with
		input = JOptionPane.showInputDialog("Enter number lines to draw on each side:");
		lines = Integer.parseInt(input);
but it came asked twice and I couldnt expand the applet without it asking for input again.
Tagged:

Best Answer

  • walterln
    walterln Member Posts: 2,302
    Answer ✓
    1) Don't mix AWT and Swing - Use JApplet if you want to use JOptionPane
    2) Don't ask for input inside paint or paintComponent methods. You cannot control when this method is called and it shouldn't change state, only paint as fast as possible.
    3) Don't override the paint of a JApplet instead add a JPanel with a overridden paintComponent method to the content pane. Search on "java custom painting tutorial" for more info.
    4) For the proper way to code an JApplet search on "java JApplet tutorial". You option pane should be shown in the called in the init() method (on the EDT). The result is best set as a instance variable of the panel which does the painting.
    5) Two loops seems fine but you could move the color statement out of it.

Answers

  • 810103
    810103 Member Posts: 23
    edited Dec 5, 2010 9:07PM
    Came up with this:
    import java.awt.*;
    import java.applet.*;
    import javax.swing.JOptionPane;
    
    public class VertLines extends Applet {
    
    	public void paint(Graphics g) 
    	{
    		String input;
    		int lines;
    		
    		input = JOptionPane.showInputDialog("Enter number of lines to draw on each side:");
    		lines = Integer.parseInt(input);
    
    		int horizontal, vertical, x, y;
    		
    		//Gets size of Applet
    		int appletWidth = getSize().width;
    		int appletHeight = getSize().height;
    		
    		//for every horizontal on the board
    		for (horizontal = 0;  horizontal < lines;  horizontal++ ) 
    		{
    			//Coordinates
    			y = horizontal*(appletWidth/10);
    			g.setColor(Color.red);
    			g.drawLine(0, y, appletWidth, y);
    		} 
    		
    		for (vertical = 0;  vertical < lines;  vertical++ ) 
    		{
    			//Coordinates
    			x = vertical*(appletHeight/10);
    			g.setColor(Color.blue);
    			g.drawLine(x, 0, x, appletHeight);
    		} 
    	} 
    }
    Doesnt really do 'exactly' what I want but its the closest ive gotten and ive had to use two different loops and drawLine instead of drawRect.

    The input dialog still comes up twice too.
  • walterln
    walterln Member Posts: 2,302
    Answer ✓
    1) Don't mix AWT and Swing - Use JApplet if you want to use JOptionPane
    2) Don't ask for input inside paint or paintComponent methods. You cannot control when this method is called and it shouldn't change state, only paint as fast as possible.
    3) Don't override the paint of a JApplet instead add a JPanel with a overridden paintComponent method to the content pane. Search on "java custom painting tutorial" for more info.
    4) For the proper way to code an JApplet search on "java JApplet tutorial". You option pane should be shown in the called in the init() method (on the EDT). The result is best set as a instance variable of the panel which does the painting.
    5) Two loops seems fine but you could move the color statement out of it.
This discussion has been closed.