Java 如何缩放基本图形?

Java 如何缩放基本图形?,java,arrays,list,jpanel,awt,Java,Arrays,List,Jpanel,Awt,我想有一个形状列表,显示在我的窗口中。无论何时更改窗口的大小,我都希望缩放所有的图形 我已经准备了一些类,这些类将随机形状的信息存储在一个列表中(矩形、椭圆形等)。我对所有的画都没有问题,但我不能处理缩放问题。我的解决方案不会改变任何东西,也不会使所有形状消失 public class Shape extends JPanel{ int x, y,width,height,red,green,blue; double scX, scY; //scale x and y

我想有一个形状列表,显示在我的窗口中。无论何时更改窗口的大小,我都希望缩放所有的图形

我已经准备了一些类,这些类将随机形状的信息存储在一个列表中(矩形、椭圆形等)。我对所有的画都没有问题,但我不能处理缩放问题。我的解决方案不会改变任何东西,也不会使所有形状消失

public class Shape extends JPanel{
    int x, y,width,height,red,green,blue;
    double scX, scY; //scale x and y

    public Shape(int x, int y, int width, int height, int red, int green, int blue) {
//...long constructor
        scX=1;
        scY=1;
    }
    void randomizeValues(){...}

        void setScale(double x, double y) {
        this.scX = x;
        this.scY = y;
    }
}

public class Rectangle extends Shape{

    public Rectangle(int x, int y, int width, int height, int red, int green, int blue) {
        super(x, y, width, height, red, green, blue);
    }

    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        graphics.fillRect((int)(x*scX), (int)(y*scY), (int)(width*scX), (int)(height*scY));
    }
}


class Window extends JFrame {

    int defaultWidth = 768;
    int defaultHeight = 512;
    List<Shape> paintList = new ArrayList<>();

 public Window() {
        setTitle("Shape");
        add(new DrawShape);
        setSize(defaultWidth, defaultHeight);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

class DrawShape extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (int i = 0; i< paintList.size(); i++) {
                Shape s = paintList.get(i);
                s.setScale(this.getWidth()/defaultWidth, this.getHeight()/defaultHeight);
                s.paintComponent(g);
            }
   }
}
公共类形状扩展了JPanel{
整数x,y,宽度,高度,红色,绿色,蓝色;
双scX,scY;//缩放x和y
公共形状(整数x、整数y、整数宽度、整数高度、整数红色、整数绿色、整数蓝色){
//…长构造函数
scX=1;
scY=1;
}
void randomizeValues(){…}
空隙设置刻度(双x,双y){
这个.scX=x;
this.scY=y;
}
}
公共类矩形扩展形状{
公共矩形(整数x、整数y、整数宽度、整数高度、整数红色、整数绿色、整数蓝色){
超级(x、y、宽度、高度、红色、绿色、蓝色);
}
@凌驾
受保护的空心油漆组件(图形){
super.paintComponent(图形);
图形.fillRect((int)(x*scX),(int)(y*scY),(int)(宽度*scX),(int)(高度*scY));
}
}
类窗口扩展JFrame{
int defaultWidth=768;
int defaultHeight=512;
List paintList=new ArrayList();
公共窗口(){
片名(“形状”);
添加(新的DrawShape);
设置大小(默认宽度、默认高度);
setVisible(真);
setLocationRelativeTo(空);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
类DrawShape扩展了JPanel{
公共组件(图形g){
超级组件(g);
对于(int i=0;i

如何进行适当的缩放?我应该在何处乘以值,以使一切正常工作?

首先,您不应该将JPanel添加到窗口中,而是将其设置为ContentPane:
setContentPane(new DrawShape());
。接下来,您不应该在循环内部重新绘制,而应该在外部(在
paintComponent的末尾)重新绘制(图g)
方法)。这样,JPanel应该永久性地再次绘制。 如果需要根据窗口尺寸更改形状的大小,请按照JPanel
paintComponent(Graphics g)
方法中的操作:

//fill it with the shapes base sizes (index 0 = width, index 1 = height)
Map<Shape,int[]> shapeSizes = new HashMap<Shape,int[]>();
public void paintComponent(Graphics g) {
double widthCoeff = this.getWidth()/(double)Window.this.defaultWidth;
double heightCoeff = this.getHeight()/(double)Window.this.defaultHeight;
for (int i = 0; i< paintList.size(); i++) {
Shape s = paintList.get(i);
int[] baseSize = shapeSizes.get(s);
int oldWidth = baseSize[0], oldHeight = baseSize[1];
int width = oldWidth*widthCoeff, height = oldHeight*heightCoeff;
//you can now give the shape its new width and height
}
}
//填充形状基本尺寸(索引0=宽度,索引1=高度)
Map shapeSizes=new HashMap();
公共组件(图形g){
double widthCoeff=this.getWidth()/(double)Window.this.defaultWidth;
double heightCoeff=this.getHeight()/(double)Window.this.defaultHeight;
对于(int i=0;i
s.paintComponent(g);
-不,不要这样做,没有任何理由直接调用components
paint
方法。相反,设计一个或多个类来执行所需的操作(即
Box
,它不从具有“draw”的组件扩展)方法,您可以通过该方法传递
图形
上下文),在处理基于像素的渲染时,该方法可用于缩放
图形
上下文解决方案是缩放各个形状的坐标,这已经演示过了,如果您希望看到基于窗口大小的解决方案缩放,您可以看到您正在为自己的类使用诸如Shape、window和Rectangle之类的名称,这些名称已经在JavaAPI中定义(其中Shape是一个接口)。为避免混淆,您应该更改这些。您还应该查看Java教程。谢谢您的建议。如果在循环中调用listElement(I).paintComponent()是一个错误的操作,请告诉我,我应该如何绘制列表中的每个形状?我建议将
形状
对象转换为
多边形
s(由Java API定义),您可以通过调用
g.drawPolygon(polygon);
“首先,您不应该将您的JPanel添加到您的窗口,而是将其设置为ContentPane:setContentPane(new DrawShape());”自Java 1.6以来,
JFrame#自动添加到
ContentPane