java paint绘图添加组件不能显示_java window制作简单绘图工具 为什么没有显示啊?...

这篇博客介绍了如何使用Java Swing组件创建一个简单的绘图工具。通过`DrawingBoard`类,开发者可以实现画布面板,添加颜色选择器,并支持绘制直线、矩形和椭圆。用户在画布上按下鼠标时确定起点,拖动时更新终点,释放鼠标后形状绘制完成。程序通过`paintComponent`方法更新图形,并存储绘制的形状实例。
摘要由CSDN通过智能技术生成

package Drawing;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JComboBox;

import java.awt.Font;

import java.awt.Graphics;

import javax.swing.DefaultComboBoxModel;

import javax.swing.JPanel;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.Vector;

import java.awt.event.MouseMotionAdapter;

import java.awt.Color;

import javax.swing.border.LineBorder;

import java.awt.Window.Type;

public class DrawingBoard extends JPanel{

private JFrame frmDrawingboard;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

DrawingBoard window = new DrawingBoard();

window.frmDrawingboard.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the application.

*/

public DrawingBoard() {

initialize();

}

/**

* Initialize the contents of the frame.

*/

int x1, y1, x2, y2;

Vector vectorShapes = new Vector();

private void initialize() {

frmDrawingboard = new JFrame();

frmDrawingboard.setResizable(false);

frmDrawingboard.setTitle("DrawingBoard\r\n");

frmDrawingboard.getContentPane().setBackground(Color.DARK_GRAY);

frmDrawingboard.setBounds(100, 100, 464, 350);

frmDrawingboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frmDrawingboard.getContentPane().setLayout(null);

JComboBox comboBox = new JComboBox();

comboBox.setForeground(Color.RED);

comboBox.setBackground(Color.BLACK);

comboBox.setModel(new DefaultComboBoxModel(new String[] {"Line", "Rectangle", "Oval"}));

comboBox.setFont(new Font("Comic Sans MS", Font.BOLD, 16));

comboBox.setBounds(345, 280, 102, 21);

frmDrawingboard.getContentPane().add(comboBox);

JPanel panel = new JPanel();

panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));

panel.setBackground(Color.WHITE);

panel.addMouseListener(new MouseAdapter() {

@Override

public void mousePressed(MouseEvent e) {

x1 = e.getX();

y1 = e.getY();

Shapes c =new Shapes();

switch(comboBox.getSelectedIndex()){

case -1:c = new Line(x1,y1,x1,y1);break;

case 0:c = new Rectangle(x1,y1,x1,y1);break;

case 1:c = new Oval(x1,y1,x1,y1);break;

} //end switch

vectorShapes.add(c);

}

});

panel.addMouseMotionListener(new MouseMotionAdapter() {

@Override

public void mouseDragged(MouseEvent e) {

vectorShapes.get(vectorShapes.size()-1).x2 = e.getX();

vectorShapes.get(vectorShapes.size()-1).y2 = e.getY();

repaint();

}

});

panel.setBounds(10, 10, 437, 260);

frmDrawingboard.getContentPane().add(panel);

}

protected void paintComponent(Graphics g){

g.clearRect(0, 0, getWidth(), getHeight());

for(int i = 0; i < vectorShapes.size(); i++)

{

vectorShapes.get(i).draw(g);

}

}

}

9add55c8ec7457cd40411badcbfa2272.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值