0% found this document useful (0 votes)
13 views3 pages

Java Graphics Bar Chart Example

The document contains Java code for a graphical application that displays bars based on user input. It includes methods for drawing bars, getting random bar heights, and a placeholder for swapping bars. The application uses a JPanel to render graphics and prompts the user for the number of bars to display.

Uploaded by

ompanem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Java Graphics Bar Chart Example

The document contains Java code for a graphical application that displays bars based on user input. It includes methods for drawing bars, getting random bar heights, and a placeholder for swapping bars. The application uses a JPanel to render graphics and prompts the user for the number of bars to display.

Uploaded by

ompanem
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//----------------------------------------//

// DO NOT CHANGE ANY CODE IN THE BOXES! //


//----------------------------------------//
/******************************************/
/**/ import [Link].*; /**/
/**/ import [Link].*; /**/
/**/ import [Link]; /**/
/**/ /**/
/**/class DrawPanel extends JPanel /**/
/**/{ /**/
/******************************************/
Scanner input = new Scanner([Link]);
private int[] barHeight;
private int bar1;
private int bar2;
private int count = 0;

public void draw(Graphics g)


{
if(count == 0)
{
[Link]("How many bars will be displayed {1-600}? ");
int numBars = [Link]();
barHeight = new int[numBars];

repaint();
count++;
}
else if(count == 1)
{
getBarValues();
displayBars(g);
}
else if(count == 2)
{
delay(3000);
swapBars();
displayBars(g);
}
}

// COMPLETE THESE METHODS TO EARN 80 POINTS


/**/
/**/ private void getBarValues()
/**/ {
/**/ for (int i=0;i<[Link];i++)
{
barHeight[i] = (int) [Link]([Link]() *(380 - 10 + 1) +
10);
}

/**/ }
/**/
/**/ private void displayBars(Graphics g)
/**/ {
/**/ // This method should draw an ORANGE line for each integer in
'barHeight'.
/**/ // For 80 points, the lines should start at the top of the display.
/**/ // For 90 points, the lines should start at the bottom of the display.
/**/
/**/ // Your code goes here
for (int i=0; i<[Link];i++){
[Link](i,390,i,barHeight[i]);
}
/**/ }
/**/
/**/

// COMPLETE THIS METHOD TO EARN 100 POINTS


/**/
/**/ private void swapBars()
/**/ {

/**/ }
/**/
/**/

//------------------------------------------------------------//
// DO NOT CHANGE ANY CODE BELOW! //
//------------------------------------------------------------//
/**************************************************************/
/**/ public void delay(int n) /**/
/**/ { /**/
/**/ long startDelay = [Link](); /**/
/**/ long endDelay = 0; /**/
/**/ while (endDelay - startDelay < n) /**/
/**/ endDelay = [Link](); /**/
/**/ } /**/
/**/ /**/
/**/ @Override /**/
/**/ public void paintComponent(Graphics g) /**/
/**/ { /**/
/**/ [Link](g); /**/
/**/ draw(g); /**/
/**/ } /**/
/**/} /**/
/**/public class MyProgram extends JFrame /**/
/**/{ /**/
/**/ public MyProgram() /**/
/**/ { /**/
/**/ initUI(); /**/
/**/ } /**/
/**/ private void initUI() /**/
/**/ { /**/
/**/ DrawPanel drawPanel = new DrawPanel(); /**/
/**/ add(drawPanel); /**/
/**/ setSize(600,390); /**/
/**/ setTitle("Graphics"); /**/
/**/ setLocationRelativeTo(null); /**/
/**/ } /**/
/**/ public static void main(String[] args) /**/
/**/ { /**/
/**/ [Link](() -> /**/
/**/ { /**/
/**/ MyProgram ex = new MyProgram(); /**/
/**/ [Link](true); /**/
/**/ }); /**/
/**/ } /**/
/**/} /**/
/**************************************************************/

You might also like