Java绘画程序:如何在形状后留下痕迹

时间:2015-04-29 01:27:58

标签: java swing java-2d

您好,感谢您阅读此问题。我是java程序的初学者,我试图用我现在已有的知识创建一个程序。我试图创建的程序是一个绘图程序,它基本上只允许您移动一个形状,并将形状绘制到框架上。我遇到的一个问题是,当我画它时不会留下痕迹。我认为它没有留下痕迹,因为我重写了paint方法,当我想要绘制一个新的时,它会清除我以前绘制的对象。但是当我删除覆盖它的代码并运行程序时,形状会留下一条痕迹,但框架顶部的按钮会搞得一团糟。

这是我的代码:

package MovingBlock;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MovingBlockFrame extends JFrame implements KeyListener, ActionListener, ChangeListener{


   FlowLayout flo = new FlowLayout(FlowLayout.LEADING, 10, 10);
   JPanel buttonPanel = new JPanel();
   JButton circle = new JButton("Circle");
   JButton rectangle = new JButton("Rectangle");
   JButton color = new JButton("Color");
   JSlider speed = new JSlider(0, 100, 10);

   Graphics2D test3;
   int desiredWidth = 780;
   int desiredHeight = 800;
   MovingBlockCanvas background = new MovingBlockCanvas();
   int paintSpeed = 10;
   boolean diagonalKeysDown;
    boolean diagonalKeysUp;

    public MovingBlockFrame(){



    setTitle("Painter");
    buttonPanel.setBorder(BorderFactory.createLineBorder(Color.black));
    setSize(desiredWidth, desiredHeight);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    KeyListener();
    buttonPanel.setLayout(flo);
    buttonPanel.add(circle);
    buttonPanel.add(rectangle);
    buttonPanel.add(color);
    buttonPanel.add(speed);



    background.desiredShape = "Circle";
    background.addKeyListener(this);

    add(background);
    add(buttonPanel, BorderLayout.NORTH);

    setVisible(true);



}

public static void main(String[] args){
    MovingBlockFrame frame = new MovingBlockFrame();

    frame.addKeyListener(frame);
}

public void keyPressed(KeyEvent input) {

    int pressed = input.getKeyCode();
    if(pressed == 83){
        diagonalKeysDown = true;
        background.xValue+= paintSpeed;
        background.repaint();
    }else if(pressed == 68 && !diagonalKeysDown){
        background.yValue+=paintSpeed;
        background.repaint();
    }else if(pressed == 87 ){
        diagonalKeysUp = true;
        background.xValue-=paintSpeed;
        background.repaint();
    }else if(pressed == 65 ){
        background.yValue-=paintSpeed;
        background.repaint();
    } if(diagonalKeysDown && pressed == 68){
        background.yValue+=paintSpeed;
        background.xValue+= paintSpeed;
        background.repaint();
    } if(diagonalKeysUp  && pressed == 68){
        background.yValue+=paintSpeed;
        background.xValue-= paintSpeed;
        background.repaint();
    } if(diagonalKeysDown && pressed == 65){
        background.yValue-=paintSpeed;
        background.xValue+= paintSpeed;
        background.repaint();
    } if(diagonalKeysUp && pressed == 65){
        background.yValue-=paintSpeed;
        background.xValue-= paintSpeed;
        background.repaint();
    }
}


public void keyReleased(KeyEvent input) {
    int released = input.getKeyCode();
    if(released == 83){
        diagonalKeysDown = false;
    } if(released == 87){
        diagonalKeysUp = false;
    }
}

public void keyTyped(KeyEvent input) {

}

public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
        if(command.equals("Circle")){
            background.desiredShape = "Circle";
            background.repaint();
        }else if (command.equals("Rectangle")){
            background.desiredShape = "Rectangle";
            background.repaint();
        }else if(command.equals("Color")){
            //ColorSliders test = new ColorSliders();
        }

}


public void stateChanged(ChangeEvent event) {
    JSlider source = (JSlider) event.getSource();
    if(source.getValueIsAdjusting() != true){
        paintSpeed = source.getValue();
    }

}

public void KeyListener(){
    buttonPanel.addKeyListener(this);
    circle.addKeyListener(this);
    rectangle.addKeyListener(this);
    circle.addActionListener(this);
    rectangle.addActionListener(this);
    color.addKeyListener(this);
    color.addActionListener(this);
    speed.addKeyListener(this);
    speed.addChangeListener(this);
}

}

然后有这个班:

package MovingBlock;

import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JPanel;

public class MovingBlockCanvas extends JPanel {

int xValue = 50;
int yValue = 50;
String desiredShape;


public MovingBlockCanvas(){

}


public void paint(Graphics render){

    //super.paint(render);

    render.setColor(ColorPanel.getColor2());

    if(desiredShape == "Rectangle"){
        render.fillRect(yValue, xValue, 50, 50);
    }
    if(desiredShape == "Circle"){
        render.fillOval(yValue, xValue, 50, 50);

    }   
}
}

这是我到目前为止的代码。所以问题出在paint方法中,当我覆盖方法时,我不会为形状留下痕迹。如果你有任何编码技巧,我很乐意听到它们。提前谢谢。

1 个答案:

答案 0 :(得分:1)

这是我学会修复此错误的原因。我只需创建一个新的缓冲图像,然后绘制到该图像,然后使用paintComponent方法重绘缓冲的图像。这是我使用更新的代码创建的类,使程序绘制并在形状后面留下痕迹,同时仍然覆盖paintComponenet方法。

public class MovingBlockCanvas extends JPanel {

int xValue = 50;
int yValue = 50;
int width = 10;
int height = 10;
String desiredShape;
BufferedImage drawing;

public MovingBlockCanvas(){

}

public void drawShapes(){
    if(drawing == null){
        createBufferedImage();
    }
    if(desiredShape.equals("Rectangle")){
        Graphics2D g = drawing.createGraphics();
        g.setColor(ColorPanel.getColor2());
        g.fillRect(yValue, xValue, width, height);
        g.dispose();
    }
    if(desiredShape.equals("Circle")){
        Graphics2D g = drawing.createGraphics();
        g.setColor(ColorPanel.getColor2());
        g.fillOval(yValue, xValue, width, height);
        g.dispose();
    }
    repaint();
}

public void createBufferedImage() {


        BufferedImage buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = buffer.createGraphics();
        g2d.setColor(Color.white);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.dispose();
        drawing = buffer;

}


@Override
public void paintComponent(Graphics render){

    super.paintComponent(render);
    Graphics2D g = (Graphics2D) render.create();
    if(drawing == null){
        createBufferedImage();
    }
    g.drawImage(drawing, 0, 0, this);
    g.dispose();


}


}

感谢所有帮助我学习这一切的人!