2013-03-05 27 views
0

我一直在製作一個繪畫程序,並且我在光譜中放入了一個帶有每種顏色的顏色條,以便用戶可以在任何地方點擊,並且它會給它們(大致)所需的顏色。我的問題是,它的工作原理是完美的,但是當程序運行時,你必須重新調整窗口大小(AKA調用repaint())幾次,直到它真正起作用。這是一些冗長的代碼,但您可以瀏覽令人厭惡的布爾重置。Java重繪()使第二個畫布繪圖不出現

import java.applet.Applet; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.*; 
import java.awt.*; 


public class PaintHomework extends Applet implements ActionListener, MouseListener, MouseMotionListener 
{ 
    Canvas c1; 
    Canvas c2; 


    Graphics myG; 
    Graphics myG2; 

    TextField t1; 
    TextField currentColor; 
    TextField currentMode; 
    TextField currentShape; 

    int xForStraightLine=0; 
    int yForStraightLine=0; 
    int x = 0; 
    int y = 0; 

    int counter = 0; 
    int x11=1; 
    int x22=0; 
    int y11=1; 
    int y22=200; 

    int x1; 
    int y1; 
    int x2; 
    int y2; 
    int xSize=0; 
    int ySize=0; 

    float num1 = (float).01; 
    float num2 = (float)1.0; 
    float num3 = (float)1.0; 
    float hueNum=0; 

    int penSize = 10; 
    String text = ""; 

    Button red; 
    Button blue; 
    Button green; 

    Button oval; 
    Button rect; 
    Button sprayCan; 
    Button straightLine; 
    Button filled; 
    Button empty; 
    Button free; 
    Button dragOval; 
    Button dragRect; 

    Button reset; 

    boolean ovalTime; 
    boolean rectTime; 
    boolean sprayCanTime; 
    boolean straightLineTime; 
    boolean filledTime; 
    boolean emptyTime; 
    boolean freeTime; 
    boolean dragOvalTime; 
    boolean dragRectTime; 


