私はペイントプログラムを作成しており、スペクトル内のすべての色を含むカラーストリップを配置して、ユーザーがどこでもクリックするだけで、(大まかに) 目的の色を与えることができるようにしています。私の問題は、それが完全に機能することですが、プログラムが実行されると、実際に機能するまでウィンドウのサイズを変更する必要があります (別名 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)
{
}
}