gpt4 book ai didi

Java绘画程序: How to leave a trail behind a shape

转载 作者:行者123 更新时间:2023-11-30 08:13:55 25 4
gpt4 key购买 nike

我正在尝试创建一个绘画程序,它允许您移动形状并将该形状绘制到JFrame上。然而,当我作画时,它不会留下痕迹。我认为它不会留下痕迹,因为我重写了绘制方法,并且当我想绘制新对象时,它会清除以前绘制的对象。但是,当我删除覆盖此代码并运行程序时,形状会留下痕迹,但框架顶部的按钮会变得一团糟。

MovingBlockFrame.java:

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);
}

}

MovingBlockCanvas.java:

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);

}
}
}

问题出在绘制方法中,当我重写该方法时,我不会留下形状的痕迹。另外,如果您有任何编码技巧,我很想听听。预先感谢您。

最佳答案

这是我学到的解决此错误的方法。我只需创建一个新的缓冲图像,然后绘制到该图像,然后使用 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();


}


}

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

关于Java绘画程序: How to leave a trail behind a shape,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29932822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com