Java - repainting JPanel gives an error - java

I'm a beginner in Java, and I'm trying to create an application that draws a rectangle where ever the cursor is located. I've already done everything, but I can't get the mouseMoved(MouseEvent) method to repaint the JPanel. Without the repaint, the rectangle is only drawn once and that's it. With the repaint, it compiles fine, but when I run it, every time the mouse is moved, I get this big "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" error.
So, can anyone please help me out on this?
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Game extends JPanel implements MouseMotionListener
{
public static void main(String[] args) {
new Game().game();
}
JPanel panel;
JButton button2;
JButton button;
public void game() {
JPanel panel = new Game();
button = new JButton("Ok");
panel.setLayout(new FlowLayout());
panel.add(button);
button2 = new JButton("Cancel");
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setResizable(false);
frame.add(panel);
frame.setVisible(true);
panel.addMouseMotionListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
g.fillRect(x,y,100,100);
}
public void mouseMoved(MouseEvent evt) {
panel.repaint; //This is the line of code that I need help with. Thanks!
}
public void mouseDragged(MouseEvent evt) {}
}

Hopefully the comments in the code example, be able to tell what you doing wrong in your code :-), otherwise there is always a reason to put forth your doubts...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Game extends JPanel {
/*
* Now x and y are instance variables,
* whose values you can change at each
* MouseMove Event, and call repaint()
* to see the effects
*/
private int x;
private int y;
private MouseAdapter mouseActions =
new MouseAdapter() {
#Override
public void mouseMoved(MouseEvent me) {
/*
* Now as the Mouse moves, we simply
* updating the instance variables,
* i.e. x and y to the new values
* of the Mouse Location and calling
* repaint() to draw the rectangle.
* Since this class (Game) extends JPanel,
* hence all the functions of the JPanel
* belongs to this class, hence like
* as we call any other method of this
* class, without using the object,
* we can call repaint, likewise.
*/
x = me.getX();
y = me.getY();
repaint();
}
};
/*
* This JPanel panel is unnecessary in
* this case, since the class itself
* extends JPanel, hence you can use
* this (keyword) to access the instance
*/
//JPanel panel;
// Not needed for this case.
//JButton button2;
//JButton button;
public void game() {
JFrame frame = new JFrame("Game");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setResizable(false);
addMouseMotionListener(mouseActions);
/*
* Here this means the instance
* of the current class
*/
frame.add(this);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
/*
* While overriding methods of the
* super class, try to keep the
* ACCESS SPECIFIER, as close to
* the original thingy as possible
* In this case, it's protected
* and not public
*/
#Override
protected void paintComponent(Graphics g) {
/*
* Do not perform calculation in this method
* atleast.
*/
super.paintComponent(g);
g.fillRect(x, y, 100, 100);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Game().game();
}
};
EventQueue.invokeLater(runnable);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
}

