I'm learning Swing GUI design. One thing I haven't quite sorted out is how to add an add a Canvas
to a specific location in a container.
More specifically: I create a Canvas
class that uses Paint
method. Object of this class is added to a Panel. What I do not quite understand, is how and where it is added to a Panel. In Tkinter Canvas
is a widget that contains only an image, but in Swing there is no similar widget (probably not the best word) added to the Frame that contains only Canvas object and nothing else.
Sorry if it is too vague, I'm adding a self-contained code. Please ignore text fields and labels.
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
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;
import javax.swing.JSeparator;
import javax.swing.JTextField;
//frame class
class frame_class2 extends JFrame implements ActionListener{
//declare buttons
JButton draw_button = new JButton("Draw");
JButton quit_button= new JButton("Quit");
JButton info_button = new JButton("Info");
//declare labels
JLabel x_loc = new JLabel("X:");
JLabel y_loc = new JLabel("Y:");
JLabel w_label= new JLabel("Width:");
JLabel h_label = new JLabel("Height:");
//Layout
FlowLayout layout_frame1 = new FlowLayout();
//Text boxes
JTextField x_loc_box = new JTextField("0");
JTextField y_loc_box = new JTextField("0");
JTextField w_loc_box = new JTextField("100");
JTextField h_loc_box = new JTextField("100");
//Info
JOptionPane info1 = new JOptionPane();
//Canvas
//Canvas area1 = new Canvas();
//Containers
JPanel panel1 = new JPanel();
JPanel panel2= new JPanel();
//Container container3 = new Container();
Container con = getContentPane();
public frame_class2(){
//panel1 = getContentPane();
//add(area1);
//add labels to the first panel
panel1.setLayout(layout_frame1);
panel2.setLayout(layout_frame1);
panel1.add(x_loc);
panel1.add(x_loc_box);
panel1.add(y_loc);
panel1.add(y_loc_box);
panel1.add(w_label);
panel1.add(w_loc_box);
panel1.add(h_label);
panel1.add(h_loc_box);
//add buttons to the second panel
draw_button.addActionListener(this);
quit_button.addActionListener(this);
info_button.addActionListener(this);
panel2.add(draw_button);
panel2.add(quit_button);
panel2.add(info_button);
con.add(panel1, BorderLayout.NORTH);
//con.add(new JSeparator(), BorderLayout.CENTER);
con.add(panel2, BorderLayout.SOUTH);
setDefaultCloseOperation(super.EXIT_ON_CLOSE);
setTitle("Graphics Toolbox v2");
//Set up the content pane.
//this.getContentPane();
pack();
//setSize(500, 500);
setLocationRelativeTo(null);
//setBackground(Color.BLUE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource()==info_button){
info1.showMessageDialog(this, "hahahahahaha");
}
else if (e.getSource()==quit_button){
System.exit(0);
}
else if (e.getSource()==draw_button){
graphics_class2 input1 = new graphics_class2();
con.add(input1);
//info1.showMessageDialog(this, "Not yet!");
}
}
}
//graphics class
class graphics_class2 extends Canvas{
public graphics_class2(){
//frame_class1 inst1 = new frame_class1();
//Canvas img1 = inst1.area1;
setSize(50,50);
//setBackground(Color.BLUE);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.GREEN);
g.fillArc(0, 0, 50, 50, 50, 50);
}
}
public class main_code {
public static void main(String args[]){
frame_class2 inst1 = new frame_class2();
}
}