Chaz Bertino
Chaz Bertino

Reputation: 9

Java input error

Okay, I give up. I've been a C++ programmer for a few years, but I tried learning Java because it's a popular language. As I studied I learn a lot, but eventually I started playing around and tried using the input system so that when I click this red diamond shape polygon it turns green, but after several frustrating days... nada. I still only have a red diamond. It's probably something very small but I just can't find it

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

Upvotes: 0

Views: 167

Answers (2)

paulsm4
paulsm4

Reputation: 121799

@Chaz Bertino -

I think the entire problem was using "Canvas" (obsolete since Java 1.2) vs "JPanel" (replaces the old "Panel" and "Canvas" combined since J2SE 2.0).

Here's another example that behaves like you expected. The main differences is that it uses a Swing JPanel instead of an 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()...");           
    }

}

Upvotes: 0

paulsm4
paulsm4

Reputation: 121799

I got rid of the extraneous "SpaceInput" class and added the mouse listener to the applet (not "castle"). And everything worked :)

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

}

Upvotes: 1

Related Questions