Change this :
public void game() {
JPanel panel = new Game();
to this :
public void game() {
panel = new Game();
You are just creating a local variable in the first case. To fix this you need to instantiate the class variable.

Related

JLabel not displaying initially

I have made a simple class to practice layouts. most of it is working fine, but my JLabel is not appearing until after I click the button. I had the same info as a JTextField and JTextArea in earlier versions, but really prefer the appearance of the JLabel, but even in the other iterations, it would only appear if I clicked on or tried to select the text from the window. I've tried setting the text variable to visible after generating it, after adding it to the under panel, and setting the whole under panel to visible in addition to the setVisible(true) called for the whole object, and none of those worked.
Here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LayoutPractice extends JFrame implements ActionListener {
//Set up variables
private JPanel graphic;
private JPanel under;
private JButton button;
private JLabel text;
private int clicks;
/**
* Constructor, sets up GUI
*
*/
public LayoutPractice(){
//Default JFrame setup
super("Layout Practice");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the graphic panel
this.graphic = new JPanel();
graphic.setPreferredSize(new Dimension(500, 500));
//Set up the components that go under the graphic
this.clicks = 0;
this.button = new JButton("Click for dialog");
this.text = new JLabel("No Clicks");
//Set up the under panel, add in the button and label
this.under = new JPanel();
under.setLayout(new FlowLayout());
under.add(button);
under.add(text);
//Tell it to react to the button being pressed
button.addActionListener(this);
//Set the panels onto the JFrame
getContentPane().add(graphic, BorderLayout.CENTER);
getContentPane().add(under, BorderLayout.PAGE_END);
//Pack and set the JFrame to visible
pack();
setVisible(true);
}
/**
* Paints the image displayed on graphic
*
* #param A Graphics object to be worked on
*
*/
public void paint(Graphics g) {
//Assigns which panel to paint
graphic.paint(g);
//Set a color to pains
g.setColor(Color.BLUE);
//Use variables for a pattern
int x = 0;
int y = 0;
//Loop for a pattern
while (x <= 230) {
//Generates a filled rectangle of the correct size
g.fillRect(x, y, (500-(2 * x)), (500-(2 * y)));
//Alternates color
if (g.getColor() == Color.BLUE) {
g.setColor(Color.ORANGE.darker());
}
else {
g.setColor(Color.BLUE);
}
//Increase variables to reduce rectangle size
x += 20;
y += 20;
}
}
/**
* Tells the GUI what to do when the button is pressed
*
* #param An ActionEvent, specifically the buton being pressed
*/
public void actionPerformed(ActionEvent event) {
//Increase the clicks variable
clicks++;
//Change/update the JLabel
text.setText("Clicks: " + clicks);
//Open a dialog using available tools
JOptionPane.showMessageDialog(new JFrame(),
("Clicks: " + clicks),
"Click Count",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Very simple main, makes a new LayoutPractice
*
* #param args
*/
public static void main(String[] args) {
new LayoutPractice();
}
}
The quick fix is to just call super.paint(g) at the beginning of your overriden paint method, so that the frame ensures its clearing/cleaning/layout correctly.
The best fix takes the following into account :
When overriding a method, add the #Override annotation, so that your IDE will warn you if you incorrectly override .
When overriding a painting method , call its super implementation to ensure that things get correctly cleaned by the parent component.
For custom painting, better use a JComponent (usually a JPanel).
For custom painting, override paintComponent(Graphics) (and call super.paintComponent), don't use paint(Graphics).
You don't need to extend JFrame, just create one JFrame and use it.
In the below example, a custom JPanel class has been added for custom painting, and the application doesn't extend JFrame anymore :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class LayoutPractice implements ActionListener {
//Set up variables
private final JPanel graphic;
private final JPanel under;
private final JButton button;
private final JLabel text;
private int clicks;
/**
* Constructor, sets up GUI
*
*/
public LayoutPractice() {
//Default JFrame setup
JFrame frame = new JFrame("Layout Practice");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the graphic panel
graphic = new GraphicPanel();
graphic.setPreferredSize(new Dimension(500, 500));
//Set up the components that go under the graphic
clicks = 0;
button = new JButton("Click for dialog");
text = new JLabel("No Clicks");
//Set up the under panel, add in the button and label
under = new JPanel();
under.setLayout(new FlowLayout());
under.add(button);
under.add(text);
//Tell it to react to the button being pressed
button.addActionListener(this);
JPanel mainPanel = new JPanel(new BorderLayout());
//Set the panels onto the JFrame
mainPanel.add(graphic, BorderLayout.CENTER);
mainPanel.add(under, BorderLayout.PAGE_END);
frame.setContentPane(mainPanel);
//Pack and set the JFrame to visible
frame.pack();
frame.setVisible(true);
}
/**
* Tells the GUI what to do when the button is pressed
*
* #param An ActionEvent, specifically the buton being pressed
*/
public void actionPerformed(final ActionEvent event) {
//Increase the clicks variable
clicks++;
//Change/update the JLabel
text.setText("Clicks: " + clicks);
//Open a dialog using available tools
JOptionPane.showMessageDialog(new JFrame(),
("Clicks: " + clicks),
"Click Count",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Very simple main, makes a new LayoutPractice
*
* #param args
*/
public static void main(final String[] args) {
new LayoutPractice();
}
private class GraphicPanel extends JPanel {
#Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
//Set a color to pains
g.setColor(Color.BLUE);
//Use variables for a pattern
int x = 0;
int y = 0;
//Loop for a pattern
while (x <= 230) {
//Generates a filled rectangle of the correct size
g.fillRect(x, y, (500 - (2 * x)), (500 - (2 * y)));
//Alternates color
if (g.getColor() == Color.BLUE) {
g.setColor(Color.ORANGE.darker());
} else {
g.setColor(Color.BLUE);
}
//Increase variables to reduce rectangle size
x += 20;
y += 20;
}
}
}
}

Jcomponent paintcomponent not appearing on the panel

i am making a simple GUI in which small boxes should appear on the Jpanel according to their x,y coordinates. So i in my structure i have got three classes:
1: MyFrame which contains the main JFrame
2: MyPanel extends JPanel
3: Icon extends JComponent
In my MyFrame i want to have a MenuBar through which i can open a file of X,Y coordinates and below the menu bar i want to have the MyPanel which will have all the Icons for each X,Y coordinates. The first problem i have is that the Icon do not appear on MyPanel when i add the Icons.
My code can be seen below:
public class MyFrame {
private JFrame frame;
private MyPanel panel;
public MyFrame(){
panel = new MyPanel();
}
/*
*HERE GOES THE `FILE OPEN` LISTENER
* it creates `Coordinate` for each line
* passes the coordinate to panel.makeIcons()
*/
public void createGui(){
frame = new JFrame("Graph Editor");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(true);
//create, get and set the Jframe menu bar
//createMenuBar() returns a JMenuBar
frame.setJMenuBar(createMenuBar());
Container frame_pane = frame.getContentPane();
panel.setBounds(0, 0, frame.getWidth(), frame.getHeight());
frame_pane.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]){
MyFrame window = new MyFrame();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
window.createGui();
}
});
}
}
Code For the panel for holding icons:
public class MyPanel extends JPanel{
private Set<Icon> points;
public MyPanel(){
setLayout(null);
setPreferredSize(new Dimension(600, 600));
setBackground(Color.YELLOW);
}
//gets called by `FILE OPEN` listener for each coordinate
public void makeIcons(Coordinate obj){
Icon temp = new Icon(obj);
points.add(temp);
//add the temp JComponent to this JPanel
this.add(temp);
}
}
Code for Icon which needs to be shown on the above panel:
public Icon extends JComponent{
private Coordinate location;
public Icon(Coordinate obj){
location = obj;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(location.getX(), location.getY(), 20, 20);
g.setColor(Color.BLACK);
g.drawRect(location.getX(), location.getY(), 20, 20);
}
}
First Problem: The Icons do not show up in the panel with the above code.
Second Problem: When i change the makeIcon method in MyPanel.class to below. It shows the Icons By the MenuBar erases them when the MenuBar appears on any of the icons:
public void makeIcons(Coordinate obj){
Icon temp = new Icon(obj);
points.add(temp);
//add the temp JComponent to this JPanel
this.add(temp);
temp.paintComponent(this.getGraphics());
revalidate();
}
Any help is appreciated. Thanks
Don't call paintComponent (or any paint) method yourself, ever. This is not how painting works.
The core reasons why a component won't be painted are because:
it's size is 0x0
it's invisible
it's not added to a container that is (indirectly) added to a native peer.
Based on my brief observation, I would say you're suffering from point number 1, in part due to the use of a null layout
Also, remember, when painting a component, the component's Graphics context has already been translated so that 0x0 is the top/left corner of the component. This means, based on your code, you'd most likely be painting beyond the visible bounds of the component any way.
So, you basically have two choices. Implement your own layout manager, which uses the Coordinate information to place the Icons with in the container (which would then need to override getPreferredSize in order to provide sizing hints) or, paint it yourself
Which might look something like this...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Graph Editor");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.add(new MyPanel());
frame.pack();
frame.setVisible(true);
}
});
}
public class Coordinate {
private int x;
private int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Icon {
private Coordinate coordinate;
public Icon(Coordinate coordinate) {
this.coordinate = coordinate;
}
public Coordinate getCoordinate() {
return coordinate;
}
public void paint(Graphics2D g2d) {
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, 20, 20);
g2d.setColor(Color.BLACK);
g2d.drawRect(0, 0, 20, 20);
}
}
public class MyPanel extends JPanel {
private Set<Icon> points;
public MyPanel() {
points = new HashSet<>();
setLayout(null);
setPreferredSize(new Dimension(600, 600));
setBackground(Color.YELLOW);
}
//gets called by `FILE OPEN` listener for each coordinate
public void makeIcons(Coordinate obj) {
points.add(new Icon(obj));
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Icon icon : points) {
Graphics2D g2d = (Graphics2D) g.create();
Coordinate coordinate = icon.getCoordinate();
// I'd have a size component associated with the Icon
// which I'd then use to offset the context by half its
// value, so the icon is paint around the center of the point
g2d.translate(coordinate.getX() - 10, coordinate.getY() - 10);
icon.paint(g2d);
g2d.dispose();
}
}
}
}
I've not seeded any values, I'm lazy, but that's the basic idea

