如何修复此绘图程序,以便在选择另一个像素时,从一条线绘制的最后一个像素不会变为不同的颜色?
Posted
技术标签:
【中文标题】如何修复此绘图程序,以便在选择另一个像素时,从一条线绘制的最后一个像素不会变为不同的颜色?【英文标题】:How can I fix this paint program so that the last pixel drawn from a line does not change to a different color when another is selected? 【发布时间】:2016-09-23 15:42:27 【问题描述】: 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 。这将是我发布后尝试和玩的东西。任何帮助将不胜感激!
【问题讨论】:
Java Plugin support deprecated 和 Moving to a Plugin-Free Web 创建一个自定义类,它可能从Rectangle
扩展而来,它还带有应该绘制的颜色
【参考方案1】:
您需要设置一些标志来确定颜色发生了变化,而不是用新颜色重绘矩形。有很多不同的方法可以实现这一点,这里有一个:
如果颜色发生变化,请将newX
和newY
设置为某个无效值:
@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);
【讨论】:
以上是关于如何修复此绘图程序,以便在选择另一个像素时,从一条线绘制的最后一个像素不会变为不同的颜色?的主要内容,如果未能解决你的问题,请参考以下文章
修复 laravel 5 会话在刷新或进入另一个页面后过期?