0

我正在尝试学习 Java,但我很难理解 OOP。我正在尝试创建一个简单的 java 绘画小程序。我目前有一个要绘制的背景图像、一个颜色选择器和一个宽度选择器。我正在尝试导入在http://www.javakode.com/applets/08-painting/上找到的 Brush1.java 示例

到目前为止,我现有的代码是:

package imagetest;

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ImageTest {




public static JList colorJList; // list to display colors
   private static final String[] colorNames = { "Black", "Blue", "Cyan",
  "Dark Gray", "Gray", "Green", "Light Gray", "Magenta",
  "Orange", "Pink", "Red", "White", "Yellow" };
   public static final Color[] colors = { Color.BLACK, Color.BLUE,
  Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, 
  Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, 
  Color.RED, Color.WHITE, Color.YELLOW };

   public static JList widthJList; // list to display colors
   private static final String[] widthNames = { "Thin", "Small", "Medium","Large" };
   public static final int[] widths = { 2,4,6,10 };




   public static ImagePanel panel;

  public static void main(String[] args) {


    panel = new ImagePanel(new ImageIcon("willy.jpg").getImage());
    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    colorJList = new JList( colorNames ); // create with colorNames
      colorJList.setVisibleRowCount( 5 ); // display five rows at once

      // do not allow multiple selections
      colorJList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

      // add a JScrollPane containing JList to frame
      frame.add( new JScrollPane( colorJList ) );
      colorJList.setSelectedIndex(0);

      colorJList.addListSelectionListener(
     new ListSelectionListener() // anonymous inner class
     {   
        // handle list selection events
        public void valueChanged( ListSelectionEvent event )
        {
           panel.repaint();
        } // end method valueChanged
     } // end anonymous inner class
  );

  widthJList = new JList( widthNames ); // create with colorNames
  widthJList.setVisibleRowCount( 4 ); // display five rows at once

  // do not allow multiple selections
  widthJList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

  // add a JScrollPane containing JList to frame
  frame.add( new JScrollPane( widthJList ) );
  widthJList.setSelectedIndex(0);

  widthJList.addListSelectionListener(
     new ListSelectionListener() // anonymous inner class
     {   
        // handle list selection events
        public void valueChanged( ListSelectionEvent event )
        {
           panel.repaint();
        } // end method valueChanged
     } // end anonymous inner class
  );

  frame.getContentPane().setBackground(Color.LIGHT_GRAY );
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
  }
  }

   class ImagePanel extends JPanel {


    private Image img;

  public Color color = Color.BLACK;
  public int linewidth = 2;

 public ImagePanel(String img) {
 this(new ImageIcon(img).getImage());
  }
  public ImagePanel(Image img) {
 this.img = img;
 Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
 setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
  }



   public void paintComponent(Graphics g) {
 g.drawImage(img, 0, 0, null);
  color = ImageTest.colors[ ImageTest.colorJList.getSelectedIndex()];
  linewidth = ImageTest.widths[ ImageTest.widthJList.getSelectedIndex()];
      g.setColor(color);
  for(int i = 0; i < 10; i++)
  {
   g.drawLine(10+i*10,10+i*10,50+i*10,50+i*10);

  }
   }
   }

我真的在为面向对象编程而苦苦挣扎。我完全不明白。它只是不适合我。我仍在寻找一种资源,将其分解为任何假人都可以学习的婴儿步骤。那么如何将刷子实现到我的练习小程序的其余部分?

4

1 回答 1

0

链接中显示的小程序是基于旧的AWT小程序而不是新的基于 Swing 的JApplet。您也可以使用Java Web Start来部署当前JFrame基于应用程序

于 2013-01-30T19:27:36.620 回答