2012-06-28 63 views
0

好吧,我放棄了。我幾年來一直是C++程序員,但我嘗試學習Java,因爲它是一種流行的語言。在我學習的過程中,我學到了很多東西,但最終我開始玩耍並嘗試使用輸入系統,這樣當我點擊這個紅色菱形多邊形時,它變成了綠色,但經過幾天令人沮喪的日子...... nada。我仍然只有一顆紅色的鑽石。這可能是一些非常小,但我無法找到它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); 
    } 
} 
+1

什麼是錯誤?我們沒有通過你的代碼並發現錯誤! –

+2

@Hemant Metalia - 問題在於鼠標偵聽器永遠不會被調用,所以顏色永遠不會被改變。這是一個合法的問題... – paulsm4

+0

Applets不是學習Java GUI編程的好方法。製作應用。第一。不要使用AWT,請使用Swing。大多數曾經知道如何使用AWT的人都忘記了細節。 –

回答

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

} 
+0

Paulsm4我使用的是eclipse,當你點擊時你的代碼會變成綠色,即使它不是點擊的鑽石。你可以改變你的代碼,只有當鑽石被點擊時它纔會改變顏色。 –

+0

如果是這種情況,請嘗試使用他的代碼,但將'addMouseListener(this)'更改爲'castle.addMouseListener(this)' – gobernador

+0

Gobernador,這與我的問題相同。 –

0

@Chaz Bertino -

我認爲整個問題是用 「畫布」(過時自從Java 1.2)VS 「的JPanel」(取代舊的 「面板」 的「畫布「自J2SE 2.0起合併)。

下面是另一個行爲像你期望的例子。主要區別在於它使用的是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 ("[email protected]()..."); 
     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 ("[email protected](): 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()...");   
    } 

} 
+0

好的,更接近,但點擊空間是正方形,而不是鑽石。 –

+0

1)鼠標監聽器適用於整個面板(面板的整個矩形),2)您可以編寫一個函數,該函數使用鼠標x/y並確定您是否位於面板的特定區域內,如果需要3 )主要問題 - 爲什麼不檢測鼠標點擊 - 已解決。正確? – paulsm4