    public void init() 
    { 
     this.setSize(1350,651); 

     c1 = new Canvas(); 
     add(c1); 
     c1.setSize(900,450); 


     c1.addMouseListener(this); 
     c1.addMouseMotionListener(this); 

     red = new Button("Red"); 
     add(red); 
     red.addActionListener(this); 
     red.setBackground(Color.red); 
     red.addMouseListener(this); 

     blue = new Button("Blue"); 
     add(blue); 
     blue.addActionListener(this); 
     blue.setBackground(Color.blue); 
     blue.addMouseListener(this); 

     green = new Button("Green"); 
     add(green); 
     green.addActionListener(this); 
     green.setBackground(Color.green); 
     green.addMouseListener(this); 


     oval = new Button("Oval"); 
     add(oval); 
     oval.addActionListener(this); 

     rect = new Button("Square"); 
     add(rect); 
     rect.addActionListener(this); 

     sprayCan = new Button("Spray Can"); 
     add(sprayCan); 
     sprayCan.addActionListener(this); 

     filled = new Button("Filled"); 
     add(filled); 
     filled.addActionListener(this); 

     empty = new Button("Empty"); 
     add(empty); 
     empty.addActionListener(this); 

     free = new Button("Free"); 
     add(free); 
     free.addActionListener(this); 

     straightLine = new Button("Straight Line"); 
     add(straightLine); 
     straightLine.addActionListener(this); 

     dragOval = new Button("Drag Oval"); 
     add(dragOval); 
     dragOval.addActionListener(this); 

     dragRect = new Button("Drag Square"); 
     add(dragRect); 
     dragRect.addActionListener(this); 

     t1 = new TextField("10"); 
     add(t1); 
     t1.addActionListener(this); 

     currentColor = new TextField("Black"); 
     add(currentColor); 
     currentColor.addActionListener(this); 

     currentMode = new TextField("Free"); 
     add(currentMode); 
     currentMode.addActionListener(this); 

     currentShape = new TextField("Oval"); 
     add(currentShape); 
     currentShape.addActionListener(this); 

     reset = new Button("Reset"); 
     add(reset); 
     reset.addActionListener(this); 

     c2 = new Canvas(); 
     add(c2); 
     c2.addMouseListener(this); 
     c2.setSize(900,200); 




     ovalTime = false; 
     rectTime = false; 
     sprayCanTime = false; 
     straightLineTime = false; 
     filledTime = false; 
     emptyTime = true; 
     freeTime = true; 
     dragOvalTime = false; 
     dragRectTime = false; 

     myG = c1.getGraphics(); 
     myG2 = c2.getGraphics(); 

     currentShape.setText(""); 
     drawRainbow(); 


    } 

    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource()==red) 
     { 
      myG.setColor(Color.red); 
     } 
     if(e.getSource()==blue) 
     { 
      myG.setColor(Color.blue); 
     } 
     if(e.getSource()==green) 
     { 
      myG.setColor(Color.green); 
     } 
     if(e.getSource()==oval) 
     { 
      ovalTime = true; 
      rectTime = false; 

      freeTime = false; 
      straightLineTime = false; 
      emptyTime = true; 

      currentMode.setText("Empty"); 
      currentShape.setText("Oval"); 
     } 
     if(e.getSource()==rect) 
     { 
      ovalTime = false; 
      rectTime = true; 
      currentShape.setText("Square"); 
     } 
     if(e.getSource()==sprayCan) 
     { 
      sprayCanTime = true; 
      straightLineTime = false; 
      filledTime = false; 
      emptyTime = false; 
      freeTime = false; 
      dragOvalTime = false; 
      dragRectTime = false; 
      currentMode.setText("Spray"); 
      currentShape.setText(""); 
     } 
     if(e.getSource()==straightLine) 
     { 
      straightLineTime = true; 
      sprayCanTime = false;   
      filledTime = false; 
      emptyTime = false; 
      freeTime = false; 
      dragOvalTime = false; 
      dragRectTime = false; 
      currentMode.setText("Line"); 
     } 
     if(e.getSource()==filled) 
     { 
      filledTime = true; 
      sprayCanTime = false; 
      straightLineTime = false; 
      emptyTime = false; 
      freeTime = false; 
      dragOvalTime = false; 
      dragRectTime = false; 
      currentMode.setText("Filled"); 
     } 
     if(e.getSource()==empty) 
     { 
      emptyTime = true; 
      sprayCanTime = false; 
      straightLineTime = false; 
      filledTime = false; 
      freeTime = false; 
      dragOvalTime = false; 
      dragRectTime = false; 
      currentMode.setText("Empty"); 
     } 
     if(e.getSource()==free) 
     { 
      freeTime = true; 
      sprayCanTime = false; 
      straightLineTime = false; 
      filledTime = false; 
      emptyTime = false; 
      dragOvalTime = false; 
      dragRectTime = false; 
      currentMode.setText("Free"); 

     } 
     if(e.getSource()==dragOval) 
     { 
      dragOvalTime = true; 
      freeTime = false; 
      sprayCanTime = false; 
      straightLineTime = false; 
      filledTime = false; 
      emptyTime = false; 
      dragRectTime = false; 
      currentMode.setText("Drag Oval"); 
     } 
     if(e.getSource()==dragRect) 
     { 
      dragRectTime = true; 
      dragOvalTime = false; 
      freeTime = false; 
      sprayCanTime = false; 
      straightLineTime = false; 
      filledTime = false; 
      emptyTime = false; 
      currentMode.setText("Drag Square"); 
     } 
     if(e.getSource()==t1) 
     { 
      text = t1.getText(); 
      penSize = Integer.parseInt(text); 
     } 
     if(e.getSource()==reset) 
     { 

      myG.setColor(Color.white); 
      myG.fillRect(0, 0, 900, 450); 
      myG.setColor(Color.black); 

      ovalTime = true; 
      rectTime = false; 
      sprayCanTime = false; 
      straightLineTime = false; 
      filledTime = false; 
      emptyTime = true; 
      freeTime = true; 
      dragOvalTime = false; 
      dragRectTime = false; 

      t1.setText("10"); 
      penSize = 10; 

      currentColor.setText("Black"); 
      currentMode.setText("Free"); 
      currentShape.setText(""); 

     } 


    } 


    public void paint(Graphics g) 
    { 
     c1.setLocation(0,0); 
     g.drawRect(0, 0, 901, 450); 
     free.setLocation(1050, 250); 
     reset.setLocation(1275, 250); 
     straightLine.setLocation(950,250); 
     t1.setLocation(1100, 50); 
     t1.setSize(40,20); 
     g.drawString("Press Enter After Changing Size", 1050, 30); 
     g.drawRect(1049, 18, 177, 20); 
     currentColor.setLocation(990, 90); 
     currentMode.setLocation(1200, 90); 
     currentMode.setSize(65,20); 
     currentShape.setLocation(1095, 90); 
     dragOval.setLocation(1100,250); 
     dragRect.setLocation(1175,250); 

     c2.setLocation(0,495); 
     drawRainbow(); 

    } 

    public void drawRainbow() 
    { 
     counter = 0; 
     x11=1; 
     x22=0; 
     y11=1; 
     y22=200; 
     num1 = (float).01; 
     num2 = (float)1.0; 
     num3 = (float)1.0; 

     while(counter < 1349) 
     { 
      myG2.setColor(Color.getHSBColor(num1, num2, num3)); 
      myG2.drawLine(x11,y11,x22,y22); 
      num1=(float)(num1+.001); 
      x11++; 
      x22++; 
      counter++; 
     } 
    } 

    public void mouseClicked(MouseEvent e) 
    { 


    } 



    public void mouseEntered(MouseEvent e) 
    { 

    } 



    public void mouseExited(MouseEvent e) 
    { 

    } 



    public void mousePressed(MouseEvent e) 
    { 
     if(e.getSource()==red) 
     { 
      red.setBackground(Color.yellow); 
     } 

     if(e.getSource()==blue) 
     { 
      blue.setBackground(Color.magenta); 
     } 
     if(e.getSource()==green) 
     { 
      green.setBackground(Color.pink); 
     }  
     if(straightLineTime) 
     { 
      xForStraightLine = e.getX(); 
      yForStraightLine = e.getY(); 
     } 
     if(ovalTime) 
     { 
      if(!freeTime) 
      { 
       if(emptyTime) 
       { 
        myG.drawOval(e.getX(),e.getY(),penSize,penSize); 
       } 
       if(filledTime) 
       { 
        myG.fillOval(e.getX(),e.getY(),penSize,penSize); 
       } 
      } 
     } 
     if(rectTime) 
     { 
      if(!freeTime) 
      { 
       if(emptyTime) 
       { 
        myG.drawRect(e.getX(),e.getY(),penSize,penSize); 
       } 
       if(filledTime) 
       { 
        myG.fillRect(e.getX(),e.getY(),penSize,penSize); 
       } 
      } 
     } 
     if(freeTime) 
     { 
      x = e.getX(); 
      y = e.getY(); 
      ovalTime = false; 
      rectTime = false; 
      currentShape.setText(""); 
     } 
     if(dragOvalTime || dragRectTime) 
     { 
      x1=e.getX(); 
      y1=e.getY(); 
     } 

     if(e.getSource()==c2) 
     { 
      hueNum = e.getX(); 
      hueNum = hueNum+70; 
      hueNum = (float)(hueNum*.001); 
      myG.setColor(Color.getHSBColor(hueNum,num2,num3)); 
     } 

    } 



    public void mouseReleased(MouseEvent e) 
    { 
     if(e.getSource()==red) 
     { 
      red.setBackground(Color.red); 
      currentColor.setText("Red"); 
     } 

     if(e.getSource()==blue) 
     { 
      blue.setBackground(Color.blue); 
      currentColor.setText("Blue"); 
     } 
     if(e.getSource()==green) 
     { 
      green.setBackground(Color.green); 
      currentColor.setText("Green"); 
     } 
     if(straightLineTime) 
     { 
      myG.drawLine(xForStraightLine, yForStraightLine, e.getX(), e.getY()); 
     } 
     if(dragOvalTime) 
     { 
      x2=e.getX(); 
      y2=e.getY(); 
      xSize = Math.abs(x2-x1); 
      ySize = Math.abs(y2-y1); 

      if(x2>x1 && y2>y1) //if top left to bottom right 
      { 
       myG.drawOval(x1,y1,xSize,ySize); 
      } 
      if(x2<x1 && y2<y1) //if bottom right to top left 
      { 
       myG.drawOval(x2,y2,xSize,ySize); 
      } 
      if(x2<x1 && y2>y1) //if top right to bottom left 
      { 
       myG.drawOval(x2,y1,xSize,ySize); 
      } 
      if(x2>x1 && y2<y1) //if bottom left to top right 
      { 
       myG.drawOval(x1, y2, xSize, ySize); 
      } 

      /* There are 4 possible cases, originally it draws on the initial press point. 
      * Judge each of the cases be seeing which coordinates are larger than others, to know whether to start on draw or end point. 
      */ 
     } 
     if(dragRectTime) 
     { 
      x2=e.getX(); 
      y2=e.getY(); 
      xSize = Math.abs(x2-x1); 
      ySize = Math.abs(y2-y1); 

      if(x2>x1 && y2>y1) //if top left to bottom right 
      { 
       myG.drawRect(x1,y1,xSize,ySize); 
      } 
      if(x2<x1 && y2<y1) //if bottom right to top left 
      { 
       myG.drawRect(x2,y2,xSize,ySize); 
      } 
      if(x2<x1 && y2>y1) //if top right to bottom left 
      { 
       myG.drawRect(x2,y1,xSize,ySize); 
      } 
      if(x2>x1 && y2<y1) //if bottom left to top right 
      { 
       myG.drawRect(x1, y2, xSize, ySize); 
      } 

      /* There are 4 possible cases, originally it draws on the initial press point. 
      * Judge each of the cases be seeing which coordinates are larger than others, to know whether to start on draw or end point. 
      */ 
     } 
    } 



    public void mouseDragged(MouseEvent arg0) 
    { 
     if(ovalTime) 
     { 
      if(emptyTime) 
      { 
       myG.drawOval(arg0.getX(),arg0.getY(),penSize,penSize); 
      } 
      if(filledTime) 
      { 
       myG.fillOval(arg0.getX(),arg0.getY(),penSize,penSize); 
      } 
     } 
     if(rectTime) 
     { 
      if(emptyTime) 
      { 
       myG.drawRect(arg0.getX(),arg0.getY(),penSize,penSize); 
      } 
      if(filledTime) 
      { 
       myG.fillRect(arg0.getX(),arg0.getY(),penSize,penSize); 
      } 
     } 
     if(sprayCanTime) 
     { 
      myG.drawOval(arg0.getX(),arg0.getY(),penSize,penSize); 
      myG.fillOval(arg0.getX(),arg0.getY(),penSize,penSize); 
     } 

     if(freeTime) 
     { 
      myG.drawLine(arg0.getX(), arg0.getY(), x, y); 
      x = arg0.getX(); 
      y = arg0.getY(); 
     } 

    } 



    public void mouseMoved(MouseEvent arg0) 
    { 


    } 



} 
+0

