JavaScript (Canvas):
```javascript
function drawSquare(sideLength, color) {
const ctx = document.getElementById('myCanvas').getContext('2d');
ctx.beginPath();
ctx.fillStyle = color;
for (let i = 0; i < 4; i++) {
if (i === 0) {
ctx.moveTo(0, 0);
}
ctx.lineTo(sideLength * (i + 1), 0);
ctx.lineTo(sideLength * (i + 1), sideLength);
ctx.lineTo(0, sideLength);
ctx.closePath();
}
ctx.fill();
}
```
Python (Turtle):
```python
import turtle
screen = turtle.Screen()
my_turtle = turtle.Turtle()
for _ in range(4):
my_turtle.forward(100)
my_turtle.right(90)
screen.mainloop()
```
Java (Swing):
```java
import javax.swing.*;
import java.awt.*;
public class SquareDrawing {
public static void main(String[] args) {
JFrame frame = new JFrame("Draw Square");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 100);
}
};
frame.add(panel);
frame.setVisible(true);
}
}
```
C (Windows Forms):
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
public class SquareDrawingForm : Form {
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
e.Graphics.FillRectangle(Brushes.Red, new Rectangle(50, 50, 100, 100));
}
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new SquareDrawingForm());
}
}
```
这些示例展示了如何在不同的编程环境中使用不同的库和方法来绘制一个正方形。你可以根据自己的需求和使用的编程语言选择合适的方法。