0

好吧,我放弃了。我已经做了几年 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 );
    }
}
4

2 回答 2

1

我摆脱了无关的“SpaceInput”类,并将鼠标侦听器添加到小程序(不是“城堡”)。一切正常:)

public class Vici extends Applet implements MouseListener
{

    private static final long serialVersionUID = 1L;
    private Space castle;

    public Vici()
    {
        castle = new Space();
        // castle.addMouseListener(this);
        addMouseListener (this);
    }



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


    }

    public void mouseEntered(MouseEvent m) {
System.out.println ("mouse entered...");
    }
    public void mouseExited(MouseEvent m) {
System.out.println ("mouse mouseExited...");            
    }
    public void mouseReleased(MouseEvent m)
    {
System.out.println ("mouse mouseReleased...");          
        switch(m.getButton())
        {
            case MouseEvent.BUTTON1:
                castle.setColor(Color.GREEN);
                castle.repaint();
                repaint();
        }
    }
    public void mouseClicked(MouseEvent m)
    {
System.out.println ("mouse mouseClicked...");           
        switch(m.getButton())
        {
            case MouseEvent.BUTTON1:
                castle.setColor(Color.GREEN);
                castle.repaint();
                repaint();
        }
    }

    public void mousePressed(MouseEvent m)
    {
System.out.println ("mouse mousePressed...");           
        switch(m.getButton())
        {
            case MouseEvent.BUTTON1:
                castle.setColor(Color.GREEN);
                castle.repaint();
                repaint();
        }
    }

}
于 2012-06-28T05:43:08.477 回答
0

@Chaz Bertino -

我认为整个问题是使用“Canvas”(自 Java 1.2 以来已过时)与“JPanel”(替换自 J2SE 2.0 以来组合的旧“Panel”和“Canvas”)。

这是另一个表现如您预期的示例。主要区别在于它使用 Swing JPanel 而不是 AWT Canvas:

import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class HelloAWT extends Applet {

    private Diamond diamond;
    private static final int height = 350;
    private static final int width = 350;

    public void init () {
        setBackground(Color.green);
        resize (width, height);
        diamond = new Diamond ();
        diamond.init (Color.green);
        add(diamond);
    }

    public void paint(Graphics g)
    {
System.out.println ("HelloAWT@paint()...");
        String msg = "Hello AWT!";
        g.setFont(new Font ("Arial", Font.BOLD, 18));
        FontMetrics fm = g.getFontMetrics ();
        int x = (getWidth() - fm.stringWidth(msg)) / 2;
        int y = (getHeight() / 2) + 50;
        g.drawString(msg, x, y);
        diamond.repaint ();
    }
}

class Diamond extends JPanel implements MouseListener {
    private static final int height = 100;
    private static final int width = 100;
    private Color c = Color.red;
    private Color bkgd;
    private int[] polyX;
    private int[] polyY;
    private Polygon poly;

    public void init (Color bkgd) {
System.out.println ("Diamond@init(): bkgd=" + bkgd + "...");        
        setPreferredSize (new Dimension(width, height));
        this.bkgd = bkgd;
        polyX = new int[] { width/2, width, width/2, 0 };
        polyY= new int[] { 0, height/2, height, height/2 };
System.out.println ("polyX=" + polyX[0] + "," + polyX[1] + "," + polyX[2] + "," + polyX[3] + "...");        
System.out.println ("polyY=" + polyY[0] + "," + polyY[1] + "," + polyY[2] + "," + polyY[3] + "...");        
        poly = new Polygon( polyX, polyY, polyX.length);
        addMouseListener (this);
    }

    public void paint(Graphics g)
    {       
System.out.println ("Diamond()@paint(), bounds=" + getHeight() + "," + getWidth () + "...");
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(bkgd);
        g2d.fillRect(0, 0, width, height);
        g2d.setColor(c);
        g2d.fill(poly);
    }

    public void mouseEntered(MouseEvent m) {
System.out.println ("mouseEntered()...");
    }

    public void mouseExited(MouseEvent m) {
System.out.println ("mouseExited()...");            
    }

    public void mouseReleased(MouseEvent m) {
System.out.println ("mouseReleased()...");          
    }

    public void mouseClicked(MouseEvent m) {
System.out.println ("mouseClicked(), current color=" + c + "...");
        if (c == Color.red)
            c = Color.blue;
        else
            c = Color.red;
        repaint ();
    }

    public void mousePressed(MouseEvent m) {
System.out.println ("mousePressed()...");           
    }

}
于 2012-06-29T05:31:21.533 回答