到目前为止,我正在尝试创建一个可以绘制的java程序(比如绘画),我有笔大小和不同的颜色。唯一的问题是矩形(它们就在那里,所以你可以看到在哪里点击)每当你画画时闪烁。这是我的代码:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
@SuppressWarnings({ "serial" })
public class DrawGame extends Applet implements MouseListener, MouseMotionListener, ActionListener {
int x, y, width, height, colorx, colory, pen;
boolean isRed = false, isYellow = false, isBlue = false, isGreen = false, isGrey = false, isBlack = false;
String color = "Black";
Image backbuffer;
Graphics background;
Button clear;
CheckboxGroup radioGroup;
Checkbox ps4, ps8, ps16, ps32, ps64;
public void init() {
clear = new Button("Clear");
radioGroup = new CheckboxGroup();
ps4 = new Checkbox("4", radioGroup, false);
ps8 = new Checkbox("8", radioGroup, true);
ps16 = new Checkbox("16", radioGroup, false);
ps32 = new Checkbox("32", radioGroup, false);
ps64 = new Checkbox("64", radioGroup, false);
setLayout(new FlowLayout());
setSize(900, 700);
width = getSize().width;
height = getSize().height;
add(clear);
add(ps4);
add(ps8);
add(ps16);
add(ps32);
add(ps64);
setBackground(Color.WHITE);
backbuffer = createImage(width, height);
background = backbuffer.getGraphics();
addMouseListener(this);
addMouseMotionListener(this);
}
public void mousePressed(MouseEvent v) {
if(x < 80) { //so you can't draw where the squares are
colorx = v.getX();
colory = v.getY();
}
if(colorx >= 25 && colory <= 50 && colory >= 25 && colorx <= 75) {
isRed = true;
isBlue = false;
isBlack = false;
isGreen = false;
isGrey = false;
isGreen = false;
isYellow = false;
color = "Red";
}
else if(colorx >= 25 && colory <= 100 && colory >= 75 && colorx <= 75) {
isBlack = true;
isRed = false;
isBlue = false;
isGreen = false;
isGrey = false;
isGreen = false;
isYellow = false;
color = "Black";
}
else if(colorx >= 25 && colory <= 150 && colory >= 125 && colorx <= 75) {
isBlue = true;
isBlack = false;
isRed = false;
isGreen = false;
isGrey = false;
isGreen = false;
isYellow = false;
color = "Blue";
}
else if(colorx >= 25 && colory <= 200 && colory >= 175 && colorx <= 75) {
isGreen = true;
isBlack = false;
isRed = false;
isBlue = false;
isGrey = false;
isYellow = false;
color = "Green";
}
else if(colorx >= 25 && colory <= 250 && colory >= 225 && colorx <= 75) {
isYellow = true;
isBlack = false;
isRed = false;
isBlue = false;
isGrey = false;
isGreen = false;
color = "Yellow";
}
else if(colorx >= 25 && colory <= 300 && colory >= 275 && colorx <= 75) {
isGrey = true;
isBlack = false;
isRed = false;
isBlue = false;
isYellow = false;
isGreen = false;
color = "Yellow";
}
else{
isBlack = false;
isBlue = false;
isRed = false;
isGreen = false;
isGrey = false;
isGreen = false;
isYellow = false;
color = "Black";
}
}
public void mouseDragged(MouseEvent v) {
x = v.getX();
y = v.getY();
if(ps4.getState()) pen = 4;
if(ps8.getState()) pen = 8;
if(ps16.getState()) pen = 16;
if(ps32.getState()) pen = 32;
if(ps64.getState()) pen = 64;
if(isRed == true) {
background.setColor(Color.red);
}
else if(isBlue == true) {
background.setColor(Color.blue);
}
else if(isGreen == true) {
background.setColor(Color.GREEN);
}
else if(isYellow == true) {
background.setColor(Color.YELLOW);
}
else if(isGrey == true) {
background.setColor(Color.GRAY);
}
else{
background.setColor(Color.black);
}
if(x > 80) {
background.fillOval(x-(pen/2), y-(pen/2), pen, pen);
}
repaint();
v.consume();
}
public void mouseMoved(MouseEvent v) {
x = v.getX();
y = v.getY();
showStatus("Mouse co-ordinates: " + x + ", " + y + " Current selected color: " + color);
}
public void mouseClicked(MouseEvent v) {
v.getX();
v.getY();
if(x > 80) {
background.fillOval(x-(pen/2), y-(pen/2), pen, pen);
}
repaint();
v.consume();
}
public void mouseEntered(MouseEvent v) {}
public void mouseExited(MouseEvent v) {}
public void mouseReleased(MouseEvent v) {}
public void update (Graphics g) {
g.drawImage(backbuffer, 0, 0, this);
repaint();
}
public void squares(Graphics g) {
g.setColor(Color.RED);
g.fillRect(25, 25, 50, 25);
g.setColor(Color.black);
g.fillRect(25, 75, 50, 25);
g.setColor(Color.BLUE);
g.fillRect(25, 125, 50, 25);
g.setColor(Color.GREEN);
g.fillRect(25, 175, 50, 25);
g.setColor(Color.YELLOW);
g.fillRect(25, 225, 50, 25);
g.setColor(Color.GRAY);
g.fillRect(25, 275, 50, 25);
repaint();
}
public void paint (Graphics draw) {
update(draw);
draw.drawImage(backbuffer, 0, 0, this);
}
public void actionPerformed(ActionEvent e) {}
}
答案 0 :(得分:0)
首先,您似乎以不恰当的方式调用方法,产生重复循环:
paint() invokes update()
update() invokes repaint()
repaint() invokes paint()
...
然后,尊重双缓冲,我希望这个链接对你有用: Double Buffering with awt
此致