2016-05-25 7 views
0
import java.awt.*; 
    import java.applet.*; 


    public class lab13 extends Applet 
    { 

    Rectangle pencil; 
    Rectangle red; 
    Rectangle blue; 
    Rectangle green; 
    Rectangle purple; 
    Rectangle drawArea; 


    Image virtualMem; 
    Graphics gBuffer; 

    int newX; 
    int newY; 


    int appletWidth; 
    int appletHeight; 

    int drawnPoint; 
    int choice; 

    public void init() 
    { 
     appletWidth = getWidth(); 
     appletHeight = getHeight(); 
     virtualMem = createImage(appletWidth,appletHeight); 
     gBuffer = virtualMem.getGraphics(); 
     gBuffer.setColor(Color.WHITE); 
     gBuffer.fillRect(0,0,appletWidth,appletHeight); 

     pencil = new Rectangle(10,10,50,50); 
     red = new Rectangle(10,70,50,50); 
     blue = new Rectangle(10,130,50,50); 
     green = new Rectangle(10,190,50,50); 
     purple = new Rectangle(10,250,50,50); 

     drawArea = new Rectangle(85,10,290,380); 

     gBuffer.drawRect(85,10,290,380); 

     gBuffer.setColor(Color.BLACK); 
     gBuffer.fillRect(10,10,50,50); 

     gBuffer.setColor(Color.RED); 
     gBuffer.fillRect(10,70,50,50); 

     gBuffer.setColor(Color.BLUE); 
     gBuffer.fillRect(10,130,50,50); 

     gBuffer.setColor(Color.GREEN); 
     gBuffer.fillRect(10,190,50,50); 

     gBuffer.setColor(new Color(218,112,214)); 
     gBuffer.fillRect(10,250,50,50); 

     choice = 0; 
    } 

    public void paint(Graphics g) 
    { 

     gBuffer.setColor(Color.WHITE);   //This is a quick fix for a colored pixel in the top left hand corner 
     gBuffer.fillRect(0,0,3,3); 


     switch(choice) 
     { 
      case 1: 
       gBuffer.setColor(Color.BLACK); 
       break; 
      case 2: 
       gBuffer.setColor(Color.red); 
       break; 
      case 3: 
       gBuffer.setColor(Color.BLUE); 
       break; 
      case 4: 
       gBuffer.setColor(Color.GREEN); 
       break; 
      case 5: 
       gBuffer.setColor(new Color(218,112,214)); 
       break; 
     } 

      gBuffer.fillRect(newX,newY,3,3); 
      g.drawImage(virtualMem,0,0,this); 

    } 





    public boolean mouseDown(Event e, int x, int y) 
    { 
     if (pencil.inside(x,y)) 
      choice = 1; 
     else if (red.inside(x,y)) 
      choice = 2; 
     else if (blue.inside(x,y)) 
      choice = 3; 
     else if (green.inside(x,y)) 
      choice = 4; 
     else if (purple.inside(x,y)) 
      choice = 5; 
     repaint(); 
     return true; 

    } 

    public boolean mouseDrag(Event e, int x, int y) 
    { 
     if(drawArea.inside(x,y)) 
     { 
      newX = x; 
      newY = y; 
      repaint(); 
     } 
     return true; 

    } 




    public void update(Graphics g) 
    { 
     paint(g); 
    } 



} 

これは私たちが学校で研究室で行っているペイントプログラムです。私が抱えている問題は、線を引いて別の色を選択すると、前の行の最後の部分が、一様な色を保つのではなく、選択された色に変わるということです。私は本当にどこで問題を探すべきか分からない。私は変えようとしました。私はそれが私のペイント方法の終わりにdrawImageかもしれないと思う。それは、私がこれを投稿した後、私が試してやるものです。どんな助けでも大歓迎です!このペイントプログラムを修正して、線から描画された最後のピクセルが別の色が選択されたときに別の色に変わらないようにするにはどうすればよいですか?

+0

[Javaプラグインのサポートは終了](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/)および[プラグインフリーWebへの移行]( https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free) – MadProgrammer

+0

顧客を作成するこれはおそらく 'Rectangle'から拡張されていて、描画されるべき色を持っています – MadProgrammer

答えて

0

色が変更されたと判断し、新しい色で矩形を再描画しないようにするには、フラグを設定する必要があります。あなたはこれを実現するさまざまな方法がありますが、ここでは1です:

色の変化は、いくつかの無効な値にnewXnewYを設定した場合:

@Override 
public boolean mouseDown(final Event e, final int x, final int y) { 
    int choiceNew = 0; 

    if (pencil.inside(x, y)) { 
     choiceNew = 1; 
    } else if (red.inside(x, y)) { 
     choiceNew = 2; 
    } else if (blue.inside(x, y)) { 
     choiceNew = 3; 
    } else if (green.inside(x, y)) { 
     choiceNew = 4; 
    } else if (purple.inside(x, y)) { 
     choiceNew = 5; 
    } 

    if ((choiceNew > 0) && (choice != choiceNew)) { 
     choice = choiceNew; 
     newX = -1; 
     newY = -1; 
    } else { 
     newX = x; 
     newY = y; 
    } 

    repaint(); 
    return true; 
} 

ペイント時次に、あなたがそのフラグをチェック:

@Override 
public void paint(final Graphics g) { 
    gBuffer.setColor(Color.WHITE); //This is a quick fix for a colored pixel in the top left hand corner 
    gBuffer.fillRect(0, 0, 3, 3); 

    switch (choice) { 
     case 1: 
      gBuffer.setColor(Color.BLACK); 
      break; 
     case 2: 
      gBuffer.setColor(Color.red); 
      break; 
     case 3: 
      gBuffer.setColor(Color.BLUE); 
      break; 
     case 4: 
      gBuffer.setColor(Color.GREEN); 
      break; 
     case 5: 
      gBuffer.setColor(new Color(218, 112, 214)); 
      break; 
    } 

    if ((newX != -1) && (newY != -1)) { 
     gBuffer.fillRect(newX, newY, 3, 3); 
    } 

    g.drawImage(virtualMem, 0, 0, this); 
} 
関連する問題