📄 pictureplatform.java
字号:
/*
* Created on 2005-3-31
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package bubbletea;
import javax.imageio.ImageIO;
import javax.swing.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.*;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* @author roger
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class PicturePlatform extends JFrame {
//menus
//_______________________________________________
private JMenu[] menus = {
new JMenu("File "), new JMenu("Picture "),
new JMenu("Color "), new JMenu("Help ")
};
//Items
//___________________________________________________
private JMenuItem[] FileItems = {
new JMenuItem("New"), new JMenuItem("Open"),
new JMenuItem("Save"), new JMenuItem("Exit"),
};
private JMenuItem[] PictureItems = {
new JMenuItem("change red"), new JMenuItem("change green"),
new JMenuItem("change blue"), new JMenuItem("recover"),
new JMenuItem("zoom to 1/2"), new JMenuItem("zoom to 2"),
new JMenuItem("rotate right"), new JMenuItem("rotate left"),
new JMenuItem("extend X"), new JMenuItem("entend Y")
};
private JMenuItem ColorItem = new JMenuItem("Pen Color");
private JMenuItem paneColorItem = new JMenuItem("Pane Color");
private JMenuItem HelpItem = new JMenuItem("About");
//menubar
//__________________________________________________
private JMenuBar mb = new JMenuBar();
//optional buttons and button group
//__________________________________________________
private MyButton penButton = null;
private MyButton lineButton = null;
private MyButton eraButton = null;
private MyButton rectButton = null;
private MyButton circleButton = null;
private ButtonGroup bg = new ButtonGroup();
private ButtonGroup bg2 = new ButtonGroup();
private ButtonGroup bg3 = new ButtonGroup();
//stroke button
//__________________________________________________
private strokeButton wideButton = null;
private strokeButton midButton = null;
private strokeButton thinButton = null;
//cut button
//___________________________________________________
private MyButton cutToButton = null;
private JToggleButton cutterButton = null;
//text field
//__________________________________________________
private JTextField shape = new JTextField();
private JTextField stro = new JTextField();
private JTextField cut = new JTextField();
//status,main panel and another panel
//__________________________________________________
JLabel status = new JLabel();
MainPanel mp = null;
JPanel jp = new JPanel();
/**
* init
* @param void
*/
public PicturePlatform()
{
//create main panel
//_________________________________________________
status.setText("Roger's platform !");
mp = new MainPanel(status);
//add menuitems to menu
//_________________________________________________
FileItems[0].addActionListener(new newActionListener());
FileItems[1].addActionListener(new OpenL());
FileItems[2].addActionListener(new SaveL());
FileItems[3].addActionListener(new exitActionListener());
for(int i = 0; i < FileItems.length; i ++)
{
menus[0].add(FileItems[i]);
}
PictureItems[0].addActionListener(new catchRedActionListener());
PictureItems[1].addActionListener(new catchGreenActionListener());
PictureItems[2].addActionListener(new catchBlueActionListener());
PictureItems[3].addActionListener(new recoverActionListener());
PictureItems[4].addActionListener(new zoomHalfActionListener());
PictureItems[5].addActionListener(new zoomDoubleActionListener());
PictureItems[6].addActionListener(new changeRActionListener());
PictureItems[7].addActionListener(new changeLActionListener());
PictureItems[8].addActionListener(new extendXActionListener());
PictureItems[9].addActionListener(new extendYActionListener());
for(int i = 0; i < PictureItems.length; i ++)
{
menus[1].add(PictureItems[i]);
}
ColorItem.addActionListener(new colorActionListener());
menus[2].add(ColorItem);
paneColorItem.addActionListener(new paneColorActionListener());
menus[2].add(paneColorItem);
HelpItem.addActionListener(new messageActionListener());
menus[3].add(HelpItem);
//add menus to menubar
//_________________________________________________
for(int i = 0; i < menus.length; i ++)
{
menus[i].setBackground(Color.lightGray);
mb.add(menus[i]);
}
//initialize buttons
//__________________________________________________
penButton = new MyButton("Pen",MainPanel.PEN);
lineButton = new MyButton("Line",MainPanel.LINE);
eraButton = new MyButton("Eraser",MainPanel.ERASER);
rectButton = new MyButton("Rect",MainPanel.RECT);
circleButton = new MyButton("Circle",MainPanel.CIRCLE);
//__________________________________________________
wideButton = new strokeButton("wide",20,mp);
midButton = new strokeButton("mid",10,mp);
thinButton = new strokeButton("thin",1,mp);
//__________________________________________________
cutToButton = new MyButton("Cut choose",MainPanel.CUTTER);
cutterButton = new JToggleButton("Cut");
cutterButton.addActionListener(new cutActionListener());
//__________________________________________________
shape.setText(" Shapes:");
shape.setEditable(false);
shape.setBackground(Color.yellow);
stro.setText(" strokes:");
stro.setEditable(false);
stro.setBackground(Color.YELLOW);
cut.setText(" Cut:");
cut.setEditable(false);
cut.setBackground(Color.YELLOW);
//add buttons to button group and specified panel
//__________________________________________________
bg.add(penButton);
bg.add(lineButton);
bg.add(eraButton);
bg.add(rectButton);
bg.add(circleButton);
bg2.add(thinButton);
bg2.add(midButton);
bg2.add(wideButton);
bg3.add(cutToButton);
bg3.add(cutterButton);
jp.setLayout(new GridLayout(13,1));
jp.add(shape);
jp.add(penButton);
jp.add(lineButton);
jp.add(eraButton);
jp.add(rectButton);
jp.add(circleButton);
jp.add(stro);
jp.add(thinButton);
jp.add(midButton);
jp.add(wideButton);
jp.add(cut);
jp.add(cutToButton);
jp.add(cutterButton);
//add componets to content pane
//__________________________________________________________
Container cp = getContentPane();
cp.add(mp,BorderLayout.CENTER);
cp.add(status,BorderLayout.SOUTH);
cp.add(jp,BorderLayout.WEST);
setJMenuBar(mb);
}
class MyButton extends JToggleButton{
int toType;
public MyButton(String str,int toType)
{
super(str);
this.toType = toType;
this.addActionListener(new MyButtonListener());
}
class MyButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
mp.setType(toType);
}
}
}
class strokeButton extends JToggleButton{
float stroke;
public strokeButton(String str,float stroke,MainPanel mp)
{
super(str);
this.stroke = stroke;
this.addActionListener(new strokeActionListener());
}
class strokeActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
mp.setStroke(stroke);
}
}
}
class messageActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"<html><b><font size=+2>" +
"<center>Roger's<i> Progeram!",
"About",JOptionPane.DEFAULT_OPTION);
}
}
class colorActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Color color = Color.black;
color = JColorChooser.showDialog(PicturePlatform.this,
"Choose a color !",color);
mp.setColor(color.getRed(),color.getGreen(),color.getBlue());
}
}
class paneColorActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Color color = Color.black;
color = JColorChooser.showDialog(PicturePlatform.this,
"Choose a color !",color);
mp.setBackground(color);
mp.setPaneColor(color);
}
}
class exitActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -