我有点坚持维持JPanel
的宽高比。
我有Netbeans默认生成的JFrame
边框布局。整个框架都填充了我的JPanel
(来自我的自定义类GraphicsPanel.java,扩展了JPanel
)。
GraphicsPanel类只有一个用于绘制带有一些基本构造函数的图形的重载方法。我的JPanel
上有一个矩形和多边形。
我想要实现的是当我重新调整帧的大小时,我希望保持JPanel
宽高比为4:3(或类似的东西)。当框架对于纵横比来说太大时,它会用一些默认颜色填充框架背面。
我已经阅读了一些有关宽高比的主题(例如this)。但没有运气,我仍然不知道如何做到这一点。
我的JPanel
课程中有我的代码。我是初学者,而且我正在尝试学习如何使用Java图形(绘制,填充,调整大小等),所以请放轻松我。
import java.awt.Color;
import java.awt.Graphics;
public class GraphicsPanel extends javax.swing.JPanel {
private int x;
private int z;
private int y;
public GraphicsPanel() { //constructor
initComponents();
x = getHeight() / 2;
z = getWidth() / 2;
y = getHeight() - 1;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
x = getHeight() / 2;
z = getWidth() / 2;
y = getHeight() - 1;
setBackground(new Color(255, 255, 255));
g.setColor(Color.red);
g.fillRect(0, x, getWidth() - 1, y);
g.setColor(Color.blue);
int[] poleX = {0, 0, z};
int[] poleY = {0, y, x};
g.fillPolygon(poleX, poleY, 3);
}
}
答案 0 :(得分:1)
我认为您想要的是在您的面板中绘制您的图像(看起来很像捷克国旗!),但不会填满所有空间。例如,如果框架非常宽,则图像将不会填充整个宽度。如果我理解正确,这不是那么难。
在您的代码中,您可以获得面板的宽度和高度。当你这样做时,做一些算术。如果纵横比“太宽”,那么您的图像将填充高度,否则它将填充宽度。现在您知道一个维度,因此您可以计算另一个维度,并且只绘制足够大的图像以适合这些维度。
修改:示例计算......
(GSCHCT00002541001_iso_:1.36551607731154023284,((((tr_B6K9U3_B6K9U3_TOXGO_1_4alphaglucanbranching_enzyme_OS=Toxoplasma_gondii_GN=TGVEG_316520_PE=4_SV=1_:0.00000121823973671653,tr_Q5IXJ1_Q5IXJ1_TOXGO_Putative_1_4alphaglucan_branching_enzyme_1_OS=Toxoplasma_gondii_PE=4_SV=1_:0.00000121823973671653)100:0.08309756202092893895,NCLIV_058970___organism=Neospora_caninum_Liverpool___product=GlgB_EC_2_4_1_18_related___location=FR82339249768214985526___length=910___sequence_SO=chromosome___SO=protein_codingLength=910_:0.10501488554905853701)100:2.09488643049910772120,((sp_D2WL32_GLGB3_ARATH_1_4alphaglucanbranching_enzyme_3_chloroplastic/amyloplastic_OS=Arabidopsis_thaliana_GN=SBE3_PE=1_SV=1_:0.00000793499571061110,sp_D2WL323_GLGB3_ARATH_Isoform_3_of_1_4alphaglucanbranching_enzyme_3_chloroplastic/amyloplastic_OS=Arabidopsis_thaliana_GN=SBE3_:0.01062473346874111095)100:1.22585344127521778113,(((((TVAG_276310___Trichomonas_vaginalis_G3___starch_branching_enzyme_II_putative___protein___length=671_:0.26096529219513048270,TVAG_453180___Trichomonas_vaginalis_G3___amylase_putative_=_starch_branching_enzyme___protein___length=671_:0.36958750954860625226)100:0.41842557987087769522,(((Query_116761_1_4alphaglucan_branching_enzyme_Amphimedon_queenslandica__:0.00000121823973671653,jgi_Monbr1_17492_estExt_gwp_gw1_C_30090_1_4alphaglucan_branching_enzyme_Monosiga_brevicollis_MX1__:0.00000121823973671653)100:0.62703546565975865068,gi_602378_gb_AAB64488_1__1_4alphaglucan_branching_enzyme_Saccharomyces_cerevisiae__:0.53686309075793836598)70:0.13386580507453740840,sp_Q555Q9_GLGB_DICDI_1_4alphaglucanbranching_enzyme_OS=Dictyostelium_discoideum_GN=glgB_PE=3_SV=1_:0.47890669361866494702)70:0.10263562958656322066)60:0.09627472451982788115,((DHA2_15823___Giardia_Assemblage_A2_isolate_DH___1_4alphaglucan_branching_enzyme___protein___length=790_:1.38033367488971192572,(gi_403355152_gb_EJY77145_1_putative_1_4alphaglucan_branching_enzyme_from_glycoside_hydrolase_family_GH13_Oxytricha_trifallax__:0.57062195989872910307,gi_403359242_gb_EJY79278_1__putative_1_4alphaglucan_branching_enzyme_from_glycoside_hydrolase_family_GH13_Oxytricha_trifallax__:0.45721715963625586543)100:0.16040553970790100147)60:0.18422255610153490113,(tr_A8J2H1_A8J2H1_CHLRE_Starch_branching_enzyme_Fragment_OS=Chlamydomonas_reinhardtii_GN=SBE1_PE=4_SV=1_:0.70822871603694392828,((tr_A8HW52_A8HW52_CHLRE_Starch_branching_enzyme_OS=Chlamydomonas_reinhardtii_GN=SBE2_PE=4_SV=1_:0.20373019381459359090,tr_A8IHX1_A8IHX1_CHLRE_Starch_branching_enzyme_OS=Chlamydomonas_reinhardtii_GN=SBE3_PE=4_SV=1_:0.19417255052024848250)100:0.36247563752590050701,(sp_O23647_GLGB1_ARATH_1_4alphaglucanbranching_enzyme_21_chloroplastic/amyloplastic_OS=Arabidopsis_thaliana_GN=SBE2_1_PE=2_SV=1_:0.18306459143375086729,sp_Q9LZS3_GLGB2_ARATH_1_4alphaglucanbranching_enzyme_22_chloroplastic/amyloplastic_OS=Arabidopsis_thaliana_GN=SBE2_2_PE=2_SV=1_:0.19172895913289067504)100:0.26486677810195868865)50:0.16685150542870180734)40:0.17590037403691774487)0:0.06573467470539001711)20:0.08495767144039514940,((GSCHCT00008657001_starch_branching_enzyme_:0.41758330756918182747,evm_model_contig_2064_4_SBE_:0.36023201026789519741)100:0.12831089761745814726,CMH144C_branching_enzyme_:0.31146443745380530954)100:0.11773937724705520191)20:0.14200386638651510407,NCLIV_004200___organism=Neospora_caninum_Liverpool___product=hypothetical_protein___location=FR82338115074771519904_____length=1734___sequence_SO=chromosome___SO=protein_codingLength=1734_:1.56986633442481493539)60:0.27085853102669676939)50:0.60075545485436487869)30:0.54380192798189208592,(gi_403347780_gb_EJY73324_1_Putative_alphaamylase_Oxytricha_trifallax__:3.79816205351230751219,(jgi_Guith1_136858_jgi_Guith1_136858_fgenesh2_pg_22_#_123124488_:1.19666825808863852565,(gi_32398951_emb_CAD98416_1__1_4alphaglucan_branching_enzyme_possible_Cryptosporidium_parvum__:1.19101784665038845645,(NCLIV_063470___organism=Neospora_caninum_Liverpool___product=putative_1_4alphaglucan_branching_enzyme___location=FR82339325724112581747_____length=941___sequence_SO=chromosome___SO=protein_codingLength=941_:0.68667357703692166737,((ConsensusfromContig12026snap_maskedConsensusfromContig12026abinitgene0_0mRNA1cds4076/67850_:1.79972121664357276316,ConsensusfromContig6995snap_maskedConsensusfromContig6995abinitgene0_4mRNA1cds527/439144570__:0.92078205301668014648)40:0.21444814008989548926,Cvel_20619___organism=Chromera_velia_CCMP2878___product=Malto-oligosyltrehalose_trehalohydrolase_putative___location=Cvel_scaffold1867_1208-11669_-___length=662___sequence_SO=supercontig___SO=protein_coding_:0.93952895504658817671)20:0.18331222947529093870)70:0.45890345971164558936)80:0.33542172384911861371)100:1.10060499239467945998)30:0.42698796779576941862)100:2.44706655918227600210,sp_Q9M0S5_ISOA3_ARATH_Isoamylase_3_chloroplastic_OS=Arabidopsis_thaliana_GN=ISA3_PE=2_SV=2_:0.91892546724791945856);
面板的大小和形状很难(不可能?)来修复。您必须将面板添加到容器(示例中的JFrame,但可以是任何内容),容器将使用布局管理器来设置面板大小。
答案 1 :(得分:1)
我需要维护我正在绘制的图像(但我想维护组件的比例会更容易)。因为我的图像不是经典图像(png,jpg),而是一些看起来像图像的绘制/填充形状。
您可以轻松地以适当的宽高比创建一个合理大小的BufferedImage
,并将您的形状绘制到BufferedImage
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
// paint image background
g2d.setColor( ... );
g2d.fillRect(0, 0, width, height);
// draw shapes
g2d.fillOval(....);
您可以使用Darryls Stretch Icon在JLabel中显示图像。 StretchIcon
类将在标签可用空间内按比例缩放图标:
JLabel label = new JLabel( new StretchIcon(image) );
frame.add(label);