I'm trying to create an applet that will produce as many ovals as the number specified within a textbox. The textbox appears, but upon hitting enter, my paintComponent does not draw. Thank you in advance.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.layout.*;
import net.miginfocom.swing.MigLayout;
import java.awt.geom.*;
public class OvalDrawer extends JApplet
{
private JLabel numberL;
private JTextField numberTF;
private NumHandler numHandler;
public static final int WIDTH = 500;
public static final int HEIGHT = 500;
//Create Layout
public void init()
{
setLayout(new MigLayout("wrap 2"));
numberL = new JLabel("Enter number of ovals to draw:");
numberTF = new JTextField(7);
add(numberL);
add(numberTF);
numHandler = new NumHandler();
numberTF.addActionListener(numHandler);
setSize(500, 500);
}
//Event Handler
public class NumHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
repaint();
}
}
//Draw Ovals
public void paintComponent (Graphics g)
{
super.paintComponents(g);
int number;
int x = 10;
int y = 30;
int width = 20;
int height = 10;
number = Integer.parseInt(numberTF.getText());
for (int i = 0; i < number; i++)
{
g.drawOval(x, y, width, height);
x += 5;
y += 5;
width += 5;
height += 5;
}
}
}
A JApplet class does not have a paintComponent method to override. Note that your compiler won't let you call the actual super method (you think you may be doing this, but you're actually calling super.paintComponents(...)
, a completely different method).
A bad solution is to override the JApplet's paint method, but I strongly advise you not to do this. Instead you should draw in the paintComponent method of a JPanel and then have the JApplet display that JPanel. Also, you'll want to get into the habit of using the @Override
annotation to be sure that you're actually overriding methods you think are.
/* * <Applet code=PressButton2 width=600 height=600> * </Applet>
*/
import javax.swing.*; import java.awt.*; import java.awt.event.*;
class MyPanel extends JPanel {
static String s="n";
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(s.equals("g"))
setBackground(Color.green);
if(s.equals("b"))
setBackground(Color.blue);
if(s.equals("c"))
setBackground(Color.white);
}
}
public class PressButton2 extends JApplet {
MyPanel panel;
MyPanel screen;
String s="n";
JButton green, clear, blue;
public void init() {
Container container = getContentPane();
panel = new MyPanel();
screen = new MyPanel();
panel.setLayout(new GridLayout(1, 3));
green = new JButton("Green");
blue = new JButton("Blue");
clear = new JButton("Clear");
green.addActionListener(new ActionEventHandler1());
blue.addActionListener(new ActionEventHandler1());
clear.addActionListener(new ActionEventHandler1());
panel.add(green);
panel.add(blue);
panel.add(clear);
container.add(panel, BorderLayout.SOUTH);
container.add(screen);
}
class ActionEventHandler1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String temp = e.getActionCommand();
if (temp.equals("Green")) {
MyPanel.s = "g";
screen.repaint();
}
if (temp.equals("Blue")) {
MyPanel.s = "b";
screen.repaint();
}
if (temp.equals("Clear")) {
MyPanel.s = "c";
screen.repaint();
}
}
}
来源:https://stackoverflow.com/questions/13707942/drawing-with-paintcomponent-in-applet-upon-event