早上好。我想编写一个程序,在JSlider
. 当ActPanel
和Contour
是 的内部类时Lab4
,他们至少在JFrame
. 但是通过半径和重新绘制存在一个巨大的问题。我没有使用内部类,但它没有给我更多ActPanel
,而且Contour
在屏幕上。
这是代码最重要的部分
class ActPanel extends JPanel implements ChangeListener {
public Contour cont;
public ActPanel(Contour contour) {
super(new FlowLayout());
cont = contour;
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 250, 50);
slider.addChangeListener(this);
JButton button = new JButton("display mark");
}
public void stateChanged(ChangeEvent e) {
if (e.getSource() instanceof JSlider) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
int R = (int)source.getValue();
cont.setR(R);
cont.repaint();
}
}
}
}
class Contour extends JPanel {
private static final int pointCount = 9;
public void paintComponent(Graphics gfx) {
super.paintComponent(gfx);
Graphics2D g = (Graphics2D) gfx;
GeneralPath fpolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, pointCount);
fpolygon.moveTo(Float.valueOf((float)R+250), Float.valueOf(250.0f));
fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf(250.0f));
//some drawing
}
private int R;
public void setR(int R) {
this.R = R;
}
public int getR() {
return R;
}
}
public class Lab4 {
private static void addComponentsToPane(Container pane) {
if (!(pane.getLayout() instanceof BorderLayout)) {
pane.add(new JLabel("Container doesn't use BorderLayout!"));
return;
}
Contour cont = new Contour();
ActPanel bottom = new ActPanel(cont);
pane.add(bottom, BorderLayout.PAGE_END);
cont.setPreferredSize(new Dimension(500,500));
pane.add(cont, BorderLayout.LINE_END);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Lab4");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String [] arg) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
这是一个完整版
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
class ActPanel extends JPanel implements ChangeListener {
public Contour cont;
public ActPanel(Contour contour) {
super(new FlowLayout());
cont = contour;
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 250, 50);
slider.addChangeListener(this);
JButton button = new JButton("display mark");
}
public void stateChanged(ChangeEvent e) {
if (e.getSource() instanceof JSlider) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
int R = (int)source.getValue();
cont.setR(R);
cont.repaint();
}
}
}
}
class Contour extends JPanel {
private static final int pointCount = 9;
public void paintComponent(Graphics gfx) {
super.paintComponent(gfx);
double kappa = 0.5522847498;
Graphics2D g = (Graphics2D) gfx;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GeneralPath fpolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, pointCount);
fpolygon.moveTo(Float.valueOf((float)R+250), Float.valueOf(250.0f));
fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf(250.0f));
fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf((float)250-R/2));
fpolygon.curveTo(
Float.valueOf((float)((-R/2)*kappa+250)),Float.valueOf((float)(-R/2)+250),
Float.valueOf((float)(-R/2)+250),Float.valueOf((float)((-R/2)*kappa)+250),
Float.valueOf((float)-R/2+250), Float.valueOf(250.0f));
fpolygon.lineTo(Float.valueOf((float)-R+250), Float.valueOf(250.0f));
fpolygon.lineTo(Float.valueOf((float)-R+250), Float.valueOf((float)R+250));
fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf((float)R+250));
fpolygon.lineTo(Float.valueOf(250.0f), Float.valueOf((float)R/2+250));
fpolygon.lineTo(Float.valueOf((float)R+250), Float.valueOf(250.0f));
fpolygon.closePath();
g.setPaint(Color.red);
g.fill(fpolygon);
g.setPaint(Color.black);
g.draw(fpolygon);
}
private int R;
public void setR(int R) {
this.R = R;
}
public int getR() {
return R;
}
}
public class Lab4 {
private static void addComponentsToPane(Container pane) {
if (!(pane.getLayout() instanceof BorderLayout)) {
pane.add(new JLabel("Container doesn't use BorderLayout!"));
return;
}
Contour cont = new Contour();
ActPanel bottom = new ActPanel(cont);
pane.add(bottom, BorderLayout.PAGE_END);
JList<Integer> xList = new JList<Integer>(new Integer[] {4,2,5,1,-5,-1,-4});
pane.add(xList, BorderLayout.LINE_START);
JPanel yPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
yPanel.add(new Label("x\\y"));
yPanel.add(new JCheckBox("5"));
yPanel.add(new JCheckBox("1"));
yPanel.add(new JCheckBox("-5"));
yPanel.add(new JCheckBox("2"));
yPanel.add(new JCheckBox("-5"));
yPanel.add(new JCheckBox("-1"));
yPanel.add(new JCheckBox("4"));
pane.add(yPanel, BorderLayout.PAGE_START);
cont.setPreferredSize(new Dimension(500,500));
pane.add(cont, BorderLayout.LINE_END);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Lab4");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String [] arg) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}