java - 如何将paintComponent 方法连接到JButton 以绘制矩形或椭圆?
问题描述
我遇到的这个问题是试图让用户从 GUI Combobox 下拉框中选择 #1 框(选择椭圆形或矩形)、#2 框(填充或空心)、#3 框(选择颜色:黑色、蓝色、黄色或灰色)。Textfield #1 用户可以输入高度,TextField #2 用户可以输入宽度,TextField #3 用户可以输入 x 坐标,TextField #4 用户可以输入 y 坐标。绘制按钮调用paintComponent方法让所有用户选择他们想要制作的东西,椭圆形或矩形,以便它可以显示在面板上。
public class Drawing extends JPanel{
public int currentShape;
public void paintComponent(Graphics g) {
super.paintComponent(g);
int x1 = 0;
int y1 = 0;
g.drawRect(x1, y1, getWidth(), getHeight());
g.getColor();
g.fillRect(getX(), getY(), getWidth(), getHeight());
//g.drawOval(x, y, width, height);
g.getColor();
g.fillOval(getX(), getY(), getWidth(), getHeight());
}
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public void drawShape(int currentShape) throws OutsideBounds {
}
}
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Project3 {
private JFrame frame;
private JTextField widthTextField;
private JTextField heightTextField;
private JTextField xCoordinateTextField;
private JTextField yCoordinateTextField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Project3 window = new Project3();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Project3() {
initialize();
}
/**
* Initialize the contents of the frame
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("Geometric Drawing");
frame.setBounds(100, 100, 536, 373);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblShapeType = new JLabel("Shape Type");
JLabel lblFillType = new JLabel("Fill Type");
JLabel lblColor = new JLabel("Color");
JLabel lblWidth = new JLabel("Width");
JLabel lblHeight = new JLabel("Height");
JLabel lblXCoordinate = new JLabel("x coordinate");
JLabel lblYCoordinate = new JLabel("y coordinate");
//String[] shapeString = { "Oval", "Rectangle"};
JComboBox<String> shapeTypeComboBox = new JComboBox<String>();
//Drawing oval = (Drawing) shapeTypeComboBox.getSelectedItem();
shapeTypeComboBox.addItem("Oval");
shapeTypeComboBox.addItem("Rectangle");
JComboBox<String> fillTypeComboBox = new JComboBox<String>();
fillTypeComboBox.addItem("Solid");
fillTypeComboBox.addItem("Hollow");
JComboBox<String> colorComboBox = new JComboBox<String>();
colorComboBox.addItem("Black");
colorComboBox.addItem("Red");
colorComboBox.addItem("Orange");
colorComboBox.addItem("Yellow");
colorComboBox.addItem("Green");
colorComboBox.addItem("Blue");
colorComboBox.addItem("Magenta");
widthTextField = new JTextField();
widthTextField.setColumns(10);
heightTextField = new JTextField();
heightTextField.setColumns(10);
xCoordinateTextField = new JTextField();
xCoordinateTextField.setColumns(10);
yCoordinateTextField = new JTextField();
yCoordinateTextField.setColumns(10);
JPanel drawPanel = new JPanel();
TitledBorder titled = new TitledBorder("Shape Drawing");
drawPanel.setBorder(titled);
JButton btnDraw = new JButton("Draw");
btnDraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
解决方案
推荐阅读
- reactjs - 如何使用反应样式组件添加自定义 CSS
- gradle - gradle 中 JUnit 5 和 JUnit 4 的不同配置
- reactjs - 你可以将 scss 文件作为道具传递给另一个反应组件来使用吗?
- c# - WCF XML反序列化不填充数组
- r - 使用 dplyr 根据模式过滤数据帧并重新排序
- android - 无法使用 Google 应用签名测试应用内交易
- tfs - 如何为拉取请求重新执行 TFS CI 构建集
- sql - 两条sql语句的区别
- graphql - 在 Python Graphene 中使用模式中的不同类型执行嵌套查询
- java - javax.net.ssl.SSLHandshakeException:远程主机终止握手