Why does the JPanel not display on the Frame?

i got the following Code, which is a shortened Version of the actual problem i have:
import javax.swing.*;
import java.awt.*;
public class Circle {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
Painter panel = new Painter();
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
import javax.swing.*;
import java.awt.*;
public class Painter extends JPanel{
/**
*
*/
private static final long serialVersionUID = 5663520834139683160L;
/**
* #param args
*/
Painter()
{
}
public void drawPlayer(Graphics g)
{
g.setColor(Color.GREEN);
g.fillOval(0, 0, 200, 30);
//g.setColor(Color.GREEN);
//g.drawRect(0, 0, 80, 50);
//g.setColor(Color.BLUE);
//g.fillRect(0, 0, 80/2, 50/2);
//g.setColor(Color.BLACK);
//g.drawString("BOB", 10/2+10, 5/2);
}
#Override
public void printComponent(Graphics g)
{
super.paintComponent(g);
this.drawPlayer(g);
}
}
When I execute it, the JPanel doesnt show up and I just can't find the mistake.
Don't know if this is important, but in the actual problem I initialize the JFrame with its JPanels in the constructor but it doesnt work either.
If necessary I can post the original code. The actual task is to provide a GUI for a ConnectFour game where the Frame is divided up into a JPanel drawing the discs and another JPanel providing player information like name etc. . The first part is working fine but the last part just won't work. (Don't get confuse by this code i posted drawing a circle. I wanted to post the structure of the solution i'm considering for the players information Panel.)
Why does the JPanel not display on the Frame?
It should be overridden paintComponent() method instead of printComponent() method
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.drawPlayer(g);
}

