JAVA画图板的问题

要用applet实现一个简单的画图板可以画圆,四边形,圆形,我写了一个程序但是每次点按钮的时候都会有一大堆错误求高手帮忙看一下应该怎么写publicclasspainti... 要用applet实现一个简单的画图板 可以画圆,四边形,圆形,我写了一个程序 但是每次点按钮的时候都会有一大堆错误 求高手帮忙看一下应该怎么写
public class painting extends JApplet{
private JRadioButton lineButton;
private JRadioButton rectangleButton;
private JRadioButton circleButton;
private int currentX = 0;
private int currentY = 0;
private int width = 0;
private int height = 0;
public Graphics g2;

public void init(){
setSize(700,500);
JPanel panel = buildPanel();
add(panel);

addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseMotionListener());
}

private JPanel buildPanel(){
lineButton = new JRadioButton("line");
rectangleButton = new JRadioButton("rectangel");
circleButton = new JRadioButton("circle");

ButtonGroup group = new ButtonGroup();
group.add(lineButton);
group.add(rectangleButton);
group.add(circleButton);

MyListener listener = new MyListener();
lineButton.addActionListener(listener);
rectangleButton.addActionListener(listener);
circleButton.addActionListener(listener);

JPanel panel = new JPanel();
panel.add(lineButton);
panel.add(rectangleButton);
panel.add(circleButton);
return panel;

}
public void paintComponent(Graphics g){
super.paintComponents(g);
g2=(Graphics2D)g;
}

private class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (lineButton.isSelected()){
Line2D line = new Line2D.Double();
line.setLine(currentX, currentY, width, height);
((Graphics2D) g2).draw(line);
}
else if (rectangleButton.isSelected()){
Rectangle2D rectangle = new Rectangle2D.Double();
rectangle.setRect(currentX, currentY, width, height);
((Graphics2D) g2).draw(rectangle);
}
else if (circleButton.isSelected()){
Ellipse2D circle=new Ellipse2D.Double();
circle.setFrame(currentX, currentY, width, height);
((Graphics2D) g2).draw(circle);
}
}

}

private class MyMouseListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
currentX = e.getX();
currentY = e.getY();
} }
}
private class MyMouseMotionListener extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e){
width = e.getX() - currentX;
height = e.getY() - currentY;

// Repaint the window.
repaint();
展开
 我来答
PPxuelianhua
2009-04-15 · TA获得超过282个赞
知道小有建树答主
回答量:135
采纳率:0%
帮助的人:0
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;

public class painting extends JApplet{
private JRadioButton lineButton;
private JRadioButton rectangleButton;
private JRadioButton circleButton;

PaintPanel paintPanel;
JPanel panel;

private int shape = 0;//这里是我加上去的如果shape==1,就画线,为2就画圆矩形,为3为来圆形
boolean isFirst = true;

public void init(){
//setSize(700,500);
lineButton = new JRadioButton("line");
rectangleButton = new JRadioButton("rectangel");
circleButton = new JRadioButton("circle");

ButtonGroup group = new ButtonGroup();
group.add(lineButton);
group.add(rectangleButton);
group.add(circleButton);

MyListener listener = new MyListener();
lineButton.addActionListener(listener);
rectangleButton.addActionListener(listener);
circleButton.addActionListener(listener);

panel = new JPanel();
panel.add(lineButton);
panel.add(rectangleButton);
panel.add(circleButton);

paintPanel = new PaintPanel();
paintPanel.setBackground(Color.blue);
add(panel,BorderLayout.NORTH);
add(paintPanel,BorderLayout.CENTER);

paintPanel.addMouseListener(
new MouseAdapter(){
public void mousePressed(MouseEvent e){
if(isFirst){
paintPanel.setCurrentXY(e.getX(), e.getY());
isFirst = false;
}
else{
paintPanel.setLastXY(e.getX(),e.getY());
paintPanel.repaint();
isFirst = true;
}
}

public void mouseReleased(MouseEvent e){

}
});
paintPanel.addMouseMotionListener(
new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){

paintPanel.setLastXY(e.getX(),e.getY());
isFirst = true;

// Repaint the window.
paintPanel.repaint();
}
});
}

private class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if (e.getSource() == lineButton){
shape = 1;
//paintPanel.repaint();
}
else if (e.getSource()==rectangleButton){
shape = 2;
//paintPanel.repaint();
}
else if (e.getSource()==circleButton){
shape = 3;
//paintPanel.repaint();
}
}

}

public class PaintPanel extends JPanel{
private int currentX = 100;
private int currentY = 100;
private int lastX = 100;
private int lastY = 100;
//public Graphics g2;
public void paintComponent(Graphics g){

super.paintComponents(g);
Graphics2D g2=(Graphics2D)g; //这里要先声明类型,因为在头部我已经把//public Graphics g2; 删了。wich
switch(shape){//判断要画什么图
case 1 :g2.draw(new Line2D.Double(currentX, currentY, lastX, lastY));break;
case 2 :g2.draw(new Rectangle2D.Double(currentX, currentY,
Math.abs(currentX-lastX), Math.abs(currentY-lastY)));break;
case 3 :g2.draw(new Ellipse2D.Double(currentX, currentY,
Math.abs(currentX-lastX), Math.abs(currentY-lastY)));break;

}
}

public void setCurrentXY(int x,int y){
currentX = x;
currentY = y;
}

public void setLastXY(int w,int h ){
lastX = w;
lastY = h;
}

public int getX(){
return currentX;

}

public int getY(){
return currentY;
}
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式