[英]Java repaint() makes second canvas drawing not appear
我一直在做一個繪畫程序,我在光譜中的每種顏色上都插入了一個色條,以便用戶可以單擊任意位置,它將(大致)為他們提供所需的顏色。 我的問題是它可以完美運行,但是在程序運行時,您必須調整窗口大小(也稱為repaint())幾次,直到它真正起作用為止。 這是一些冗長的代碼,但是您可以略過令人討厭的布爾值重置。
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import java.awt.*;
public class PaintHomework extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
Canvas c1;
Canvas c2;
Graphics myG;
Graphics myG2;
TextField t1;
TextField currentColor;
TextField currentMode;
TextField currentShape;
int xForStraightLine=0;
int yForStraightLine=0;
int x = 0;
int y = 0;
int counter = 0;
int x11=1;
int x22=0;
int y11=1;
int y22=200;
int x1;
int y1;
int x2;
int y2;
int xSize=0;
int ySize=0;
float num1 = (float).01;
float num2 = (float)1.0;
float num3 = (float)1.0;
float hueNum=0;
int penSize = 10;
String text = "";
Button red;
Button blue;
Button green;
Button oval;
Button rect;
Button sprayCan;
Button straightLine;
Button filled;
Button empty;
Button free;
Button dragOval;
Button dragRect;
Button reset;
boolean ovalTime;
boolean rectTime;
boolean sprayCanTime;
boolean straightLineTime;
boolean filledTime;
boolean emptyTime;
boolean freeTime;
boolean dragOvalTime;
boolean dragRectTime;
public void init()
{
this.setSize(1350,651);
c1 = new Canvas();
add(c1);
c1.setSize(900,450);
c1.addMouseListener(this);
c1.addMouseMotionListener(this);
red = new Button("Red");
add(red);
red.addActionListener(this);
red.setBackground(Color.red);
red.addMouseListener(this);
blue = new Button("Blue");
add(blue);
blue.addActionListener(this);
blue.setBackground(Color.blue);
blue.addMouseListener(this);
green = new Button("Green");
add(green);
green.addActionListener(this);
green.setBackground(Color.green);
green.addMouseListener(this);
oval = new Button("Oval");
add(oval);
oval.addActionListener(this);
rect = new Button("Square");
add(rect);
rect.addActionListener(this);
sprayCan = new Button("Spray Can");
add(sprayCan);
sprayCan.addActionListener(this);
filled = new Button("Filled");
add(filled);
filled.addActionListener(this);
empty = new Button("Empty");
add(empty);
empty.addActionListener(this);
free = new Button("Free");
add(free);
free.addActionListener(this);
straightLine = new Button("Straight Line");
add(straightLine);
straightLine.addActionListener(this);
dragOval = new Button("Drag Oval");
add(dragOval);
dragOval.addActionListener(this);
dragRect = new Button("Drag Square");
add(dragRect);
dragRect.addActionListener(this);
t1 = new TextField("10");
add(t1);
t1.addActionListener(this);
currentColor = new TextField("Black");
add(currentColor);
currentColor.addActionListener(this);
currentMode = new TextField("Free");
add(currentMode);
currentMode.addActionListener(this);
currentShape = new TextField("Oval");
add(currentShape);
currentShape.addActionListener(this);
reset = new Button("Reset");
add(reset);
reset.addActionListener(this);
c2 = new Canvas();
add(c2);
c2.addMouseListener(this);
c2.setSize(900,200);
ovalTime = false;
rectTime = false;
sprayCanTime = false;
straightLineTime = false;
filledTime = false;
emptyTime = true;
freeTime = true;
dragOvalTime = false;
dragRectTime = false;
myG = c1.getGraphics();
myG2 = c2.getGraphics();
currentShape.setText("");
drawRainbow();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==red)
{
myG.setColor(Color.red);
}
if(e.getSource()==blue)
{
myG.setColor(Color.blue);
}
if(e.getSource()==green)
{
myG.setColor(Color.green);
}
if(e.getSource()==oval)
{
ovalTime = true;
rectTime = false;
freeTime = false;
straightLineTime = false;
emptyTime = true;
currentMode.setText("Empty");
currentShape.setText("Oval");
}
if(e.getSource()==rect)
{
ovalTime = false;
rectTime = true;
currentShape.setText("Square");
}
if(e.getSource()==sprayCan)
{
sprayCanTime = true;
straightLineTime = false;
filledTime = false;
emptyTime = false;
freeTime = false;
dragOvalTime = false;
dragRectTime = false;
currentMode.setText("Spray");
currentShape.setText("");
}
if(e.getSource()==straightLine)
{
straightLineTime = true;
sprayCanTime = false;
filledTime = false;
emptyTime = false;
freeTime = false;
dragOvalTime = false;
dragRectTime = false;
currentMode.setText("Line");
}
if(e.getSource()==filled)
{
filledTime = true;
sprayCanTime = false;
straightLineTime = false;
emptyTime = false;
freeTime = false;
dragOvalTime = false;
dragRectTime = false;
currentMode.setText("Filled");
}
if(e.getSource()==empty)
{
emptyTime = true;
sprayCanTime = false;
straightLineTime = false;
filledTime = false;
freeTime = false;
dragOvalTime = false;
dragRectTime = false;
currentMode.setText("Empty");
}
if(e.getSource()==free)
{
freeTime = true;
sprayCanTime = false;
straightLineTime = false;
filledTime = false;
emptyTime = false;
dragOvalTime = false;
dragRectTime = false;
currentMode.setText("Free");
}
if(e.getSource()==dragOval)
{
dragOvalTime = true;
freeTime = false;
sprayCanTime = false;
straightLineTime = false;
filledTime = false;
emptyTime = false;
dragRectTime = false;
currentMode.setText("Drag Oval");
}
if(e.getSource()==dragRect)
{
dragRectTime = true;
dragOvalTime = false;
freeTime = false;
sprayCanTime = false;
straightLineTime = false;
filledTime = false;
emptyTime = false;
currentMode.setText("Drag Square");
}
if(e.getSource()==t1)
{
text = t1.getText();
penSize = Integer.parseInt(text);
}
if(e.getSource()==reset)
{
myG.setColor(Color.white);
myG.fillRect(0, 0, 900, 450);
myG.setColor(Color.black);
ovalTime = true;
rectTime = false;
sprayCanTime = false;
straightLineTime = false;
filledTime = false;
emptyTime = true;
freeTime = true;
dragOvalTime = false;
dragRectTime = false;
t1.setText("10");
penSize = 10;
currentColor.setText("Black");
currentMode.setText("Free");
currentShape.setText("");
}
}
public void paint(Graphics g)
{
c1.setLocation(0,0);
g.drawRect(0, 0, 901, 450);
free.setLocation(1050, 250);
reset.setLocation(1275, 250);
straightLine.setLocation(950,250);
t1.setLocation(1100, 50);
t1.setSize(40,20);
g.drawString("Press Enter After Changing Size", 1050, 30);
g.drawRect(1049, 18, 177, 20);
currentColor.setLocation(990, 90);
currentMode.setLocation(1200, 90);
currentMode.setSize(65,20);
currentShape.setLocation(1095, 90);
dragOval.setLocation(1100,250);
dragRect.setLocation(1175,250);
c2.setLocation(0,495);
drawRainbow();
}
public void drawRainbow()
{
counter = 0;
x11=1;
x22=0;
y11=1;
y22=200;
num1 = (float).01;
num2 = (float)1.0;
num3 = (float)1.0;
while(counter < 1349)
{
myG2.setColor(Color.getHSBColor(num1, num2, num3));
myG2.drawLine(x11,y11,x22,y22);
num1=(float)(num1+.001);
x11++;
x22++;
counter++;
}
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
if(e.getSource()==red)
{
red.setBackground(Color.yellow);
}
if(e.getSource()==blue)
{
blue.setBackground(Color.magenta);
}
if(e.getSource()==green)
{
green.setBackground(Color.pink);
}
if(straightLineTime)
{
xForStraightLine = e.getX();
yForStraightLine = e.getY();
}
if(ovalTime)
{
if(!freeTime)
{
if(emptyTime)
{
myG.drawOval(e.getX(),e.getY(),penSize,penSize);
}
if(filledTime)
{
myG.fillOval(e.getX(),e.getY(),penSize,penSize);
}
}
}
if(rectTime)
{
if(!freeTime)
{
if(emptyTime)
{
myG.drawRect(e.getX(),e.getY(),penSize,penSize);
}
if(filledTime)
{
myG.fillRect(e.getX(),e.getY(),penSize,penSize);
}
}
}
if(freeTime)
{
x = e.getX();
y = e.getY();
ovalTime = false;
rectTime = false;
currentShape.setText("");
}
if(dragOvalTime || dragRectTime)
{
x1=e.getX();
y1=e.getY();
}
if(e.getSource()==c2)
{
hueNum = e.getX();
hueNum = hueNum+70;
hueNum = (float)(hueNum*.001);
myG.setColor(Color.getHSBColor(hueNum,num2,num3));
}
}
public void mouseReleased(MouseEvent e)
{
if(e.getSource()==red)
{
red.setBackground(Color.red);
currentColor.setText("Red");
}
if(e.getSource()==blue)
{
blue.setBackground(Color.blue);
currentColor.setText("Blue");
}
if(e.getSource()==green)
{
green.setBackground(Color.green);
currentColor.setText("Green");
}
if(straightLineTime)
{
myG.drawLine(xForStraightLine, yForStraightLine, e.getX(), e.getY());
}
if(dragOvalTime)
{
x2=e.getX();
y2=e.getY();
xSize = Math.abs(x2-x1);
ySize = Math.abs(y2-y1);
if(x2>x1 && y2>y1) //if top left to bottom right
{
myG.drawOval(x1,y1,xSize,ySize);
}
if(x2<x1 && y2<y1) //if bottom right to top left
{
myG.drawOval(x2,y2,xSize,ySize);
}
if(x2<x1 && y2>y1) //if top right to bottom left
{
myG.drawOval(x2,y1,xSize,ySize);
}
if(x2>x1 && y2<y1) //if bottom left to top right
{
myG.drawOval(x1, y2, xSize, ySize);
}
/* There are 4 possible cases, originally it draws on the initial press point.
* Judge each of the cases be seeing which coordinates are larger than others, to know whether to start on draw or end point.
*/
}
if(dragRectTime)
{
x2=e.getX();
y2=e.getY();
xSize = Math.abs(x2-x1);
ySize = Math.abs(y2-y1);
if(x2>x1 && y2>y1) //if top left to bottom right
{
myG.drawRect(x1,y1,xSize,ySize);
}
if(x2<x1 && y2<y1) //if bottom right to top left
{
myG.drawRect(x2,y2,xSize,ySize);
}
if(x2<x1 && y2>y1) //if top right to bottom left
{
myG.drawRect(x2,y1,xSize,ySize);
}
if(x2>x1 && y2<y1) //if bottom left to top right
{
myG.drawRect(x1, y2, xSize, ySize);
}
/* There are 4 possible cases, originally it draws on the initial press point.
* Judge each of the cases be seeing which coordinates are larger than others, to know whether to start on draw or end point.
*/
}
}
public void mouseDragged(MouseEvent arg0)
{
if(ovalTime)
{
if(emptyTime)
{
myG.drawOval(arg0.getX(),arg0.getY(),penSize,penSize);
}
if(filledTime)
{
myG.fillOval(arg0.getX(),arg0.getY(),penSize,penSize);
}
}
if(rectTime)
{
if(emptyTime)
{
myG.drawRect(arg0.getX(),arg0.getY(),penSize,penSize);
}
if(filledTime)
{
myG.fillRect(arg0.getX(),arg0.getY(),penSize,penSize);
}
}
if(sprayCanTime)
{
myG.drawOval(arg0.getX(),arg0.getY(),penSize,penSize);
myG.fillOval(arg0.getX(),arg0.getY(),penSize,penSize);
}
if(freeTime)
{
myG.drawLine(arg0.getX(), arg0.getY(), x, y);
x = arg0.getX();
y = arg0.getY();
}
}
public void mouseMoved(MouseEvent arg0)
{
}
}
永遠不要維護您未創建的任何Graphics
上下文的任何引用
這個...
myG = c1.getGraphics();
myG2 = c2.getGraphics();
不是如何執行自定義繪畫。 除了getGraphics
可以返回null
,這些只是最后一個繪制周期的“快照”。 而且,沒有人保證這些方法返回的參考將在下一個繪制周期中使用相同的參考。
我不確定為什么在Swing上使用AWT,但這是您的選擇。
創建一個自定義版本的Canvas
並覆蓋它的paint
方法。 使用Graphics
上下文,以該方法執行所有繪制。 不要忘記先調用super.paint
!
閱讀AWT和Swing中的繪畫以獲取更多詳細信息
有趣的程序。 嘗試這個。
更改:
c2 = new Canvas();
至:
c2 = new Canvas() {
public void paint(final Graphics g) {
drawRainbow(g);
}
};
在drawRainbow(Graphics g)中使用傳遞的圖形對象g,並刪除對drawRainbow()的任何調用,如下所示:
public void drawRainbow(final Graphics g) {
...
g.setColor(Color.getHSBColor(num1, num2, num3));
g.drawLine(x11, y11, x22, y22);
...
編輯:請注意您接受的答案中有關圖形的建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.