Tonno22
Tonno22

Reputation: 165

Changing my frame, and making the contents resizable

I'm doing an assignment and had some questions, when my frame opens when I run my program, it doesn't open to its full size, 700x100. Why is that? Also, how can I make the contents within the frame resizable?

Here's what I have.

package hw06;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TargetLogoUI {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
   JFrame frame = new JFrame();
    JTextField TargetMessagePanel = new JTextField();

    TargetMessagePanel.setBackground(new java.awt.Color(204, 204, 204));
    TargetMessagePanel.setFont(new java.awt.Font("Helvetica", 1, 72)); // NOI18N
    TargetMessagePanel.setText("Welcome to Target");

    frame.add(TargetMessagePanel, BorderLayout.NORTH);

    final TargetLogoPanel panel = new TargetLogoPanel();
    panel.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(java.awt.event.MouseEvent e) {
            panel.setTarget(true);
            panel.repaint();
        }
    });
    frame.add(panel);
    frame.setSize(700, 100);
    frame.setVisible(true);
}
 }

 class TargetLogoPanel extends JPanel {
private boolean isTarget;

public void paintComponent(Graphics g) {
    super.paintComponent(g); // erase back
    Graphics2D g2 = (Graphics2D) g;

    Color c = null;
    if (isTarget) {
        c = Color.BLUE;
    } else {
        c = Color.RED;
    }

    g2.setColor(c);
    g2.fill(new Ellipse2D.Double(250, 50, 160, 160));

    g2.setColor(Color.WHITE);
    g2.fill(new Ellipse2D.Double(275, 75, 110, 110));

    g2.setColor(c);
    g2.fill(new Ellipse2D.Double(300, 100, 60, 60));
}

public boolean isTarget() {
    return isTarget;
}

public void setTarget(boolean isTarget) {
    this.isTarget = isTarget;
}


}

Upvotes: 1

Views: 38

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

I'm doing an assignment and had some questions, when my frame opens when I run my program, it doesn't open to its full size, 700x100.

It in fact does size to 700 x 100.

I recommend that you not do it this way but rather that you override the getPreferredSize() method of the JPanel, and then call pack() on the JFrame prior to visualizing it. Something like:

class TargetLogoPanel extends JPanel {
   private static final int PREF_W = 700; //!!
   private static final int PREF_H = 300;
   private boolean isTarget;

   public void paintComponent(Graphics g) {
      super.paintComponent(g); // erase back
      Graphics2D g2 = (Graphics2D) g;
      // rendering your circles smoothly
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

      Color c = null;
      if (isTarget) {
         c = Color.BLUE;
      } else {
         c = Color.RED;
      }

      g2.setColor(c);
      g2.fill(new Ellipse2D.Double(250, 50, 160, 160));

      g2.setColor(Color.WHITE);
      g2.fill(new Ellipse2D.Double(275, 75, 110, 110));

      g2.setColor(c);
      g2.fill(new Ellipse2D.Double(300, 100, 60, 60));
   }

   @Override  //!!
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   public boolean isTarget() {
      return isTarget;
   }

   public void setTarget(boolean isTarget) {
      this.isTarget = isTarget;
   }

}

Also, how can I make the contents within the frame resizable?

Please elaborate on just what you mean here.

Upvotes: 2

Related Questions