1)*「這是一些冗長的代碼,但可以瀏覽可惡的布爾重置。」*或者我可以忽略此問題並繼續下一步,即人員修改代碼的位置。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 2)如果老師指定了AWT,他們不適合教學。如果沒有,請使用Swing。 3)在這個階段,代碼框而不是小程序。 – 2013-03-05 23:47:12

回答

0

有趣的節目。嘗試這個。

變化:

c2 = new Canvas(); 

要:

c2 = new Canvas() { 
    public void paint(final Graphics g) { 
     drawRainbow(g); 
    } 
}; 

使用通過圖形對象在drawRainbow(圖形G)G和消除對drawRainbow()任何電話爲:

public void drawRainbow(final Graphics g) { 
... 
    g.setColor(Color.getHSBColor(num1, num2, num3)); 
    g.drawLine(x11, y11, x22, y22); 
... 

編輯:在你接受的答案中留意關於圖形的建議。

+0

非常感謝,我扔了它,它的工作。我將不得不對圖形和最終工作做更多的研究,但非常感謝你的幫助。我很感激。 – Chris 2013-03-06 01:27:17

0

從不保持任何Graphics上下文的任何引用您沒有創建自己

這...

myG = c1.getGraphics(); 
myG2 = c2.getGraphics(); 

並不怎麼風俗畫進行。除了getGraphics可以返回null這些事實,這些只是最後一次繪製週期的「快照」。更重要的是,這些方法返回的引用在下一個繪製週期中將是相同的參考用途。

我不確定你爲什麼在Swing上使用AWT,但那是你的選擇。

創建一個自定義版本的Canvas並覆蓋它的paint方法。使用此方法執行所有繪畫,使用過去的Graphics上下文。不要忘了先致電super.paint

Painting in AWT and Swing讀更多細節

+0

老實說,我不擅長編程,因爲我在高中班,所以我使用AWT,因爲我從來沒有教過Swing。我不太確定我將如何製作自己的Canvas定製版本,但我猜Google會幫助我。另外,我不知道super.paint的意思是說實話。儘管如此,感謝你爲我編寫了這個可怕的代碼塊。 – Chris 2013-03-05 23:47:38

+0

'公共類MyCanvas擴展帆布{...}' - 基本的OO。從[this]開始(http://docs.oracle.com/javase/tutorial/getStarted/index.html)和[this](http://docs.oracle.com/javase/tutorial/java/index.html )然後去[this](http://docs.oracle.com/javase/tutorial/uiswing/) – MadProgrammer 2013-03-05 23:49:06