Task Complete these two exercises, each of which involve drawing things on a pan
ID: 652971 • Letter: T
Question
Task
Complete these two exercises, each of which involve drawing things on a panel, from inside the paintComponent() method:
Filenames should be
Circles.java
Triangles.java
Exercise 1
Filename: CirclesTester.java
Write an application that draws a series of eight concentric circles. The circles should be separated by 10 pixels.
Notes and requirements
Note that concentric circles have the same center point, but different radii. (i.e. one circle is enclosed inside another, with the same center)
Cirles can easily be drawn using either method drawArc() or with drawOval()
Set the application up as a JPanel embedded inside of a JFrame. Make the title bar of the JFrame, where your own name is on the title bar. My title bar would read "Muye Liu: Circles".
The drawing of the circles should be done on a class that extends JPanel -- see the chapter 12 examples for this setup
Make the default starting size of the frame 400 x 400. You can make your circles any size, as long as they all fit in the window, and the smallest circle has at least a radius of 5 pixels, and each circle is separated from the next by 10 pixels
Hint: On code layout, the chapter 12 examples from Deitel (the recommended reference text) illustrate the idea of doing a JPanel inside of a JFrame. However, the textbook does each example in 2 files. Instead, you should put your code in a single file. See these sample layouts -- you may use either one of these if you like, to get you started:
Sample layout 1: This one is like the ch12 examples, but just with both classes placed in the same file.
Sample layout 2: This one will also work, but it does everything in one class. So the main() method would still need to create the JFrame, an instance of the panel (which is now the Circles class), and put the panel into the frame. The paintComponent() method is still where the drawing of the circles would be done
Exercise 2:
Filename: Triangles.java
Write an application that creates 5 random triangles, each one filled with random color.
Notes and requirements
As before, the drawing should be on a JPanel, embedded in a JFrame. The JFrame's starting size should be 500 x 500, and the title bar should have your name, and the word In class Graphics, the easiest way to draw filled triangles is with the methods for drawing polygons. A triangle is just a 3-sided polygon.
To create a randomly placed triangle, just pick three randomly chosen points on the panel, and make a polygon out of them. Note: It is not sufficient to choose random numbers in the range 0-499 (even though the frame starts out 500 x 500). This is because the frame COULD be resized, which changes the size of the panel. Instead, there are methods called getHeight() and getWidth() that can be called for any component, to find out what its current size is. Example:
int h = myPanel.getHeight(); // h now stores the current height of the
// panel called "myPanel"
Your triangles must actually show up on the panel. (Note that resizing the application will cause the paintComponent() method to run again, which will create new random triangles).
To create a random color, you need to choose random red/green/blue values. Remember that each RGB value is a number in the range 0-255
Explanation / Answer
1 . Write an application that draws a series of eight concentric circles. The circles should be separated by 10 pixels.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Concentric extends JFrame
{
public Concentric()
{
super( "Concentric" );
setSize( 300, 300 );
show();
}
public void paint( Graphics g )
{
for ( int x = 0; x <= 160; x += 10 ) {
int y = 160 - ( x * 2 );
g.drawOval( x + 30, x + 30, y, y );
}
}
public static void main( String args[] )
{
Concentric app = new Concentric();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
2. Write an application that creates 5 random triangles, each one filled with random color.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Lines1 extends JFrame
{
public Lines1()
{
super( "Drawing Lines" );
setSize( 200, 200 );
show();
}
public void paint( Graphics g )
{
for ( int y = 10; y < 200; y += 10 )
{
int x1 = ( int ) ( 1 + Math.random() * 199 );
g.setColor( new Color( ( float ) Math.random(),
( float ) Math.random(), ( float ) Math.random() ) );
g.drawLine( 1, y, x1, y );
}
}
public static void main( String args[] )
{
Lines1 app = new Lines1();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.