要制作一个建议的画板,界面已经建立了,就是不知道怎么在自己创建的panel上画画,求教
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.awt.geom.Line2D;import javax.swing.undo.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
class Contain extends JFrame implements ActionListener,MouseListener,MouseMotionListener,ItemListener{
private JPanel p,drawpanel;
private JLabel label;
private JButton undo,clear;
private JComboBox scolor,shape;
private JCheckBox fill;
private final String namecolor[]={"Red","Black","Green","Yellow"};
private final String nameshape[]={"Rectangle","Oval","Line"};
private int x1,y1,x2,y2;
private String color,sshape,isfill;
public Contain()
{
super("Java Drawings");
undo=new JButton("Undo");
undo.addActionListener(this);
clear=new JButton("Clear");
clear.addActionListener(this);
scolor=new JComboBox(namecolor);
scolor.addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
color=namecolor[scolor.getSelectedIndex()];
}
}
});
shape=new JComboBox(nameshape);
shape.addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
sshape=nameshape[shape.getSelectedIndex()];
}
}
});
fill=new JCheckBox("filled");
p=new JPanel();
drawpanel=new JPanel();
drawpanel.setBackground(Color.white);
p.setLayout(new FlowLayout());
p.add(undo);
p.add(clear);
p.add(scolor);
p.add(shape);
p.add(fill);
add(p,BorderLayout.NORTH);
label=new JLabel(" ");
drawpanel.addMouseMotionListener(this);
drawpanel.addMouseListener(this);
add(drawpanel,BorderLayout.CENTER);
add(label,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==clear)
{
}
if(e.getSource()==undo)
{
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
label.setText("the mouse has left the drawpanel.");
}
@Override
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
Line2D.Double line=new Line2D.Double(x1,y1,x2,y2);
}
@Override
public void mouseDragged(MouseEvent e)
{
}
@Override
public void mouseMoved(MouseEvent e) {
label.setText("("+e.getX()+","+e.getY()+")");
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==fill)
{
if(fill.isSelected())
{
isfill="fill";
}
else isfill="unfill";
}
}}
如何给panel加上鼠标绘画的方法
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.awt.geom.Line2D;import javax.swing.undo.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
class Contain extends JFrame implements ActionListener,MouseListener,MouseMotionListener,ItemListener{
private JPanel p,drawpanel;
private JLabel label;
private JButton undo,clear;
private JComboBox scolor,shape;
private JCheckBox fill;
private final String namecolor[]={"Red","Black","Green","Yellow"};
private final String nameshape[]={"Rectangle","Oval","Line"};
private int x1,y1,x2,y2;
private String color,sshape,isfill;
public Contain()
{
super("Java Drawings");
undo=new JButton("Undo");
undo.addActionListener(this);
clear=new JButton("Clear");
clear.addActionListener(this);
scolor=new JComboBox(namecolor);
scolor.addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
color=namecolor[scolor.getSelectedIndex()];
}
}
});
shape=new JComboBox(nameshape);
shape.addItemListener(
new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==ItemEvent.SELECTED)
{
sshape=nameshape[shape.getSelectedIndex()];
}
}
});
fill=new JCheckBox("filled");
p=new JPanel();
drawpanel=new JPanel();
drawpanel.setBackground(Color.white);
p.setLayout(new FlowLayout());
p.add(undo);
p.add(clear);
p.add(scolor);
p.add(shape);
p.add(fill);
add(p,BorderLayout.NORTH);
label=new JLabel(" ");
drawpanel.addMouseMotionListener(this);
drawpanel.addMouseListener(this);
add(drawpanel,BorderLayout.CENTER);
add(label,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==clear)
{
}
if(e.getSource()==undo)
{
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
label.setText("the mouse has left the drawpanel.");
}
@Override
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
Line2D.Double line=new Line2D.Double(x1,y1,x2,y2);
}
@Override
public void mouseDragged(MouseEvent e)
{
}
@Override
public void mouseMoved(MouseEvent e) {
label.setText("("+e.getX()+","+e.getY()+")");
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==fill)
{
if(fill.isSelected())
{
isfill="fill";
}
else isfill="unfill";
}
}}
如何给panel加上鼠标绘画的方法
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class Contain extends JFrame implements ActionListener, MouseListener,
MouseMotionListener, ItemListener {
private static final long serialVersionUID = -4684744217630434891L;
private JPanel p, drawpanel;
private JLabel label;
private JButton undo, clear;
private JComboBox scolor, shape;
private JCheckBox fill;
private final String namecolor[] = { "Red", "Black", "Green", "Yellow" };
private final String nameshape[] = { "Rectangle", "Oval", "Line" };
private int x1, y1, x2, y2;
private String color, sshape, isfill; public Contain() {
super("Java Drawings");
undo = new JButton("Undo");
undo.addActionListener(this);
clear = new JButton("Clear");
clear.addActionListener(this);
scolor = new JComboBox(namecolor);
scolor.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
color = namecolor[scolor.getSelectedIndex()];
}
}
});
shape = new JComboBox(nameshape);
shape.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
sshape = nameshape[shape.getSelectedIndex()];
}
}
});
fill = new JCheckBox("filled");
p = new JPanel();
drawpanel = new JPanel();
drawpanel.setBackground(Color.white);
p.setLayout(new FlowLayout());
p.add(undo);
p.add(clear);
p.add(scolor);
p.add(shape);
p.add(fill);
add(p, BorderLayout.NORTH);
label = new JLabel(" ");
drawpanel.addMouseMotionListener(this);
drawpanel.addMouseListener(this);
add(drawpanel, BorderLayout.CENTER);
add(label, BorderLayout.SOUTH);
this.setSize(300, 400);
this.setVisible(true);
} public static void main(String[] args) {
new Contain();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == clear) { }
if (e.getSource() == undo) { } } @Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
x1 = e.getX();
y1 = e.getY();
} @Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
// System.out.println("mouseEntered"); } @Override
public void mouseExited(MouseEvent e) {
label.setText("the mouse has left the drawpanel.");
// System.out.println("mouseExited"); } @Override
public void mousePressed(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
} @Override
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
repaint();
} @Override
public void mouseDragged(MouseEvent e) {
repaint();
} public void paint(Graphics g) {
g.setColor(Color.black);
g.drawLine(x1, y1 + 60, x2, y2 + 60);
} @Override
public void mouseMoved(MouseEvent e) {
label.setText("(" + e.getX() + "," + e.getY() + ")");
// System.out.println("mouseMoved");
repaint();
} @Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == fill) {
if (fill.isSelected()) {
isfill = "fill";
} else
isfill = "unfill";
}
}
}