怎样在Java中实现绘图时的橡皮线效果
答案:1 悬赏:30 手机版
解决时间 2021-01-24 21:49
- 提问者网友:感性作祟
- 2021-01-24 12:57
怎样在Java中实现绘图时的橡皮线效果
最佳答案
- 五星知识达人网友:第四晚心情
- 2021-01-24 13:28
import java.awt.geom.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DrawPanel extends JFrame{
LinkedList shapeList =new LinkedList();
Shape shape;
Stroke stroke =new BasicStroke(1.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,
0,new float[]{8f,3f},1);
Point start,end;
final String[] type =new String[]{\"Line\",\"Rectangle\",\"Ellipse\"};
JComboBox comboBox =new JComboBox(type);
public DrawPanel(){
super(\"DrawPanel\");
JPanel panel =new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
g2.setColor(Color.white);
g2.fillRect(0,0,getWidth(),getHeight());
g2.setColor(Color.black);
for(Shape s:shapeList) g2.draw(s);
if(shape!=null){
g2.setStroke(stroke);
g2.setPaint(Color.blue);
g2.draw(shape);
}
}
};
panel.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
start =e.getPoint();
}
public void mouseReleased(MouseEvent e){
if(shape!=null) shapeList.add(shape);
shape =null;
repaint();
}
});
panel.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
end =e.getPoint();
Object select =comboBox.getSelectedItem();
if(select.equals(type[0])) shape =new Line2D.Float(start,end);
else{
if(select.equals(type[1])) shape =new Rectangle();
else shape =new Ellipse2D.Float();
((RectangularShape)shape).setFrameFromDiagonal(start,end);
}
repaint();
}
});
panel.setPreferredSize(new Dimension(320,240));
add(panel,BorderLayout.NORTH);
add(comboBox,BorderLayout.SOUTH);
pack();
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
new DrawPanel();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class DrawPanel extends JFrame{
LinkedList
Shape shape;
Stroke stroke =new BasicStroke(1.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,
0,new float[]{8f,3f},1);
Point start,end;
final String[] type =new String[]{\"Line\",\"Rectangle\",\"Ellipse\"};
JComboBox comboBox =new JComboBox(type);
public DrawPanel(){
super(\"DrawPanel\");
JPanel panel =new JPanel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g;
g2.setColor(Color.white);
g2.fillRect(0,0,getWidth(),getHeight());
g2.setColor(Color.black);
for(Shape s:shapeList) g2.draw(s);
if(shape!=null){
g2.setStroke(stroke);
g2.setPaint(Color.blue);
g2.draw(shape);
}
}
};
panel.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
start =e.getPoint();
}
public void mouseReleased(MouseEvent e){
if(shape!=null) shapeList.add(shape);
shape =null;
repaint();
}
});
panel.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
end =e.getPoint();
Object select =comboBox.getSelectedItem();
if(select.equals(type[0])) shape =new Line2D.Float(start,end);
else{
if(select.equals(type[1])) shape =new Rectangle();
else shape =new Ellipse2D.Float();
((RectangularShape)shape).setFrameFromDiagonal(start,end);
}
repaint();
}
});
panel.setPreferredSize(new Dimension(320,240));
add(panel,BorderLayout.NORTH);
add(comboBox,BorderLayout.SOUTH);
pack();
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
new DrawPanel();
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