我如何将它变成一个两参数构造函数,可用于指定绘制对象的位置和/或缩放对象。这用于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)
我不确定有一个重要的组件是最好的事情,但这里是你如何实现我理解你所要求的(只是课堂上的主体):
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);
}