Java polygon resize

I am trying to make a program that has a window which displays a polygon and two buttons. The polygon starts at 3 points (a triangle) and allows the user to press a "+" and "-" button to add or subtract sides of the polygon. Here is my code:
In TestPolygonBox:
package testpolygonbox
import javax.swing.*;
import java.awt.*;
public class TestPolygonBox extends JFrame {
public TestPolygonBox(){
setLayout(new BorderLayout(5,5));
add(new PolygonBox ());
}
public static void main(String[] args) {
TestPolygonBox frame = new TestPolygonBox();
frame.setTitle("Polygon Box");
frame.setSize(400,420);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
next there is the control class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PolygonBox extends JPanel {
private JButton jbtEnlarge = new JButton("+");
private JButton jbtShrink = new JButton("-");
private PolygonPanel polygonPanel = new PolygonPanel();
public PolygonBox(){
JPanel panel = new JPanel();
panel.add(jbtEnlarge);
panel.add(jbtShrink);
setLayout(new BorderLayout());
this.add(polygonPanel,BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);
jbtEnlarge.addActionListener(new EnlargeListener());
jbtShrink.addActionListener(new ShrinkListener());
}
class EnlargeListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e){
polygonPanel.enlarge();
}
}
class ShrinkListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e){
polygonPanel.shrink();
}
}
}
class PolygonPanel extends JPanel{
private int polygonSides = 3;
/** Add side to the polygon*/
public void enlarge(){
polygonSides++;
repaint();
}
public void shrink(){
polygonSides--;
repaint();
}
#Override /** Draw requested Shape*/
protected void paintComponent(Graphics g){
int frameWidth = getWidth() / 2;
int frameHeight = getHeight() / 2;
int radius = (int)(Math.min(getWidth(),getHeight())* 0.4);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
g.setColor(Color.BLUE);
Polygon polygon = new Polygon();
polygon.addPoint(xCenter + radius, yCenter);
polygon.addPoint((int)(xCenter + radius * Math.cos(2 * Math.PI/polygonSides)),
(int)(yCenter - radius * Math.sin(2 * Math.PI / polygonSides)));
for (int polygonPoint = 2; polygonPoint <= polygonSides; polygonPoint++){
polygon.addPoint((int)(xCenter + radius * Math.cos(polygonPoint * 2 * Math.PI/polygonSides)),
(int)(yCenter - radius * Math.sin(polygonPoint * 2 * Math.PI / polygonSides)));
}
g.fillPolygon(polygon);
}
}
When i try to run this program i get the error:
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:483)
at java.awt.Container.addImpl(Container.java:1084)
at java.awt.Container.add(Container.java:998)
at javax.swing.JFrame.addImpl(JFrame.java:562)
at java.awt.Container.add(Container.java:410)
at testpolygonbox.TestPolygonBox.(TestPolygonBox.java:21)
at testpolygonbox.TestPolygonBox.main(TestPolygonBox.java:24)
Java Result: 1
if anyone could tell me where i am adding a window to a container please? Im not sure what im doing wrong.
PolygonBox is a JFrame window but you're adding it to another JFrame. Change the class so it extends JPanel instead.
public class PolygonBox extends JPanel {
There are 4 severe problems with your code.
You are not executing the Swing code in the Event dispatch thread (EDT). Your main has to look like this:
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
initializeGUI();
}
}
}
This is very important even the Swing Hello World - the simplest Swing program - does it. If you want to know why this has to be like this read this concurrency tutorial. In short, If you don't do this properly once your program gets bigger and more complex you'll see a lot of inconsistent, non-reproducible exceptions and bugs that occur in the Swing classes and you can't track down.
Second, as stated by Reimeus, you can't put a frame inside another frame, PolygonBox has to be a JPanel.
Third you are not populating the panels correctly. Look at this code:
JPanel panel = new JPanel();
panel.add(jbtEnlarge);
panel.add(jbtShrink);
This code looks weird, and it is. The question that pops up when you see it is: "Where are those buttons added?" Well in the panel of course, but where in the panel? That depends of the layout that the panel is using. And what is the layout of that panel? Well since you didn't explicitly specify one it is using the default layout for JPanels: the BorderLayout. With the border layout panel.add(component); is equivalent to panel.add(component, BorderLayout.CENTER);. However you can't put two components in the CENTER, you can put a panel with a lot of components or whatever, but you can't directly put two components, that is why that code doesn't work. Then how do you populate the panel? Pick a layout learn to use it and then add the components in a proper way. For example I'll do this with the GridLayout.
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(_enlargeButton);
panel.add(_shrinkButton);
With the Grid layout because I said that I want a grid 1x2, those adds are correct because the panel knows now that he has to put the components in each cell of the grid.
Lastly you will see that there are painting problems: the polygons are not erased, they are painted one over the other, adn you might see the image of the buttons in the panel... This is because you have to call super.paintComponent(g) in your paintComponent method. Thing is that code paints a polygon, yes, but you also have to do all the other work the component was doing for painting itself, so the first thing you have to do is call the original code you are overriding and then paint your component. For an extensive tutorial of why this works like this see Painting in AWT and Swing.
With all that, changing some variables names to follow the Java conventions and reformatting the code for a better organization. This is your fixed program:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestPolygonBox extends JFrame
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() { //Error 1: executing Swing code outside the EDT fixed
public void run()
{
TestPolygonBox frame = new TestPolygonBox();
frame.setVisible(true);
}
});
}
public TestPolygonBox()
{
super("Polygon Box"); //this is JFrame("title") since we are extending a frame
setLayout(new BorderLayout());//this line doesn't actually do nothing since the frame already usesk BorderLayout by default
add(buildPolygonBoxPanel());
setSize(400, 420);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private JPanel buildPolygonBoxPanel() //Error 2: PolygonBox has to be a panel, also I made it into a mthod returning the panel instead of a whole class since it was unnecessary.
{
JPanel polygonBox = new JPanel(new BorderLayout());
_polygonPanel = new PolygonPanel();
JPanel buttonsPanel = new JPanel(new GridLayout(1, 2)); //Error 4 adding the buttons properly to a panel.
_enlargeButton = new JButton("+1");
_enlargeButton.addActionListener(new EnlargeListener());
_shrinkButton = new JButton("-1");
_shrinkButton.addActionListener(new ShrinkListener());
buttonsPanel.add(_enlargeButton);
buttonsPanel.add(_shrinkButton);
polygonBox.add(_polygonPanel, BorderLayout.CENTER);
polygonBox.add(buttonsPanel, BorderLayout.SOUTH);
return polygonBox;
}
class EnlargeListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
_polygonPanel.enlarge();
}
}
class ShrinkListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
_polygonPanel.shrink();
}
}
private JButton _enlargeButton;
private JButton _shrinkButton;
private PolygonPanel _polygonPanel;
}
class PolygonPanel extends JPanel
{
private int _polygonSides = 3;
/** Add side to the polygon */
public void enlarge()
{
_polygonSides++;
repaint();
}
public void shrink()
{
if (_polygonSides > 3) _polygonSides--;
repaint();
}
#Override
/** Draw requested Shape*/
protected void paintComponent(Graphics g)
{
super.paintComponent(g); //Error 4 fixed, call super.paintComponent
int radius = (int) (Math.min(getWidth(), getHeight()) * 0.4);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
g.setColor(Color.BLUE);
Polygon polygon = new Polygon();
polygon.addPoint(xCenter + radius, yCenter);
polygon.addPoint((int) (xCenter + radius * Math.cos(2 * Math.PI / _polygonSides)), (int) (yCenter - radius * Math.sin(2 * Math.PI / _polygonSides)));
for (int polygonPoint = 2; polygonPoint <= _polygonSides; polygonPoint++)
{
polygon.addPoint((int) (xCenter + radius * Math.cos(polygonPoint * 2 * Math.PI / _polygonSides)), (int) (yCenter - radius * Math.sin(polygonPoint * 2 * Math.PI / _polygonSides)));
}
g.fillPolygon(polygon);
}
}
Just copy and execute it and it looks like this:

