Witam
moze zaczne od kodu:
public class SwingTestingClass extends JFrame implements ActionListener
{
private JLabel l = null;
private JPanel panel1 = null;
private static BufferedImage image = null;
private static Graphics graphics = null;
public SwingTestingClass()
{
image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
graphics = image.getGraphics();
graphics.setColor(new Color(0));
graphics.drawRect(0, 0, 50, 50);
graphics.drawImage(image, 0, 0, null);
JFrame f = new JFrame ("hello");
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
Border b = new LineBorder(new Color(0),10);
Border bor1 = new LineBorder(new Color(150),5);
JButton b1 = new JButton ("b1");
JButton b2 = new JButton ("b2");
b1.setMnemonic('1');
b2.setMnemonic('2');
b1.addActionListener(this);
b2.addActionListener(this);
JPanel panel = new JPanel(new FlowLayout());
panel.add(b1);
panel.add(b2);
panel.setBorder(b);
f.getContentPane().add(panel);
panel1 = new JPanel(new BorderLayout());
panel1.setBackground(new Color(100,50,90));
panel1.setBorder(bor1);
panel1.setPreferredSize(new Dimension(100,100));
panel1.paint(graphics);
f.getContentPane().add(panel1);
f.setLayout(new FlowLayout());
f.pack();
f.setVisible(true);
}
public static void main(String[] args) throws InvalidMidiDataException
{
new SwingTestingClass();
}
public void actionPerformed(ActionEvent arg0)
{
String cmd = arg0.getActionCommand();
if(cmd.equals("b1")) panel1.paintAll(graphics);
if(cmd.equals("b2"))System.out.println("b2 has been pressed!");
}
public void paint(Graphics g)
{
g.drawRect(0, 0, 100, 100);
}
}
i mam pytanie jak zrobic zeby np narysowac kwadrat na panelu1? chodzi mi o wykorzystanie Graphicsa.
pozdrawiam!!!