使用 Java 个图形创建 2D 圆柱体
Creating a 2D Cylinder with Java Graphics
我正在尝试使用 Java 图形和 paintComponent() 方法制作圆柱体。
应用程序的用户可以 select 他们想要的形状(在本例中是圆柱体)并输入他们想要的形状尺寸。
在他们输入尺寸并单击提交按钮后,另一个 window 将打开并在其上绘制图像。
我当前的问题是正确制作形状。我目前正在尝试制作两个椭圆并使用两条线将它们连接起来。底座将是一个红色的椭圆形,其他一切都没有颜色。
当您提交圆柱体的尺寸时,侧面永远不会是正确的长度或在 Y 轴上的正确位置。可以在此处查看示例:
此圆柱体的尺寸:200 高度,50 半径。
圆柱体应该是什么样的:
300 高度,300 半径
我将致力于添加该程序的最小版本以进行测试。但是,现在我将提供圆柱体本身和 paintComponent() 方法的代码。
柱面:
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 是最后一个方法):
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;
}
}
}
程序的完整代码:
- Point.java https://pastebin.com/iVgN47e3
- Lab6GUI.java https://pastebin.com/bKM790iQ
- Rectangle.java https://pastebin.com/MdCrJYeA
- Box.java https://pastebin.com/iZCZpUi7
- Circle.java https://pastebin.com/aui1NgJi
- Cylinder.java https://pastebin.com/fHDNmBXT
- DrawFigure.java https://pastebin.com/z8t31put
- LessThanOrEqualToZeroException.java https://pastebin.com/4ELEmsNX
- LessThanOrGreaterThanException.java https://pastebin.com/1avRUudN
好的,所以绘制一个椭圆从 x
/y
位置延伸,正值 width/height,这将使椭圆绘制 right/down 从 x
/y
位置,例如...
因此,假设我们从 0x0
开始,这意味着这些行需要从 radius / 4
的 y
位置开始,假设您使用的是 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();
}
}
}
我正在尝试使用 Java 图形和 paintComponent() 方法制作圆柱体。 应用程序的用户可以 select 他们想要的形状(在本例中是圆柱体)并输入他们想要的形状尺寸。 在他们输入尺寸并单击提交按钮后,另一个 window 将打开并在其上绘制图像。
我当前的问题是正确制作形状。我目前正在尝试制作两个椭圆并使用两条线将它们连接起来。底座将是一个红色的椭圆形,其他一切都没有颜色。
当您提交圆柱体的尺寸时,侧面永远不会是正确的长度或在 Y 轴上的正确位置。可以在此处查看示例:
此圆柱体的尺寸:200 高度,50 半径。
圆柱体应该是什么样的:
300 高度,300 半径
我将致力于添加该程序的最小版本以进行测试。但是,现在我将提供圆柱体本身和 paintComponent() 方法的代码。
柱面:
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 是最后一个方法):
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;
}
}
}
程序的完整代码:
- Point.java https://pastebin.com/iVgN47e3
- Lab6GUI.java https://pastebin.com/bKM790iQ
- Rectangle.java https://pastebin.com/MdCrJYeA
- Box.java https://pastebin.com/iZCZpUi7
- Circle.java https://pastebin.com/aui1NgJi
- Cylinder.java https://pastebin.com/fHDNmBXT
- DrawFigure.java https://pastebin.com/z8t31put
- LessThanOrEqualToZeroException.java https://pastebin.com/4ELEmsNX
- LessThanOrGreaterThanException.java https://pastebin.com/1avRUudN
好的,所以绘制一个椭圆从 x
/y
位置延伸,正值 width/height,这将使椭圆绘制 right/down 从 x
/y
位置,例如...
因此,假设我们从 0x0
开始,这意味着这些行需要从 radius / 4
的 y
位置开始,假设您使用的是 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();
}
}
}