[英]Java Swing Draw Applet
我正在做一項任務,我撞到了一堵磚牆,真的可以使用一些指導。 該程序啟動一個 Java GUI 小程序(我有這個工作),在該小程序中,您可以在幾個選項之間進行選擇,例如形狀類型(橢圓形或矩形)、填充類型(實心或空心)和顏色(一些顏色選項)。
應用程序中有一個 Draw 按鈕,這個 draw 按鈕應該根據上面所做的選擇繪制一個圖像。
對於形狀類型,我正在查看帶有“if”語句的選擇,並根據所選內容將繪圖靜態設置為橢圓形或矩形。
for (String value : shapeType) {
if (value.equals("Rectangle")) {
shapeDimensions = new Rectangle(0, 0, 200, 200);
} else {
shapeDimensions = new Rectangle(40, 30, 100, 125);
}
}
這同樣適用於顏色,如果選擇是紅色,那么我將“顏色”變量設置為color = new Color(255, 0, 0)
。
我遇到的問題是填充類型(實心或空心)? 我不知道該怎么辦。 我把我所有的代碼放在下面以供參考。 正如您將看到的,我有一個自定義的Shape
抽象類和構造函數,我也應該傳遞這些信息。 然后我有諸如setColor
和getSolid
類的方法以及兩個名為 Rectangle 和 Oval 的子類,根據我對形狀類型(橢圓形或矩形)的選擇,我應該繪制這些形狀之一。
我不知道如何處理填充類型或如何將其傳遞給某些東西。 我也不確定我在Shape
類下使用了正確的參數類型。 我也不確定我是否應該按照我在繪制按鈕 actionListener 下的方式進行計算以確定顏色和形狀類型。 任何和所有的幫助將不勝感激!
package PRJ3;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
public class PRJ3 extends JFrame {
public PRJ3() {
}
abstract static class Shape extends Rectangle {
private static Color shapeColor;
private static Shape shape;
private static Rectangle shapeType;
Shape(Rectangle shapeType, Color shapeColor, Shape shapeFill) {
Shape.shapeType = shapeType;
Shape.shapeColor = shapeColor;
Shape.shape = shapeFill;
} // end Shape constructor
public void setColor(Color shapeColor) {
/** should accept the graphics object as a parameter and set the color for the next
* draw operation to the color of the current shape.
*/
Shape.shapeColor = shapeColor;
}
public Shape getSolid() {
return shape;
}
static int getNoOfShapes() {
return 1;
}
abstract void draw (Graphics graphicObject);
static class Oval extends Shape {
Dimension objectDimension;
Rectangle graphicObject;
Color shapeColor;
Shape shapeFill;
Oval(Rectangle graphicObject, Color shapeColor, Shape shapeFill) {
super(graphicObject, shapeColor, shapeFill);
}
@Override
void draw(Graphics graphicObject) {
}
} // end over Oval subClass
static class Rectangular extends Shape {
Rectangular(Rectangle graphicObject, Color shapeColor, Shape shapeFill) {
super(graphicObject, shapeColor, shapeFill);
}
@Override
void draw(Graphics graphicObject) {
Drawing drawRectangle = new Drawing();
}
} // end of Rectangle subClass
static class Drawing extends JPanel {
private Shape shape;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Shapes: " + Shape.getNoOfShapes(), 10, 20);
/** Shape isn't initialized when this is called the first time */
if (shape != null) {
shape.draw(g);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public void drawShape() throws OutsideBounds {
// check provided size vs preferred size
if (shape.getWidth() > this.getPreferredSize().getWidth() || shape.getHeight() > this.getPreferredSize().getHeight()) {
throw new OutsideBounds();
} else {
this.shape = shape;
repaint();
}
} // end drawShape
} // end of Drawing subClass
static class OutsideBounds extends Throwable {
}
} // end of Shape parent class
static public void main(String[] args) {
JPanel contentPane;
JTextField tfWidth;
JTextField tfHeight;
JTextField tfxCoordinate;
JTextField tfyCoordinate;
JComboBox cbxShapeType;
JLabel lblShapeType;
JPanel panelShapeDrawing;
JLabel lblFillType;
JComboBox cbxFillType;
JLabel lblColor;
JComboBox cbxColor;
JLabel lblWidth;
JLabel lblHeight;
JLabel lblxCoordinate;
JLabel lblyCoordinate;
JButton btnDraw;
final String[] shapeType = new String[1];
final String[] shapeColor = new String[1];
final String[] shapeFill = new String[1];
JFrame Frame = new JFrame("Geometric Drawing");
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setBounds(100, 100, 601, 330);
contentPane = new JPanel();
Frame.setContentPane(contentPane);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] {20, 106, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 0};
gbl_contentPane.rowHeights = new int[] {30, 30, 30, 30, 29, 30, 29, 30, 19, 30, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
lblShapeType = new JLabel("Shape Type");
GridBagConstraints gbc_lblShapeType = new GridBagConstraints();
gbc_lblShapeType.fill = GridBagConstraints.HORIZONTAL;
gbc_lblShapeType.insets = new Insets(0, 0, 5, 5);
gbc_lblShapeType.gridx = 1;
gbc_lblShapeType.gridy = 1;
contentPane.add(lblShapeType, gbc_lblShapeType);
String[] shapeTypes = {"" ,"Oval", "Rectangle"};
cbxShapeType = new JComboBox(shapeTypes);
GridBagConstraints gbc_cbxShapeType = new GridBagConstraints();
gbc_cbxShapeType.fill = GridBagConstraints.HORIZONTAL;
gbc_cbxShapeType.insets = new Insets(0, 0, 5, 5);
gbc_cbxShapeType.gridx = 2;
gbc_cbxShapeType.gridy = 1;
contentPane.add(cbxShapeType, gbc_cbxShapeType);
panelShapeDrawing = new JPanel();
panelShapeDrawing.setLayout(new BorderLayout());
String title = "Shape Drawing";
Border border = BorderFactory.createTitledBorder(title);
panelShapeDrawing.setBorder(border);
GridBagConstraints gbc_panelShapeDrawing = new GridBagConstraints();
gbc_panelShapeDrawing.insets = new Insets(0, 0, 5, 0);
gbc_panelShapeDrawing.gridwidth = 10;
gbc_panelShapeDrawing.gridheight = 8;
gbc_panelShapeDrawing.fill = GridBagConstraints.BOTH;
gbc_panelShapeDrawing.gridx = 3;
gbc_panelShapeDrawing.gridy = 1;
contentPane.add(panelShapeDrawing, gbc_panelShapeDrawing);
lblFillType = new JLabel("Fill Type");
GridBagConstraints gbc_lblFillType = new GridBagConstraints();
gbc_lblFillType.fill = GridBagConstraints.HORIZONTAL;
gbc_lblFillType.insets = new Insets(0, 0, 5, 5);
gbc_lblFillType.gridx = 1;
gbc_lblFillType.gridy = 2;
contentPane.add(lblFillType, gbc_lblFillType);
String[] fillTypes = {"", "Solid", "Hollow"};
cbxFillType = new JComboBox(fillTypes);
GridBagConstraints gbc_cbxFillType = new GridBagConstraints();
gbc_cbxFillType.insets = new Insets(0, 0, 5, 5);
gbc_cbxFillType.fill = GridBagConstraints.BOTH;
gbc_cbxFillType.gridx = 2;
gbc_cbxFillType.gridy = 2;
contentPane.add(cbxFillType, gbc_cbxFillType);
lblColor = new JLabel("Color");
GridBagConstraints gbc_lblColor = new GridBagConstraints();
gbc_lblColor.fill = GridBagConstraints.HORIZONTAL;
gbc_lblColor.insets = new Insets(0, 0, 5, 5);
gbc_lblColor.gridx = 1;
gbc_lblColor.gridy = 3;
contentPane.add(lblColor, gbc_lblColor);
String[] supportedColors = {"", "Red", "Black", "Orange", "Yellow", "Green", "Blue", "Magenta"};
cbxColor = new JComboBox(supportedColors);
GridBagConstraints gbc_cbxColor = new GridBagConstraints();
gbc_cbxColor.insets = new Insets(0, 0, 5, 5);
gbc_cbxColor.fill = GridBagConstraints.HORIZONTAL;
gbc_cbxColor.gridx = 2;
gbc_cbxColor.gridy = 3;
contentPane.add(cbxColor, gbc_cbxColor);
lblWidth = new JLabel("Width");
GridBagConstraints gbc_lblWidth = new GridBagConstraints();
gbc_lblWidth.fill = GridBagConstraints.HORIZONTAL;
gbc_lblWidth.insets = new Insets(0, 0, 5, 5);
gbc_lblWidth.gridx = 1;
gbc_lblWidth.gridy = 4;
contentPane.add(lblWidth, gbc_lblWidth);
tfWidth = new JTextField();
GridBagConstraints gbc_tfWidth = new GridBagConstraints();
gbc_tfWidth.fill = GridBagConstraints.BOTH;
gbc_tfWidth.insets = new Insets(0, 0, 5, 5);
gbc_tfWidth.gridx = 2;
gbc_tfWidth.gridy = 4;
contentPane.add(tfWidth, gbc_tfWidth);
tfWidth.setColumns(10);
lblHeight = new JLabel("Height");
GridBagConstraints gbc_lblHeight = new GridBagConstraints();
gbc_lblHeight.fill = GridBagConstraints.HORIZONTAL;
gbc_lblHeight.insets = new Insets(0, 0, 5, 5);
gbc_lblHeight.gridx = 1;
gbc_lblHeight.gridy = 5;
contentPane.add(lblHeight, gbc_lblHeight);
tfHeight = new JTextField();
tfHeight.setColumns(10);
GridBagConstraints gbc_tfHeight = new GridBagConstraints();
gbc_tfHeight.insets = new Insets(0, 0, 5, 5);
gbc_tfHeight.fill = GridBagConstraints.BOTH;
gbc_tfHeight.gridx = 2;
gbc_tfHeight.gridy = 5;
contentPane.add(tfHeight, gbc_tfHeight);
lblxCoordinate = new JLabel("x coordinate");
GridBagConstraints gbc_lblxCoordinate = new GridBagConstraints();
gbc_lblxCoordinate.fill = GridBagConstraints.HORIZONTAL;
gbc_lblxCoordinate.insets = new Insets(0, 0, 5, 5);
gbc_lblxCoordinate.gridx = 1;
gbc_lblxCoordinate.gridy = 6;
contentPane.add(lblxCoordinate, gbc_lblxCoordinate);
tfxCoordinate = new JTextField();
tfxCoordinate.setColumns(10);
GridBagConstraints gbc_tfxCoordinate = new GridBagConstraints();
gbc_tfxCoordinate.insets = new Insets(0, 0, 5, 5);
gbc_tfxCoordinate.fill = GridBagConstraints.BOTH;
gbc_tfxCoordinate.gridx = 2;
gbc_tfxCoordinate.gridy = 6;
contentPane.add(tfxCoordinate, gbc_tfxCoordinate);
lblyCoordinate = new JLabel("y coordinate");
GridBagConstraints gbc_lblyCoordinate = new GridBagConstraints();
gbc_lblyCoordinate.fill = GridBagConstraints.HORIZONTAL;
gbc_lblyCoordinate.insets = new Insets(0, 0, 5, 5);
gbc_lblyCoordinate.gridx = 1;
gbc_lblyCoordinate.gridy = 7;
contentPane.add(lblyCoordinate, gbc_lblyCoordinate);
tfyCoordinate = new JTextField();
tfyCoordinate.setColumns(10);
GridBagConstraints gbc_tfyCoordinate = new GridBagConstraints();
gbc_tfyCoordinate.insets = new Insets(0, 0, 5, 5);
gbc_tfyCoordinate.fill = GridBagConstraints.BOTH;
gbc_tfyCoordinate.gridx = 2;
gbc_tfyCoordinate.gridy = 7;
contentPane.add(tfyCoordinate, gbc_tfyCoordinate);
btnDraw = new JButton("Draw");
GridBagConstraints gbc_btnDraw = new GridBagConstraints();
gbc_btnDraw.anchor = GridBagConstraints.EAST;
gbc_btnDraw.fill = GridBagConstraints.VERTICAL;
gbc_btnDraw.insets = new Insets(0, 0, 5, 5);
gbc_btnDraw.gridx = 2;
gbc_btnDraw.gridy = 8;
contentPane.add(btnDraw, gbc_btnDraw);
btnDraw.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Shape.Drawing customShape = new Shape.Drawing();
shapeType[0] = (String) cbxShapeType.getSelectedItem();
shapeFill[0] = (String) cbxFillType.getSelectedItem();
shapeColor[0] = (String) cbxColor.getSelectedItem();
Rectangle shapeDimensions = null;
Color color = null;
Shape shape = null;
for (String value : shapeType) {
if (value.equals("Rectangle")) {
shapeDimensions = new Rectangle(0, 0, 200, 200);
} else {
shapeDimensions = new Rectangle(40, 30, 100, 125);
}
}
for (String value : shapeFill) {
if (value.equals("Fill")) {
Shape fill = new Rectangle2D()
}
}
for (String s : shapeColor) {
if (s.equals("Red")) {
color = new Color(255, 0, 0);
} else if (s == "Black") {
color = new Color(0, 0, 0);
} else if (s == "Orange") {
color = new Color(255, 102, 0);
} else if (s == "Yellow") {
color = new Color(255, 255, 0);
} else if (s == "Green") {
color = new Color(0, 204, 0);
} else if (s == "Blue") {
color = new Color(0, 0, 255);
} else if (s == "Magenta") {
color = new Color(255, 0, 255);
}
}
Color finalColor = color;
Shape test = new Shape(shapeDimensions, finalColor, shape) {
@Override
void draw(Graphics graphicObject) {
}
};
test.setColor(color);
}
});
panelShapeDrawing.setVisible(true);
contentPane.setVisible(true);
Frame.setVisible(true);
} // end main
} // end PRJ3
此外,您在此處看到的類和方法是我們被建議使用的類和方法,
好吧,我不知道給了你什么以及你自己寫了什么,但我上面看到的令人困惑。
在我看來,您有 3 個屬性來控制繪畫:
所以你的Shape
類不應該擴展 Rectangle。 它應該只是一個具有上述 3 個屬性和您的draw(Graphics g)
方法的類。
請注意 JDK 有一個Shape
類,所以我不喜歡使用相同的名稱,因為它會引起混淆。
所以我會做這樣的事情:
public abstract class DrawableShape
{
protected Rectangle rectangle;
protected Color color;
protected boolean filled;
public DrawableShape(Rectangle rectangle, Color color, boolean filled)
{
this.rectangle = rectangle;
this.color = color;
this.filled = filled;
}
abstract void draw(Graphics g);
}
現在您需要為您的 Rectangle 和 Oval 對象實現繪畫代碼。 就像是:
public class DrawableRectangle extends DrawableShape
{
@Override
public void draw(Graphics g)
{
g.setColor( color );
if (filled)
g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
else
g.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
}
現在在您的DrawingPanel
您需要一個方法,例如:
public void setDrawableShape(DrawableShape drawableShape)
{
this.drawableShape = drawableShape;
repaint();
}
Ant繪制代碼需要繪制這個形狀:
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent( g );
drawableShape.draw();
}
然后在 ActionListener 代碼中,您需要將 DrawableShape 添加到您的 Drawing 類:
DrawableShape drawableShape = null;
if (value.equals("Rectangle"))
drawableShape = new DrawableRectangle(….);
else
drawableShape = new DrawableOval(...);
drawing.setDrawableShape( drawableShape );
上述結構將允許您在繪圖面板上僅繪制一個形狀。 如果您需要多個形狀,那么您需要修改您的繪圖類。 我在上面提供的關於“自定義繪畫方法”的鏈接顯示了如何做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.