2013-11-02 101 views
-2

我將如何將它變成兩個參數的構造函數,可用於指定繪製對象的位置和/或縮放對象。這用於Jframe圖形項目。如果它有幫助,我也可以發佈Jframe。兩個參數構造函數指定對象應該在哪裏繪製?

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import java.awt.geom.Ellipse2D; 
import java.awt.geom.Line2D; 
import javax.swing.JComponent; 
import java.awt.GradientPaint; 

/* 
    component that draws the concert speakers 
*/ 


public class ConcertSpeaker extends JComponent 
{ 
    public void paintComponent(Graphics g) 
    { 

     super.paintComponent(g); 

     // Recover Graphics2D 
     Graphics2D g2 = (Graphics2D) g; 

     // Speaker base 
     g2.setColor(Color.BLACK); 
     Rectangle base = new Rectangle (300, 400, 50, 100); 
     g2.fill(base); 

     // Speakers circles gray top 
     g2.setColor(Color.DARK_GRAY); 
     Ellipse2D.Double speakerTop = new Ellipse2D.Double(310, 410, 30, 30); 
     g2.fill(speakerTop); 

     //speakers circles black top 
     g2.setColor(Color.BLACK); 
     Ellipse2D.Double speakerTop1 = new Ellipse2D.Double(315, 415, 20, 20); 
     g2.fill(speakerTop1); 

     // Speakers circles gray bottom 
     g2.setColor(Color.DARK_GRAY); 
     Ellipse2D.Double speakerBottom = new Ellipse2D.Double(310, 450, 30, 30); 
     g2.fill(speakerBottom); 

     //speakers circles black bottom 
     g2.setColor(Color.BLACK); 
     Ellipse2D.Double speakerBottom1 = new Ellipse2D.Double(315, 455, 20, 20); 
     g2.fill(speakerBottom1); 


    } 
} 
+0

你認爲這可以做什麼?有任何想法嗎?有關構造函數的兩個參數是什麼的想法?任何嘗試製作一個? –

+0

代碼中沒有縮放比例。 – hasan83

回答

0

我不知道有一個很大的組成部分,是做的最好的事情,但這裏是你如何實現我明白你問(只是類的主體):

private int x, y; 
private float scale; 

public PosSCale(int x, int y, float scale) { 
    this.x = x; 
    this.y = y; 
    this.scale = scale; 
} 

public void paintComponent(Graphics g) 
{ 

    super.paintComponent(g); 

    // Recover Graphics2D 
    Graphics2D g2 = (Graphics2D) g; 
    g2.translate(x, y); 
    g2.scale(scale, scale); 

    // Speaker base 
    g2.setColor(Color.BLACK); 
    Rectangle base = new Rectangle (0, 0, 50, 100); 
    g2.fill(base); 

    // Speakers circles gray top 
    g2.setColor(Color.DARK_GRAY); 
    Ellipse2D.Double speakerTop = new Ellipse2D.Double(10, 10, 30, 30); 
    g2.fill(speakerTop); 

    //speakers circles black top 
    g2.setColor(Color.BLACK); 
    Ellipse2D.Double speakerTop1 = new Ellipse2D.Double(15, 15, 20, 20); 
    g2.fill(speakerTop1); 

    // Speakers circles gray bottom 
    g2.setColor(Color.DARK_GRAY); 
    Ellipse2D.Double speakerBottom = new Ellipse2D.Double(10, 50, 30, 30); 
    g2.fill(speakerBottom); 

    //speakers circles black bottom 
    g2.setColor(Color.BLACK); 
    Ellipse2D.Double speakerBottom1 = new Ellipse2D.Double(15, 55, 20, 20); 
    g2.fill(speakerBottom1); 


} 
相關問題