我正在嘗試使用 Java Graphics 和 paintComponent() 方法制作一個圓柱體。應用程式的用戶可以選擇他們想要的形狀(在這種情況下是圓柱體)并輸入他們想要的形狀尺寸。在他們輸入尺寸并單擊提交按鈕后,將打開另一個視窗并在其上繪制影像。
我當前的問題是正確制作形狀。我目前正在嘗試制作兩個橢圓并使用兩條線將它們連接起來。底座將是一個紅色橢圓形,其他一切都沒有顏色。
當您提交圓柱體的尺寸時,側面永遠不會是正確的長度或在 Y 軸上的正確位置。一個例子可以在這里查看:

這個圓柱體的尺寸:200 高度,50 半徑。
圓柱體應該是什么樣子:

300 高度,300 半徑
I'll be working on adding the minimal version of the program for testing. However, for right now I'll be providing what the code is for the cylinder itself and the paintComponent() method.
Cylinder:
import java.awt.Color;
public class Cylinder extends Circle {
private int length;
public Cylinder(int radius, int length, Color color) {
super(radius, length, color);
this.length = length;
this.radius = radius;
}
public Cylinder(int newX, int newY, int newRadius, int newLength) {
super(newX, newY, newRadius);
length = newLength;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int calcArea() {
return (int) Math.ceil( 2 * 3.14 * radius * radius 2 * 3.14 * radius * length);
}
public int calcVolume() {
return (int) Math.ceil(3.14 * radius * radius * length);
}
public DrawFigure drawFigure() {
DrawFigure cylinder1 = new DrawFigure(4, getRadius(), length);
return cylinder1;
}
public String toString() {
return "Length = " length " " super.toString();
}
}
DrawFigure (paintComponent is the last method):
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawFigure extends JPanel {
int type;
int length, width, height, radius;
public DrawFigure() {
super();
type = 5;
}
public DrawFigure(int myType, int myWidth, int myLength, int myHeight) { // Box and Rectangle
super();
type = myType;
length = myLength;
width = myWidth;
height = myHeight;
}
public DrawFigure(int x, int y, int myType, int myWidth, int myLength, int myHeight) {
super();
type = myType;
length = myLength;
width = myWidth;
height = myHeight;
}
public DrawFigure(int myType, int myRadius, int myHeight) {
super();
type = myType;
radius = myRadius;
height = myHeight;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (type == 1) { // Draw Rectangle
} else if (type == 2) { // Draw Box
} else if(type == 3) { // Draw Circle
} else if(type == 4) { // Draw Cylinder
g.setColor(Color.BLACK);
g.drawOval(135, 65, radius, radius - radius / 2);
// Base
g.setColor(Color.RED);
g.fillOval(135, 65 height, radius, radius / 2);
g.setColor(Color.BLACK);
g.drawLine(135, 65 height (height /4), 135, 135);
g.setColor(Color.BLACK);
g.drawLine(135 radius, 65 height (height /4), 135 radius, 135);
return;
}
}
}
Full code for the program:
- Point.java
所以,假設我們從 開始
0x0,這意味著線條需要從 的y位置開始radius / 4,因為您使用radius的是寬度和radius / 2高度(我不會提及這是多么令人困惑)。這將允許線條“出現”它們加入橢圓的外邊緣(并向下繪制)那么行會很
height長。這意味著底部的橢圓形需要從height - (radius / 4)...開始。好吧,我不得不去仔細檢查一下,但請記住,線結束于(radius / 4) height,這也意味著圓柱體的height (radius / 2)總高度。????
height=200,radius=50
height=300,radius=300
這可以防止
radius / 4大于 的情況height,因為那將是一團糟可運行的示例...
import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Main { public static void main(String[] args) { new Main(); } public Main() { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame(); frame.add(new DrawPane(300, 300)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class DrawPane extends JPanel { int height, radius; public DrawPane(int myRadius, int myHeight) { super(); radius = myRadius; height = myHeight; } @Override public Dimension getPreferredSize() { return new Dimension(400, 400); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); int x = (getWidth() - radius) / 2; int y = (getHeight() - (height (radius / 4))) / 2; g2d.translate(x, y); g2d.setColor(Color.LIGHT_GRAY); g2d.drawRect(0, 0, radius, height (radius / 4)); // Base g2d.setColor(Color.RED); g2d.fillOval(0, height - (radius / 4), radius, radius / 2); g2d.setColor(Color.BLACK); g2d.drawOval(0, 0, radius, radius / 2); g2d.setColor(Color.BLACK); g2d.drawLine(0, radius / 4, 0, height); g2d.drawLine(radius, radius / 4, radius, height); g2d.dispose(); } } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434898.html
