Question

Write an application that extends JFrame and that displays diagonal lines in a square, like those...

Write an application that extends JFrame and that displays diagonal lines in a square, like those in the figure below. Save the file as JDiagonalLines.java.

xid-1453787_1

Output of the JDiagonalLines program

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class NewJFrame extends javax.swing.JFrame {
private int side = 200;
public NewJFrame()
{
// initComponents();
// Set JFrame title
super("Square with Diagonals");
//Set default close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set JFrame size
setSize(400,400);
//Make JFrame visible   
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);   
//draw square outline
int x=50,y=50; // starting position of square
g.setColor(Color.BLUE);
g.drawRect(x,y,side,side);
g.setColor(Color.RED);
g.drawLine(x, y, x+side, y+side);// diagonal1
g.drawLine(x+side, y, x, y+side); // Diagonal2   
}

public static void main(String args[]) {

NewJFrame nf=new NewJFrame();
}   

}

//OUTPUT

Add a comment
Know the answer?
Add Answer to:
Write an application that extends JFrame and that displays diagonal lines in a square, like those...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (A). Write an application that extends JPanel and displays a pharse in large font. Each time...

    (A). Write an application that extends JPanel and displays a pharse in large font. Each time the user clicks a JButton, display the same pharse in a different color, a little further to the right, and in a slightly smaller font. Allow only three clicks…Save the file as JChangeSizeAndColorPanel.java (B). Modify the JChangeSizeAndColorPanel application so that it continuously changes the size, color, and location of phrase as long as the user continues to click the button. Save file as JChangeSizeAndColorPanel2.java

  • Write a Java application that displays the following ClickMe When the user clicks on "Click Me"...

    Write a Java application that displays the following ClickMe When the user clicks on "Click Me" button, the text, "l am clicked" is displayed. ClickMe am clicked import java.awt.*; import java.awt.event.*; public class OneButton extends JFrame implements ActionListener private JButton button; private JTextField field; public static void main(String args)( OneButton myCoolButton new OneButton0;

  • Write a Java application that displays the following output pattern: Thus, there are+signs everywhere except the...

    Write a Java application that displays the following output pattern: Thus, there are+signs everywhere except the diagonal, which has". "signs. There are no blank characters. Requirements and Restrictions: The number of lines and the number of characters per line, i.e. the size of the square pattern and line argument. For example, the size of the above square is 11 and it is specified through the following compile and execute commands: e >javac PrintPattern.java > java PrintPattern 11 Your program should...

  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • -Write a JavaFX application that displays a Label containing the opening sentence or two from your...

    -Write a JavaFX application that displays a Label containing the opening sentence or two from your favorite book. Save the project as FXBookQuote1a. -Add a button to the frame in the FXBookQuote program. When the user clicks the button, display the title of the book that contains the quote in a second label. Save the project as FXBookQuote1b.

  • Write a C# console application that reads a student’s grade and displays the letter grade. It...

    Write a C# console application that reads a student’s grade and displays the letter grade. It should validate the input before processing it, i.e. if the input is negative or more than 100, output an error message and stop the program. If the input is not a number, output an error message and stop the program, too. There is one requirement: to determine the letter grade , use “ switch…case “

  • 1.A. Write a JavaFX application that displays a Label containing the opening sentence or two from...

    1.A. Write a JavaFX application that displays a Label containing the opening sentence or two from your favorite book. Save the project as FXBookQuote1a. B. Add a button to the frame in the FXBookQuote program. When the user clicks the button, display the title of the book that contains the quote in a second label. Save the project as FXBookQuote1b.

  • C++ The manager of Keystone Tile wants an application that displays the area of a rectangular...

    C++ The manager of Keystone Tile wants an application that displays the area of a rectangular floor, given its measurements in feet. It should also display the total cost of tiling the floor, given the price per square foot of tile. 1-Create a new project named Intermediate13 Project, and save it in the Cpp8\Chap04 folder. Enter your C++ instructions into a source file named Intermediate13.cpp. Also enter appropriate comments and any additional instructions required by the compiler. Test the program...

  • 4. Consider the picture below a. Write the coefficients of vectors corresponding to . body diagonal face diagonal Find the norms of vectors corresponding to b. body diagonal .face diagonal Find t...

    4. Consider the picture below a. Write the coefficients of vectors corresponding to . body diagonal face diagonal Find the norms of vectors corresponding to b. body diagonal .face diagonal Find the angle between a body diagonal of a cube and a face diagonal (like in the picture). Leave the answer in the form of inverse cosine of an exact number (possibly a square root). c. (0,0,0) 4. Consider the picture below a. Write the coefficients of vectors corresponding to...

  • Write an application that displays a series of at least five student ID numbers (that you...

    Write an application that displays a series of at least five student ID numbers (that you have stored in an array) and asks the user to enter a numeric test score for the student. Create a ScoreException class, and throw a ScoreException for the class if the user does not enter a valid score (less than or equal to 100 or greater than 0). Catch the ScoreException and then prompt the user to re-enter the score. At the end of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT