我有一個jFrame(分辨率:300x300),並希望在其中顯示大量的橢圓。該字段的分辨率應該是4000x4000。我怎麼做?對於我創建的省略號字段,jComponent顯示所有500個省略號。如何在jFrame中顯示大量的Graphics2D?
你會發現它只會顯示jComponent的一部分,如何調整jComponent以顯示所有的橢圓?
http://i.stack.imgur.com/75QA9.png
它我橢圓的面積/ JComponent的
package home;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JComponent;
public class Karte extends JComponent {
private static final long serialVersionUID = 1L;
int AMOUNT= 500;
public ArrayList<Ellipse2D> Kreise = new ArrayList<Ellipse2D>();
public Karte() {
createCircles(AMOUNT);
}
private void createCircles(int AMOUNT) {
Random r = new Random();
for (int i = 1; i <= AMOUNT; i++) {
Kreise.add(new Ellipse2D.Double(r.nextInt(4000), r.nextInt(4000), 4, 4));
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D kreis = (Graphics2D) g;
for (Ellipse2D t : Kreise) {
kreis.setColor(Color.BLACK);
kreis.draw(t);
}
}
}
ANS這是我的GUI
package home;
import java.awt.Dimension;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
public class NewJFrame extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
public NewJFrame() {
initComponents();
}
private void initComponents() {
jLayeredPane1 = new javax.swing.JLayeredPane();
karte1 = new home.Karte();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout karte1Layout = new javax.swing.GroupLayout(karte1);
karte1.setLayout(karte1Layout);
karte1Layout.setHorizontalGroup(karte1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER).addGap(0, 4000, Short.MAX_VALUE));
karte1Layout.setVerticalGroup(karte1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER).addGap(0, 4000, Short.MAX_VALUE));
karte1.setBounds(0, 0, 4000, 4000);
karte1.setMinimumSize(new Dimension(300, 300));
jLayeredPane1.add(karte1, javax.swing.JLayeredPane.DEFAULT_LAYER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300,
Short.MAX_VALUE));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300,
Short.MAX_VALUE));
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JLayeredPane jLayeredPane1;
private home.Karte karte1;
}
1)人眼無法解析4000 x 4000. 2)數據(模型)需要與GUI(視圖)分開。因此,您始終可以通過縮放數據(將其乘以比例因子)並在顯示數據之前對其進行平移(添加平移因子)來轉換數據。 –
創建您想要顯示的圖像,將其大小調整爲組件的大小,然後在組件上繪製圖像。 – Paul