How to resize frame dynamically with Timer?

I'm trying to resize a window dynamically using a Timer object, but not succeeding... I set the preferred size of the panel in the constructor, which sets the size of the window nicely, though only once. The preferred size changes after the program is initialized, but the window size stays the same. Why? Because the constructor is initialized only once and therefore isn't affected by the size change? If so, how could I get around this to resize the window in real-time?
I know this won't solve the problem in the exercise given in the beginning comments, so please ignore that :-)
/*
* Exercise 18.15
*
* "(Enlarge and shrink an image) Write an applet that will display a sequence of
* image files in different sizes. Initially, the viewing area for this image has
* a width of 300 and a height of 300. Your program should continuously shrink the
* viewing area by 1 in width and 1 in height until it reaches a width of 50 and
* a height of 50. At that point, the viewing area should continuously enlarge by
* 1 in width and 1 in height until it reaches a width of 300 and a height of 300.
* The viewing area should shrink and enlarge (alternately) to create animation
* for the single image."
*
* Created: 2014.01.07
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ex_18_15 extends JApplet {
// Main method
public static void main(String[] args) {
JFrame frame = new JFrame();
Ex_18_15 applet = new Ex_18_15();
applet.isStandalone = true;
frame.add(applet);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// Data fields
private boolean isStandalone = false;
private Image image = new ImageIcon("greenguy.png").getImage();
private int xCoordinate = 360;
private int yCoordinate = 300;
private Timer timer = new Timer(20, new TimerListener());
private DrawPanel panel = new DrawPanel();
// Constructor
public Ex_18_15() {
panel.setPreferredSize(new Dimension(xCoordinate, yCoordinate));
add(panel);
timer.start();
}
class DrawPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if(yCoordinate <= 50) {
yCoordinate++;
xCoordinate++;
}
else if(yCoordinate >= 300) {
yCoordinate--;
xCoordinate--;
}
panel.setPreferredSize(new Dimension(xCoordinate, yCoordinate));
repaint();
}
}
}
You need to re-pack your JFrame to resize it. For instance at the end of your ActionListener:
Window win = SwingUtilities.getWindowAncestor(panel);
win.pack();
A question for you though: Why in heaven's name is your class extending JApplet and not JPanel? Or if it needs to be an applet, why are you stuffing it into a JFrame?
Edit
Regarding your comment:
Wouldn't it usually be extending JFrame not JPanel? I'm stuffing it into a JFrame to allow it to run as an application as well as an applet. That's how 'Introduction to Java Programming' tells me how to do it :p Adding your code at the end of the actionPerformed method didn't do anything for me ;o
Most of your GUI code should be geared towards creating JPanels, not JFrames or JApplets. You can then place your JPanels where needed and desired without difficulty. Your book has serious issues and should not be trusted if it is telling you this.
Edit 2
Works for me:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class ShrinkingGui extends JPanel {
private static final int INIT_W = 400;
private static final int INIT_H = INIT_W;
private static final int TIMER_DELAY = 20;
private int prefW = INIT_W;
private int prefH = INIT_H;
public ShrinkingGui() {
new Timer(TIMER_DELAY, new TimerListener()).start();;
}
public Dimension getPreferredSize() {
return new Dimension(prefW, prefH);
}
private class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (prefW > 0 && prefH > 0) {
prefW--;
prefH--;
Window win = SwingUtilities.getWindowAncestor(ShrinkingGui.this);
win.pack();
} else {
((Timer)e.getSource()).stop();
}
}
}
private static void createAndShowGUI() {
ShrinkingGui paintEg = new ShrinkingGui();
JFrame frame = new JFrame("Shrinking Gui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Categories

Resources