Question

In: Computer Science

I need to draw a cylinder in java with user input, and I can't seem to...

I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input...

I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.

           g.drawOval(80, 110, radius, height);
           g.drawLine(?, ?, ?, ?);
g.drawLine(?, ?, ?, ?);
           g.drawOval(80, 200, radius, height);

Solutions

Expert Solution

Solution:

As per your problem statement I had designed the code for drawing cylinder in java. Iam adding my source code and screenshot for your reference

Note: I have not taken inputs as user based, I had provided you the co-ordinates to work on. So if you want, just store value in variable and pass the values in  g.methods(ur_variablename,...) , keep playing with co-ordinates to get better idea.

Hope so it will surely help you.

Source Code:

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

public class GUI extends JPanel {

  
   public static void main(String[] args) {
      
       JFrame f = new JFrame(); //maked object for using frame
   f.setSize(800, 800); // set the size for frame
   f.setBackground(Color.white); // setted background color
   f.setForeground(Color.black); // setted foreground color
   f.add(new GUI()); // called this class to repaint window
  
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // to close window
   f.setVisible(true); // maked the frame visible
  
   }
   public void paint(Graphics g) { //initiated paint class to draw on window
         
   g.drawOval(51,51,231,71); //method to draw oval shape
   g.drawLine(51,81,51,331); //method to draw line
   g.drawLine(281,81,280,331); //method to draw line
   g.drawOval(51,301,231,71); //method to draw oval shape
  
   }
   }

ScreenShots:

Code:

Output:


Related Solutions

Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
JAVA - I am asking for the user to input their firstName and lastName but I...
JAVA - I am asking for the user to input their firstName and lastName but I want the method myMethod() to be able to print it when it is called. Is that possible? I'm new to Java and i'm not too sure what I should do to fix this. Also if I were to create a IF statement would I have to declare int amountDeposited; , int accountBalance; , int newBalance; in the new if statement. import java.util.Scanner; import java.util.Arrays;...
I need to write a program and can't seem to make it run properly. Someone can...
I need to write a program and can't seem to make it run properly. Someone can help with the coding? It's with python Thanks! Here is the program: The rules of Shut the Box are as follows (abbreviated version): + You have two 5-sided die + You have 5 wooden blocks (labeled 1 through 5) + Each turn, you roll the dice and knock down the blocks corresponding to the number on each die + You have to knock down...
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
This isn't a homework but since I can't seem to find any answers for it, I...
This isn't a homework but since I can't seem to find any answers for it, I would like to receive some insights from you experts! So, I heard that UK has official left the European Union on Jan 31st (aka Brexit). Are there any trade deals that have been signed? or any negotiations that has been reached? please provide me with some updates I can't seem to find any info on which trade deals were signed and etc. thanks! p.s...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
java.. please dont change the format and give me an output sample! user need to input...
java.. please dont change the format and give me an output sample! user need to input k. public class Josephus {    /**    * All persons sit in a circle. When we go around the circle, initially starting    * from the first person, then the second person, then the third...    * we count 1,2,3,.., k-1. The next person, that is the k-th person is out.    * Then we restart the counting from the next person, go...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
How can I write an essay about Endoscopies with an bibliography? I can't seem to find...
How can I write an essay about Endoscopies with an bibliography? I can't seem to find an example. I need a step by step guide. Who can help me write it please and Thank you
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT