用java实现的一个简单的画板程序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

一个实现简单画板功能的java程序,可以画直线,长方形,椭圆,曲线。

主类

import java.awt.Color;

import javax.swing.*;

public class painter {

public static void main(String [] args){

JPanel jpl = new Board();

JFrame painter =new JFrame();

painter.add(jpl);

painter.setBackground(Color.WHITE);

painter.setTitle("Painter");

painter.setSize(400,200);

painter.setLocationRelativeTo(null);

painter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

painter.setVisible(true);

}

}

加了选择事件的画板

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

@SuppressWarnings("serial")

public class Choose extends Board{

JRadioButton jrb1=new JRadioButton("Line");

JRadioButton jrb2=new JRadioButton("Rectangle");

JRadioButton jrb3=new JRadioButton("Oval");

JRadioButton jrb4=new JRadioButton("Curve");

JButton jbt = new JButton("Clear");

public Choose(){

JPanel jp1=new JPanel();

jp1.setLayout(new GridLayout(5,1,5,5));

jp1.add(jrb1);

jp1.add(jrb2);

jp1.add(jrb3);

jp1.add(jrb4);

jp1.add(jbt);

add(jp1);

ButtonGroup group = new ButtonGroup();

group.add(jrb1);

group.add(jrb2);

group.add(jrb3);

group.add(jrb4);

ActionListener chooseListener = new chooseListener();

MouseListener clearListener = new clearListener();

jrb1.addActionListener(chooseListener);

jrb2.addActionListener(chooseListener);

jrb3.addActionListener(chooseListener);

jrb4.addActionListener(chooseListener);

jbt.addMouseListener(clearListener);

}

class chooseListener implements ActionListener{

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jrb1){

drawLine();

System.out.println("line");

}

else if(e.getSource()==jrb2){

drawRectangle();

System.out.println("rec");

}

else if(e.getSource()==jrb3){

drawOval();

System.out.println("oval");

}

else if(e.getSource()==jrb4){

drawCurve();

System.out.println("curve");

}

}

}

class clearListener extends MouseAdapter{

public void mouseClicked(MouseEvent e){

drawClear();

}

}

}

画板

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.awt.image.ImageProducer;

import javax.swing.*;

@SuppressWarnings("serial")

public class Board extends JPanel{

JRadioButton jrb1=new JRadioButton("Line");

JRadioButton jrb2=new JRadioButton("Rectangle");

JRadioButton jrb3=new JRadioButton("Oval");

JRadioButton jrb4=new JRadioButton("Curve");

JButton jbt = new JButton("Clear");

BufferedImage bi=new BufferedImage(500,500,BufferedImage.TYPE_INT_RGB);;

int x=-1;

int y=-1;

int [] location=new int[4];

int mode=0;

boolean ifNotCurveClicked=false;

boolean ifCurveClicked=false;

public Board(){

setLayout(new BorderLayout());

JPanel jp1=new JPanel();

jp1.setLayout(new GridLayout(5,1,5,5));

jp1.add(jrb1);

jp1.add(jrb2);

jp1.add(jrb3);

jp1.add(jrb4);

jp1.add(jbt);

add(jp1,BorderLayout.WEST);

ButtonGroup group = new ButtonGroup();

group.add(jrb1);

group.add(jrb2);

group.add(jrb3);

group.add(jrb4);

ActionListener chooseListener = new chooseListener();

MouseListener clearListener = new clearListener();

jrb1.addActionListener(chooseListener);

jrb2.addActionListener(chooseListener);

jrb3.addActionListener(chooseListener);

jrb4.addActionListener(chooseListener);

jbt.addMouseListener(clearListener);

}

public void drawLine(){

addMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent e){

location[0]=e.getX();

相关文档
最新文档