亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

基于java怎么實現畫圖板功能

小億
90
2024-03-05 21:30:02
欄目: 編程語言

要實現畫圖板功能,可以使用Java的Swing庫來創建一個簡單的繪圖應用。以下是一個基本的示例代碼,用于實現畫圖板功能:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DrawingBoard extends JFrame {

    private int startX, startY, endX, endY;
    private Color currentColor = Color.BLACK;

    public DrawingBoard() {
        setTitle("Drawing Board");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel controlPanel = new JPanel();
        JButton redButton = new JButton("Red");
        redButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentColor = Color.RED;
            }
        });

        JButton blackButton = new JButton("Black");
        blackButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentColor = Color.BLACK;
            }
        });

        controlPanel.add(redButton);
        controlPanel.add(blackButton);

        JPanel drawingPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(currentColor);
                g.drawLine(startX, startY, endX, endY);
            }
        };

        drawingPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                startX = e.getX();
                startY = e.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                endX = e.getX();
                endY = e.getY();
                drawingPanel.repaint();
            }
        });

        getContentPane().add(controlPanel, BorderLayout.NORTH);
        getContentPane().add(drawingPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DrawingBoard drawingBoard = new DrawingBoard();
                drawingBoard.setVisible(true);
            }
        });
    }
}

此代碼創建了一個簡單的繪圖板應用程序,用戶可以在畫板上繪制直線,并可以選擇繪制的顏色。用戶可以使用鼠標按下并拖動來繪制線條。您可以根據需要擴展此示例代碼,以實現更復雜的繪圖功能,如繪制不同形狀、使用不同的畫筆風格等。

0
加查县| 沙洋县| 九寨沟县| 塘沽区| 郎溪县| 汾阳市| 洱源县| 通辽市| 大埔区| 北碚区| 南陵县| 榆社县| 沂南县| 通城县| 仁寿县| 牟定县| 涟水县| 滨州市| 华宁县| 连江县| 小金县| 福贡县| 鄂托克旗| 藁城市| 丹巴县| 南昌县| 瓦房店市| 潼南县| 新竹市| 黄平县| 游戏| 讷河市| 巴彦县| 黄石市| 北川| 庆元县| 卢湾区| 城口县| 牙克石市| 吴川市| 绥滨县|