好吧,我放弃了。我已经做了几年 C++ 程序员,但我尝试学习 Java,因为它是一种流行的语言。在我学习的过程中,我学到了很多东西,但最终我开始玩弄并尝试使用输入系统,这样当我点击这个红色菱形多边形时,它会变成绿色,但经过几天令人沮丧的日子...... nada。我仍然只有一颗红色钻石。它可能是非常小的东西,但我找不到它
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Vici extends Applet
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Space castle;
public Vici()
{
castle = new Space();
castle.addMouseListener(new SpaceInput());
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
int width = getSize().width;
int height = getSize().height;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, width, height);
castle.paint(g2d);
}
class SpaceInput implements MouseListener
{
public void mouseEntered(MouseEvent m) { }
public void mouseExited(MouseEvent m) { }
public void mouseReleased(MouseEvent m)
{
switch(m.getButton())
{
case MouseEvent.BUTTON1:
castle.setColor(Color.GREEN);
castle.repaint();
repaint();
}
}
public void mouseClicked(MouseEvent m)
{
switch(m.getButton())
{
case MouseEvent.BUTTON1:
castle.setColor(Color.GREEN);
castle.repaint();
repaint();
}
}
public void mousePressed(MouseEvent m)
{
switch(m.getButton())
{
case MouseEvent.BUTTON1:
castle.setColor(Color.GREEN);
castle.repaint();
repaint();
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Space extends Canvas
{
private Polygon poly;
private Color c;
private int[] polyX = { 0, 24, 0, -24 };
private int[] polyY = { 24, 0, -24, 0 };
public void init()
{
poly = new Polygon( polyX, polyY, polyX.length);
c = Color.red;
}
Space()
{
init();
}
void setColor(Color c)
{
this.c = c;
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
AffineTransform identity = new AffineTransform();
g2d.setTransform(identity);
g2d.translate(100, 100);
g2d.setColor(c);
g2d.fill(poly);
}
public void update( Graphics g )
{
paint( g );
}
}