0

さて、私はあきらめます。C++プログラマー歴は数年ですが、人気の言語ということでJavaを学んでみました。勉強するうちに多くのことを学びましたが、最終的には、この赤いひし形のポリゴンをクリックすると緑に変わる入力システムを試してみましたが、数日後... だ。私はまだ赤いダイヤモンドしか持っていません。それはおそらく非常に小さいものですが、私はそれを見つけることができません

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

@チャズ・ベルティーノ -

全体の問題は、「Canvas」(Java 1.2以降廃止)と「JPanel」(J2SE 2.0以降の古い「Panel」と「Canvas」の組み合わせを置き換える)の使用にあったと思います。

期待どおりに動作する別の例を次に示します。主な違いは、AWT Canvas の代わりに Swing JPanel を使用することです。

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 に答える