我可以设置我的Swing对象按比例缩放到其底层容器的大小吗?

时间:2015-07-18 15:46:46

标签: java swing graphics

我可以使用我实例化的customerId个对象之一,并以某种方式将其设置为与其基础MyHouse容器成比例地缩放吗?

当我使用从NscComponent继承的setSize方法时,它不会更改NscComponent对象的大小。它使容器变小,但不是对象,容器调整大小并剪切掉大部分house1 MyHouse对象。我曾尝试在house1中创建一个新方法,它调整组成整个房子的每个MyHouse.java组件的大小,然后工作,但随后坐标全部搞砸了,房子本身看起来像一切都被打破了。

似乎必须操纵MyHouse对象的每个单独组件才能调整大小,从而无法创建对象。

以下是MyHouse代码:

MyHouse

这是/** * A new graphic component. This component is used in * the notes to highlight the process of designing a * new object and then implementing it. * * @author Dan Jinguji * @author Chris Wilson * @version Demo: MyHouse */ public class MyHouse extends NscComponent { // instance variables private NscUpTriangle theRoof; private NscRectangle theWalls; private NscRectangle theDoor; private NscRectangle theWindow; private NscRectangle theWindowPane; /** * Constructor for objects of class MyHouse. * This creates a MyHouse object at the specified * location * @param x the x-coordinate for the object * @param y the y-coordinate for the object */ public MyHouse(int x, int y) { // Specify the constructor for the superclass super(x, y, 120, 90); // Create the roof object theRoof = new NscUpTriangle(0, 0, 120, 40); // Set the characteristics of the roof theRoof.setFilled(true); theRoof.setBackground(new java.awt.Color(0x99, 0x33, 0x00)); // Place the roof in the MyHouse object add(theRoof); // Create the walls object theWalls = new NscRectangle(10, 40, 100, 50); // Set the characteristics of the walls theWalls.setFilled(true); theWalls.setBackground(java.awt.Color.blue); // Place the walls in the MyHouse object add(theWalls); // Create the door object theDoor = new NscRectangle(48, 50, 24, 40); // Set the characteristics of the door theDoor.setFilled(true); theDoor.setBackground(new java.awt.Color(0x99, 0x66, 0x33)); // Place the door in the MyHouse object add(theDoor); // Create the window object theWindow = new NscRectangle(80, 50, 25, 20); // Set color theWindow.setFilled(true); theWindow.setBackground(java.awt.Color.white); // Create custom glass color java.awt.Color glass = new java.awt.Color(153, 255, 255); // Create window pane object theWindowPane = new NscRectangle(83, 53, 19, 14); // Set color theWindowPane.setFilled(true); theWindowPane.setBackground(glass); // add window and pane add(theWindow); add(theWindowPane); } /* * Overloaded constructor for the MyHouse class * Creates the house objects and also inputs color variable * * @param x the x-coordinate for the object * @param y the y-coordinate for the object * @param colorVar the color for the walls of the house */ public MyHouse(int x, int y, java.awt.Color colorVar) { // constructor, call super before anything else super(x, y, 120, 90); // build roof and set color, then add theRoof = new NscUpTriangle(0, 0, 120, 40); theRoof.setFilled(true); theRoof.setBackground(new java.awt.Color(0x99, 0x33, 0x00)); add(theRoof); // Create the walls and pass the 3rd constructor parameter for color conversion, then add theWalls = new NscRectangle(10, 40, 100, 50); theWalls.setFilled(true); theWalls.setBackground(colorVar); add(theWalls); // Create door, set color and add theDoor = new NscRectangle(48, 50, 24, 40); theDoor.setFilled(true); theDoor.setBackground(new java.awt.Color(0x99, 0x66, 0x33)); add(theDoor); // Create the window object theWindow = new NscRectangle(80, 50, 25, 20); // Set color theWindow.setFilled(true); theWindow.setBackground(java.awt.Color.white); // Create custom glass color java.awt.Color glass = new java.awt.Color(153, 255, 255); // Create window pane object theWindowPane = new NscRectangle(83, 53, 19, 14); theWindowPane.setFilled(true); theWindowPane.setBackground(glass); // add window and pane add(theWindow); add(theWindowPane); } /** * Change the color of the house * * @param c The color for the walls of the house */ public void setColor(java.awt.Color c) { theWalls.setBackground(c); repaint(); } /** * Retrieve the color of the house * * @return The color of the walls of the house */ public java.awt.Color getColor() { return theWalls.getBackground(); } } 代码,它实例化3个MyScene个对象并将它们放入场景中。

MyHouse

1 个答案:

答案 0 :(得分:0)

您的问题是您手动指定每个组件的大小。当然,如果您手动指定它们,重新计算物体的大小和位置将是一个巨大的痛苦!当然,您必须使用类似ComponentListener的内容再次指定它们。

然而......有更好的方法。 Swing有一个LayoutManager概念,可以帮助你完成所有这些工作!

一些不同的布局管理器比其他布局管理器做得更好,就个人而言,我发现GroupLayout在处理调整窗口大小的具体问题时最为强大。

tl; dr所以,请记住,不要使用.setSize()指定尺寸,也不要使用setLocation()指定尺寸,请使用LayoutManager