Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Any way to compress these else if statements?

809868Nov 3 2010 — edited Nov 7 2010
Hey guys. i have a button that draws a graph once pushed. The button works perfect draws a graph in a jpanel all is good. My question though, is there a way to compress the if/else statements so there arnt so many? Just seems it is taking up alot of lines.

private JButton getJButton_Draw() {
		if (jButton_Draw == null) {
			jButton_Draw = new JButton();
			jButton_Draw.setBounds(new Rectangle(15, 525, 137, 31));
			jButton_Draw.setText("DRAW");
			jButton_Draw.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					// Get values from the text fields
				    Wk1 = Integer.parseInt(jTextField_Wk1.getText());
					Wk2 = Integer.parseInt(jTextField_Wk2.getText());
					Wk3 = Integer.parseInt(jTextField_Wk3.getText());
				    Wk4 = Integer.parseInt(jTextField_Wk4.getText());
					Wk5 = Integer.parseInt(jTextField_Wk5.getText());
					Wk6 = Integer.parseInt(jTextField_Wk6.getText());	
					iaName = jTextField_Artist.getText();
				    isName = jTextField_Song.getText();
				    // Sets the label for who sings and and what's the song name
				    jLabel_artistNameSong.setText("Chart for " + isName + " by " + iaName );
					
					if(!jRadioButton_Line.isSelected())
					{
					iY = ((Wk1 * 15) - 5);
					iX = (1 * 45) -5;
										
					jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
					
					iY = ((Wk2 * 15) - 5);
					iX = (2 * 45) -5;
										
					jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
					
					iY = ((Wk3 * 15) - 5);
					iX = (3 * 45) -5;		
					
					jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
					
					iY = ((Wk4 * 15) - 5);
					iX = (4 * 45) -5;					
					
					jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
					
					iY = ((Wk5 * 15) - 5);
					iX = (5 * 45) -5;
					
					jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
					
					iY = ((Wk6 * 15) - 5);
					iX = (6 * 45) -5;
					
					jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
					}
					
					else{
						
						iY = ((Wk1 * 15) - 5);
						iX = (1 * 45) -5;
						iPrevY = ((Wk1 * 15) - 5);
						iPrevX = 0;
						
						jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
						
						iY = ((Wk2 * 15) - 5);
						iX = (2 * 45) -5;
						iPrevY = ((Wk1 * 15) - 5);
						iPrevX = (1 * 45) -5;
						
						jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
						
						iY = ((Wk3 * 15) - 5);
						iX = (3 * 45) -5;
						iPrevY = ((Wk2 * 15) - 5);
						iPrevX = (2 * 45) -5;
						
						jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
						
						iY = ((Wk4 * 15) - 5);
						iX = (4 * 45) -5;
						iPrevY = ((Wk3 * 15) - 5);
						iPrevX = (3 * 45) -5;
						
						jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
						
						iY = ((Wk5 * 15) - 5);
						iX = (5 * 45) -5;
						iPrevY = ((Wk4 * 15) - 5);
						iPrevX = (4 * 45) -5;
						
						jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
						
						iY = ((Wk6 * 15) - 5);
						iX = (6 * 45) -5;
						iPrevY = ((Wk5 * 15) - 5);
						iPrevX = (5 * 45) -5;
						
						jPanel_drawingArea.paint(jPanel_drawingArea.getGraphics());
					
					
				}
				}});
		}
		return jButton_Draw;
	}
PS, the Wk1 and wk4 poor alignment is only in this code i pasted it in but it seems to have lost its spot..

Edited by: Darryl Burke -- removed the long comment to its own line as it was breaking the forum formatting in some browsers
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 5 2010
Added on Nov 3 2010
5 comments
265 views