In: Computer Science
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);
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: