Reputation: 11
I am creating an applet that allows the user to move a box on a grid while using the arrow keys. I have made it so the box moves but I have a question. I want to shade any grid box already visited black. So if if my box's coordinates were (20,20,20,20)
it would leave a box where it was, eventually the whole screen would be black. I have tried removing the repaint();
but then the program doesn't work. Is there a way to tell java not to repaint a specific frame? Any help is much appreciated!
package robot;
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.JApplet;
import java.awt.event.KeyListener;
public class Robot extends JApplet implements KeyListener {
private Rectangle rect;
public void init(){
rect = new Rectangle (0, 0, 20,20);
setFocusable(true);
requestFocusInWindow();
addKeyListener(this);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0,0,500,500);
{
this.setSize(360, 360);
}
g.setColor(Color.black);
g.drawLine(0,0,360,0);
g.drawLine(0,20,360,20);
g.drawLine(0,40,360,40);
g.drawLine(0,60,360,60);
g.drawLine(0,80,360,80);
g.drawLine(0,100,360,100);
g.drawLine(0,120,360,120);
g.drawLine(0,140,360,140);
g.drawLine(0,160,360,160);
g.drawLine(0,180,360,180);
g.drawLine(0,200,360,200);
g.drawLine(0,220,360,220);
g.drawLine(0,240,360,240);
g.drawLine(0,260,360,260);
g.drawLine(0,280,360,280);
g.drawLine(0,300,360,300);
g.drawLine(0,320,360,320);
g.drawLine(0,340,360,340);
g.drawLine(0,360,360,360);
//verticle lines
g.drawLine(0,0,0,360);
g.drawLine(20,0,20,360);
g.drawLine(40,0,40,360);
g.drawLine(60,0,60,360);
g.drawLine(80,0,80,360);
g.drawLine(100,0,100,360);
g.drawLine(120,0,120,360);
g.drawLine(140,0,140,360);
g.drawLine(160,0,160,360);
g.drawLine(180,0,180,360);
g.drawLine(200,0,200,360);
g.drawLine(220,0,220,360);
g.drawLine(240,0,240,360);
g.drawLine(260,0,260,360);
g.drawLine(280,0,280,360);
g.drawLine(300,0,300,360);
g.drawLine(320,0,320,360);
g.drawLine(340,0,340,360);
g.drawLine(360,0,360,360);
Graphics2D g1 = (Graphics2D) g;
g1.setColor(Color.black);
g1.fill(rect);
}
@Override
public void keyPressed(KeyEvent e)
{ switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
rect.x -= 20;
break;
case KeyEvent.VK_RIGHT:
rect.x += 20;
break;
case KeyEvent.VK_UP:
rect.y -= 20;
break;
case KeyEvent.VK_DOWN:
rect.y += 20;
break;
}
repaint();
}
public void pain(Graphics g){
g.fillRect(rect.x, rect.y, 20, 20);
}
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Upvotes: 0
Views: 52
Reputation: 11
You are working with a low level graphics. Therefore, you would have to design an algorithm that covers this functionality. If your Canvas is not that big a good solution would be creating black squares as you move the box. Keep in mind that when your are doing graphics at this level of you are dealing almost with pixel by pixel logic.
Upvotes: 0
Reputation: 31648
No, Java must be able to repaint when it needs to. repaint is not only called when you update the screen. If a user moves the frame around or minimizes it or puts another window up in front etc. All these may require a repaint.
This means you have to have a way for your program to keep track of where the boxes have been so you can redraw the previous lines.
However, that might be slow. An alternative but more complicated option is to draw your boxes onto a BufferedImage and then just display your BufferedImage in paint()
. Any updates to your box positions can overwrite just the pixels required in your BufferedImage
Upvotes: 3