import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PowerPaintGUI extends JPanel {
/**
* Created private so that doing the pack(); will be easier (I think).
*/
//private JFrame my_frame;
/**
* Panel that will be our entire drawing panel
*/
private JPanel my_paint_panel = new JPanel();
/**
* Constructor that creates the basic Frame.
* Might add it so it also creates the menu as well. I do not know yet.
*/
public PowerPaintGUI() {
JPanel panel = new JPanel();
setBackground(Color.WHITE);
setPreferredSize(new Dimension(400, 300));
}
/**
* Basic paint program it says.
*/
public void paintComponent(final Graphics the_graphics) {
super.paintComponent(the_graphics);
final Graphics2D g2d = (Graphics2D) the_graphics;
//put code here that uses g2d to paint things
Shape s = new Rectangle2D.Double(10, 10, 80, 80);
g2d.setPaint(Color.RED);
g2d.draw(s);
}
/**
*
* @param the_args just a input nothing more
*/
//Works when like this but never when panel is being created outa main.
//want to make it so I can add the panel to the frame... but not working right now
public static void main(final String[] the_args) {
final PowerPaintGUI panel = new PowerPaintGUI();
final JFrame frame = new JFrame("EllipsePanel Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
Second one which is how I want it, the basic Panel created in the constructor not in the main. Or if nothing else have it all added together in a start() method.
The bottom one does not automatically call the paintcomponenet.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PowerPaintGUI extends JPanel {
/**
* Created private so that doing the pack(); will be easier (I think).
*/
//private JFrame my_frame;
/**
* Panel that will be our entire drawing panel
*/
private JPanel my_paint_panel = new JPanel();
/**
* Constructor that creates the basic Frame.
* Might add it so it also creates the menu as well. I do not know yet.
*/
public PowerPaintGUI() {
my_paint_panel = new JPanel();
my_paint_panel.setBackground(Color.WHITE);
my_paint_panel.setPreferredSize(new Dimension(400, 300));
final JFrame frame = new JFrame("EllipsePanel Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(my_paint_panel);
frame.pack();
frame.setVisible(true);
}
/**
* Basic paint program it says.
*/
public void paintComponent(final Graphics the_graphics) {
super.paintComponent(the_graphics);
final Graphics2D g2d = (Graphics2D) the_graphics;
//put code here that uses g2d to paint things
Shape s = new Rectangle2D.Double(10, 10, 80, 80);
g2d.setPaint(Color.RED);
g2d.draw(s);
}
/**
*
* @param the_args just a input nothing more
*/
//Works when like this but never when panel is being created outa main.
//want to make it so I can add the panel to the frame... but not working right now
public static void main(final String[] the_args) {
final PowerPaintGUI panel = new PowerPaintGUI();
}
}