请写一个Java程序,画一个红色的圆和一个黄色的直线。
时间: 2023-05-20 08:07:19 浏览: 137
下面是一个绘制红色圆和黄色直线的Java程序:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
public void paint(Graphics g) {
// 绘制红色圆形
g.setColor(Color.RED);
g.fillOval(50, 50, 100, 100);
// 绘制黄色直线
g.setColor(Color.YELLOW);
g.drawLine(50, 50, 150, 150);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Java程序绘制红色圆和黄色直线");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
Main panel = new Main();
frame.add(panel);
frame.setVisible(true);
}
}
相关问题
用Java制作一个简易的画图程序,要求有七种颜色,有矩形,直线,椭圆
好的,以下是一个简单的Java画图程序,包含七种颜色,可以绘制矩形、直线、椭圆。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimplePaint extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
private int startX, startY, endX, endY;
private int currentShape;
private int currentColor;
private JButton[] colorButtons;
private JButton[] shapeButtons;
private JPanel colorPanel;
private JPanel shapePanel;
private JPanel controlPanel;
private DrawingArea drawingArea;
public SimplePaint() {
setTitle("Simple Paint");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
// 初始化画布和颜色、形状按钮
drawingArea = new DrawingArea();
colorButtons = new JButton[7];
colorButtons[0] = new JButton("Black");
colorButtons[1] = new JButton("Red");
colorButtons[2] = new JButton("Orange");
colorButtons[3] = new JButton("Yellow");
colorButtons[4] = new JButton("Green");
colorButtons[5] = new JButton("Blue");
colorButtons[6] = new JButton("Purple");
for (int i = 0; i < colorButtons.length; i++) {
colorButtons[i].addActionListener(this);
}
shapeButtons = new JButton[3];
shapeButtons[0] = new JButton("Rectangle");
shapeButtons[1] = new JButton("Line");
shapeButtons[2] = new JButton("Oval");
for (int i = 0; i < shapeButtons.length; i++) {
shapeButtons[i].addActionListener(this);
}
// 设置颜色、形状按钮面板
colorPanel = new JPanel();
colorPanel.setLayout(new GridLayout(1, colorButtons.length));
for (int i = 0; i < colorButtons.length; i++) {
colorPanel.add(colorButtons[i]);
}
shapePanel = new JPanel();
shapePanel.setLayout(new GridLayout(1, shapeButtons.length));
for (int i = 0; i < shapeButtons.length; i++) {
shapePanel.add(shapeButtons[i]);
}
// 设置控制面板
controlPanel = new JPanel();
controlPanel.setLayout(new BorderLayout());
controlPanel.add(colorPanel, BorderLayout.NORTH);
controlPanel.add(shapePanel, BorderLayout.SOUTH);
// 添加组件
add(drawingArea, BorderLayout.CENTER);
add(controlPanel, BorderLayout.NORTH);
// 添加鼠标事件监听器
drawingArea.addMouseListener(this);
drawingArea.addMouseMotionListener(this);
}
public void actionPerformed(ActionEvent e) {
// 处理颜色按钮事件
for (int i = 0; i < colorButtons.length; i++) {
if (e.getSource() == colorButtons[i]) {
currentColor = i;
return;
}
}
// 处理形状按钮事件
for (int i = 0; i < shapeButtons.length; i++) {
if (e.getSource() == shapeButtons[i]) {
currentShape = i;
return;
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
// 绘制形状
Graphics g = drawingArea.getGraphics();
g.setColor(getColor(currentColor));
switch (currentShape) {
case 0: // 矩形
g.drawRect(Math.min(startX, endX), Math.min(startY, endY), Math.abs(endX - startX), Math.abs(endY - startY));
break;
case 1: // 直线
g.drawLine(startX, startY, endX, endY);
break;
case 2: // 椭圆
g.drawOval(Math.min(startX, endX), Math.min(startY, endY), Math.abs(endX - startX), Math.abs(endY - startY));
break;
}
g.dispose();
}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
private Color getColor(int index) {
switch (index) {
case 0: // 黑色
return Color.BLACK;
case 1: // 红色
return Color.RED;
case 2: // 橙色
return Color.ORANGE;
case 3: // 黄色
return Color.YELLOW;
case 4: // 绿色
return Color.GREEN;
case 5: // 蓝色
return Color.BLUE;
case 6: // 紫色
return new Color(128, 0, 128);
default:
return Color.BLACK;
}
}
public static void main(String[] args) {
SimplePaint paint = new SimplePaint();
paint.setVisible(true);
}
// 画布面板
class DrawingArea extends JPanel {
public DrawingArea() {
setPreferredSize(new Dimension(400, 300));
setBackground(Color.WHITE);
}
}
}
```
这个程序使用了Java的Swing图形库,创建了一个窗口,在窗口中添加了一个画布和颜色、形状按钮。通过鼠标事件监听器实现了绘制矩形、直线、椭圆的功能,通过颜色按钮实现了七种颜色的选择。您可以根据需要对程序进行修改和完善。
java图形绘制程序
Java提供了许多绘制图形的API,其中最常用的是Java Swing库和Java 2D库。以下是一个简单的Java Swing程序,它绘制了一个红色的矩形:
```java
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
public MyFrame() {
setTitle("My Drawing");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillRect(100, 100, 200, 200);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
```
该程序创建了一个名为"My Drawing"的窗口,并在窗口中绘制了一个红色的矩形。要使用Java 2D库进行绘图,可以使用Graphics2D对象代替Graphics对象。Graphics2D对象提供了更多的绘图方法和功能,例如绘制图形、设置渐变、应用变换等。以下是一个使用Java 2D库的示例程序,绘制了一个渐变的矩形:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class MyFrame extends JFrame {
public MyFrame() {
setTitle("My Drawing");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradient = new GradientPaint(0, 0, Color.RED, 200, 200, Color.YELLOW);
g2d.setPaint(gradient);
g2d.fillRect(100, 100, 200, 200);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
}
}
```
该程序使用GradientPaint对象创建了一个红色到黄色的渐变,并将其应用于矩形的填充。您可以使用Java Swing和Java 2D库创建各种图形,例如直线、圆、椭圆、多边形等。
阅读全文