java 画图覆盖_请教如何在java画图中不覆盖原来的画图???
展開(kāi)全部
import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; //import javax.swing.Component;publicclass ShapeMain extends JFrame implements ActionListener,MouseListener,MouseMotionListener{ int x,y,x1,y1,x2,y2,width,height; boolean isFirstPoint = true; //初始化開(kāi)始畫(huà)的是線int drawType = PaintingGround.LINE; //初始化開(kāi)始不是填充boolean isFill = false; //添加控件 ButtonGroup btg = new ButtonGroup(); Button btLine = new Button("線"); Button btRectangle = new Button("矩形32313133353236313431303231363533e58685e5aeb931333332636435"); Button btRound = new Button("圓"); Button btEllipse = new Button("橢圓"); Button tbFillState = new Button("填充"); Button button3 = new Button("文本操作"); Button button2 = new Button("清除"); Button button1 = new Button("選擇顏色"); Panel buttonPanel = new Panel(); PaintingGround paintingGround = new PaintingGround(); //Main Methodpublicstaticvoid main(String[] args) { //設(shè)置顯示外觀try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e) { e.printStackTrace(); } new ShapeMain(); } //構(gòu)造函數(shù)public ShapeMain() { //控件添加到控件組中 // btg.add(btLine); // btg.add(btRectangle); // btg.add(btRound); // btg.add(btEllipse); buttonPanel.add(btLine); buttonPanel.add(btRectangle); buttonPanel.add(btRound); buttonPanel.add(btEllipse); buttonPanel.add(tbFillState); //設(shè)置容器及容器的整體布局 Container cp = this; cp.setLayout(new BorderLayout()); cp.add(BorderLayout.NORTH,buttonPanel); cp.add(BorderLayout.CENTER,paintingGround); //cp.add(BorderLayout.SOUTH,jf); //jf.setJMenuBar(mb); setLocation(300,150); setSize(600,480); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); //添加鼠標(biāo)觸發(fā)事件 paintingGround.addMouseListener(new MouseAdapter() { publicvoid mouseReleased(MouseEvent evn) { isFirstPoint = true; } }); //對(duì)鼠標(biāo)的輸入進(jìn)行判斷并調(diào)用畫(huà)圖程序 paintingGround.addMouseMotionListener(new MouseMotionAdapter() { publicvoid mouseDragged(MouseEvent evn) { if(isFirstPoint) { x1 = evn.getX(); y1 = evn.getY(); isFirstPoint = false; } else { x2 = evn.getX(); y2 = evn.getY(); switch(drawType) { case PaintingGround.LINE: //畫(huà)線paintingGround.drawLine(x1,y1,x2,y2); break; case PaintingGround.RECTANGLE: //畫(huà)矯形 paintingGround.drawRect(x1,y1,x2-x1,y2-y1); break; case PaintingGround.ROUND: //畫(huà)圓//兩點(diǎn)距離公式int size = Math.abs((int)Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))); paintingGround.drawRound(x1,y1,size); break; case PaintingGround.ELLIPSE: //畫(huà)橢圓 paintingGround.drawEllipse(x1,y1,x2-x1,y2-y1); break; default: break; } } } }); //各個(gè)控件的觸發(fā)事件 btLine.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { drawType = PaintingGround.LINE; } }); btRectangle.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { drawType = PaintingGround.RECTANGLE; } }); btRound.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { drawType = PaintingGround.ROUND; } }); btEllipse.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { drawType = PaintingGround.ELLIPSE; } }); tbFillState.addActionListener(new ActionListener(){ publicvoid actionPerformed(ActionEvent evn) { isFill = tbFillState.isShowing(); paintingGround.setFillState(isFill); } }); } publicvoid actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } publicvoid mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mousePressed(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseExited(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseDragged(MouseEvent e) { // TODO Auto-generated method stub } publicvoid mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } } class PaintingGround extends JPanel { publicstaticfinalint LINE = 1; publicstaticfinalint RECTANGLE = 2; publicstaticfinalint ROUND = 3; publicstaticfinalint ELLIPSE = 4; privateint x,y; privateint x1,y1,x2,y2; privateint width, height,size; privateint drawType = 0; privateboolean isFill = false; //構(gòu)造函數(shù)public PaintingGround() { setBackground(Color.white); } //判斷是用實(shí)心還是空心的,publicvoid paint(Graphics g) { //super.paintComponent(g); //super.paint(g);super.paint(g); g.setColor(Color.black); if(isFill) { switch(drawType) { case LINE: g.drawLine(x1,y1,x2,y2); break; case RECTANGLE: g.fillRect(x,y,width,height); break; case ROUND: g.fillOval(x,y,size,size); break; case ELLIPSE: g.fillOval(x,y,width,height); break; default: break; } } else { switch(drawType) { case LINE: g.drawLine(x1,y1,x2,y2); break; case RECTANGLE: g.drawRect(x,y,width,height); break; case ROUND: g.drawOval(x,y,size,size); break; case ELLIPSE: g.drawOval(x,y,width,height); break; default: break; } } } publicvoid drawLine(int x1, int y1, int x2,int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; drawType = LINE; //jim isFill = false; //jimrepaint(); } //具體的實(shí)現(xiàn)方式publicvoid drawRect(int x,int y,int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; drawType = RECTANGLE; repaint(); } publicvoid drawRound(int x,int y,int size) { this.x = x; this.y = y; this.size = size; drawType = ROUND; repaint(); } publicvoid drawEllipse(int x,int y,int width,int height) { this.x = x; this.y = y; this.width = width; this.height = height; drawType = ELLIPSE; repaint(); } publicvoid setFillState(boolean isFill) { this.isFill = isFill; } }
本回答被提問(wèn)者采納
已贊過(guò)
已踩過(guò)<
你對(duì)這個(gè)回答的評(píng)價(jià)是?
評(píng)論
收起
總結(jié)
以上是生活随笔為你收集整理的java 画图覆盖_请教如何在java画图中不覆盖原来的画图???的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java项目打成jar和war_mave
- 下一篇: java velocity是什么意思,什