package
com.odon.graphicsjob;

import
java.awt.Color;
import
java.awt.Dimension;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.Graphics2D;
import
java.awt.font.FontRenderContext;
import
java.awt.geom.Rectangle2D;

import
javax.swing.JPanel;


class
BlankCanvas
extends
JPanel
...
{
Font font = new Font("SansSerif", Font.BOLD, 14);
FontRenderContext context = null;
private UIFrame uf = null;
String info = "Author : oDon";

public BlankCanvas(UIFrame _uf)...{
uf = _uf;
setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
}
public void paintComponent(Graphics g)

...{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
context = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(info, context);
g2.setColor(Color.ORANGE);
g2.setFont(font);
g2.drawString(info, BLANK, CANVAS_HEIGHT - (int) bounds.getHeight());
}
private static final int CANVAS_WIDTH = 1024;
private static final int CANVAS_HEIGHT = 40;
private static final int BLANK = 20;
}
最近甚是忙啊,大四临近毕业 各种事情缠身啊。作业还是要做的,图形学是个很有意思的科目。老师布置的图形学作业,要按时完成。
想到还有几次图形学作业要交,所以写了一个框架。
下面公布下源码,框架已经写好了,采用状态机编写,Main.java包含了控制Layout的Frame, GraphicsCanvas是画板,JobSwitch是作业内容的切换,JobController是作业内容的控制,InfoPanel是作业内容的教学资料,BlankCanvas提供了一点信息,GraphicsUtil是作业实现的函数。
此代码将会不定期update,如果你有兴趣可以随便更改,每次更新GraphicsUtil里的几个函数添加响应的JButton就可以了,如果添加了新的图形学内容请发一份给我 yuanonline@hotmail.com
package
com.odon.graphicsjob;

/** */
/**
* @author oDon
* @version 0.1
* @description Homework of <Basic of Computer Graphics>,include :
* <p>Bresenham Line</p>
* <p>Bresenham Line</p>
* etc.
* The source code will update following the homework
* ---------------------------------------------------------------------
* | BlankCanvas(NORTH) |
* |
* |JobSwitch(WEST) GraphicsCanvas(CENTER) JobController(EAST) |
* | |
* | InfoPanel(SOUTH) |
* ----------------------------------------------------------------------
* */


import
java.awt.BorderLayout;
import
java.awt.Color;
import
java.awt.Dimension;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.Graphics2D;
import
java.awt.Toolkit;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.awt.font.FontRenderContext;
import
java.awt.geom.Rectangle2D;

import
javax.swing.JButton;
import
javax.swing.JFrame;
import
javax.swing.JMenu;
import
javax.swing.JMenuBar;
import
javax.swing.JMenuItem;
import
javax.swing.JPanel;



public
class
Main
...
{


/** *//**
* @param args
*/

public static void main(String[] args) ...{
UIFrame frame = new UIFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}


class
UIFrame
extends
JFrame
...
{
private String title = "<Basic of Computer Graphics>";
private BlankCanvas bc = new BlankCanvas(this);
private GraphicsCanvas gc = new GraphicsCanvas(this);
private JobSwitch js = new JobSwitch(this);
private JobController jc = new JobController(this);
private InfoPanel infoPanel = new InfoPanel(this);
private JMenu sys;
private JMenu menu;
private JMenu about;
private JMenuBar jmb;
private JMenuItem[] oper;
private JMenuItem exit;
private JMenuItem aboutBMS;
private Thread infoThread = null;
private Thread canvasThread = null;
private int len = 6;
private short preState = 0;
private short curState = 0;

public UIFrame()...{
setTitle(title);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screenSize);
initMenu();
setJMenuBar(jmb);
add(bc, BorderLayout.NORTH);
add(gc, BorderLayout.CENTER);
add(js, BorderLayout.WEST);
add(jc, BorderLayout.EAST);
add(infoPanel, BorderLayout.SOUTH);
infoThread = new Thread(infoPanel);
infoThread.start();
canvasThread = new Thread(gc);
canvasThread.start();
}
private void initMenu()

...{
sys = new JMenu();
menu = new JMenu();
about = new JMenu();
jmb = new JMenuBar();
oper = new JMenuItem[len];
sys.setText("System");
menu.setText("Operation");
about.setText("About");
exit = new JMenuItem();
aboutBMS = new JMenuItem();
for(int i = 0; i < len; i++)

...{
oper[i] = new JMenuItem();
}
oper[0].setText("draw Bresenham Line");
oper[1].setText("");
oper[2].setText("");
oper[3].setText("");
oper[4].setText("");
oper[5].setText("");
exit.setText("Quit");
aboutBMS.setText("About GT");
for(int i = 0; i < len; i++)

...{
menu.add(oper[i]);
}
sys.add(exit);
about.add(aboutBMS);
jmb.add(sys);
jmb.add(menu);
jmb.add(about);
}
public boolean stateChanged()

...{
curState = getCurrentState();
if(curState != preState)

...{
preState = curState;
return true;
}
return false;
}

protected synchronized void setCurrentState(short currentState) ...{
curState = currentState;
}


protected synchronized short getCurrentState() ...{
return curState;
}

}
package
com.odon.graphicsjob;


import
java.awt.Color;
import
java.awt.Dimension;
import
java.awt.Font;
import
java.awt.Graphics;
import
java.awt.Point;
import
java.awt.event.MouseEvent;
import
java.awt.event.MouseListener;
import
java.awt.event.MouseMotionListener;

import
javax.swing.JPanel;


class
GraphicsCanvas
extends
JPanel
implements
Runnable, MouseMotionListener,

MouseListener
...
{

private UIFrame uf = null;
private GraphicsUtil gu = new GraphicsUtil();
Font font = new Font("SansSerif", Font.BOLD, 32);
private Point p = new Point();
private boolean clicked = false;
private int level = 20;
private int x = 0;
private int y = 0;
private int[] pointX = null;
private int[] pointY = null;
private int clickCount = 0;

private boolean drawBresen = false;

public GraphicsCanvas(UIFrame _uf)...{
uf = _uf;
pointX = new int[2];
pointY = new int[2];
setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paintComponent(Graphics g)

...{
super.paintComponent(g);
g.setColor(Color.ORANGE);
g.drawRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
g.setColor(Color.DARK_GRAY);
for(int i = level; i < CANVAS_WIDTH; i+= level)
g.drawLine(i, 0, i, CANVAS_HEIGHT);
for(int i = level; i < CANVAS_HEIGHT; i+= level)
g.drawLine(0, i, CANVAS_WIDTH, i);
g.setColor(Color.MAGENTA);
g.setFont(font);
g.drawString("X:" + x / level + " Y:" + y / level, 0, 25);
switch(uf.getCurrentState())

...{
case States.BRESENHAM:
g.drawString("Bresenham Line", CANVAS_WIDTH >> 1, 25);
if(drawBresen)

...{
g.setColor(Color.ORANGE);
gu.drawBresenhamLine(g, pointX[0], pointY[0], pointX[1], pointY[1]);
g.drawString("(" + pointX[0] + "," + pointY[0] + ")", pointX[0],
pointY[0]);
g.drawString("(" + pointX[1] + "," + pointY[1]+ ")", pointX[1],
pointY[1]);
}
break;
}
clicked = false;
}

private static final int CANVAS_WIDTH = 800;
private static final int CANVAS_HEIGHT = 540;

@Override

public void run() ...{
while(true)

...{
repaint();
try

...{
Thread.sleep(100);
}
catch (InterruptedException e)

...{
e.printStackTrace();
}
}
}

@Override

public void mouseClicked(MouseEvent e) ...{
switch(uf.getCurrentState())

...{
case States.BRESENHAM:

...{
pointX[clickCount%2] = x;
pointY[clickCount%2] = y;
clickCount++;
if((clickCount != 0) && (clickCount == 2))

...{
drawBresen = true;
clickCount = 0;
}
else
drawBresen = false;
}
break;
}
}

@Override

public void mouseEntered(MouseEvent e) ...{
}

@Override

public void mouseExited(MouseEvent e) ...{
}

@Override

public void mousePressed(MouseEvent e) ...{
}

@Override

public void mouseReleased(MouseEvent e) ...{
}

@Override

public void mouseDragged(MouseEvent e) ...{
}

@Override

public void mouseMoved(MouseEvent e) ...{
x = e.getPoint().x;
y = e.getPoint().y;
}
}


package
com.odon.graphicsjob;

import
java.awt.Graphics;


public
class
GraphicsUtil
...
{
public void drawPoint(Graphics g, int x, int y)

...{
g.drawLine(x, y, x, y);
}
public void drawBresenhamLine(Graphics g, int stX, int stY, int edX, int edY)

...{
int x = 0;
int y = 0;
int d = 0;
int dx =0;
int dy = 0;
int upIncre = 0;
int downIncre = 0;
if(stX > edX)

...{
stX = stX ^ edX;
edX = stX ^ edX;
stX = edX ^ stX;
stY = stY ^ edY;
edY = stY ^ edY;
stY = edY ^ stY;
}
x = stX;
y = stY;
dx = edX - stX;
dy = edY - stY;
d = dx - (dy << 1);
upIncre = (dx - dy) << 1;
downIncre= -(dy << 1);
while(x < edX)

...{
drawPoint(g, x, y);
x++;
if(d < 0)

...{
y++;
d += upIncre;
}
else
d += downIncre;
}
}
}
package
com.odon.graphicsjob;

import
java.awt.Color;
import
java.awt.Dimension;
import
java.awt.Font;
import
java.awt.Graphics;

import
javax.swing.JPanel;


class
InfoPanel
extends
JPanel
implements
Runnable

...
{
private UIFrame uf = null;
private int width = 0;
private int width2 = 0;
private int height = 0;
private int len = 280;
private boolean draw1 = true;
public InfoPanel(UIFrame _uf)

...{
uf = _uf;
this.setPreferredSize(new Dimension(1024, 100));
this.setBackground(Color.LIGHT_GRAY);
}

public void paintComponent(Graphics g)

...{
super.paintComponent(g);
g.setFont(new Font("宋体", 1, 24));
g.setColor(new Color(0x3f7fff));
if(draw1)

...{
g.drawString("添加讲解功能(未完善)", width, getHeight() >> 1);
}
width += 20;
if(width >= this.getWidth())

...{
width = -len;
}
}

@Override

public void run() ...{
while(true)

...{

try ...{
Thread.sleep(500);

} catch (InterruptedException e) ...{
e.printStackTrace();
}
this.repaint();
}
}
}
package
com.odon.graphicsjob;

import
java.awt.Color;
import
java.awt.Dimension;
import
java.awt.Graphics;

import
javax.swing.JButton;
import
javax.swing.JPanel;


class
JobController
extends
JPanel
...
{
private JButton refresh = new JButton("Refresh");
private UIFrame uf = null;

public JobController(UIFrame _uf)...{
uf = _uf;
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.LIGHT_GRAY);
add(refresh);
}
public void paintComponent(Graphics g)

...{
super.paintComponent(g);
g.drawRect(0, 0, WIDTH, HEIGHT);
}
private static final int WIDTH = 110;
private static final int HEIGHT = 600;
}
package
com.odon.graphicsjob;

import
java.awt.Color;
import
java.awt.Dimension;
import
java.awt.Graphics;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;

import
javax.swing.JButton;
import
javax.swing.JPanel;



class
JobSwitch
extends
JPanel
implements
ActionListener
...
{
private JButton bresen = new JButton("Bresenham");
private UIFrame uf = null;

public JobSwitch(UIFrame _uf)...{
uf = _uf;
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.LIGHT_GRAY);
add(bresen);
bresen.addActionListener(this);
}
public void paintComponent(Graphics g)

...{
super.paintComponent(g);
g.drawRect(0, 0, WIDTH, HEIGHT);
}
private static final int WIDTH = 110;
private static final int HEIGHT = 600;

@Override

public void actionPerformed(ActionEvent e) ...{
Object source = e.getSource();

if(source == bresen)...{
uf.setCurrentState(States.BRESENHAM);
}
}
}
package
com.odon.graphicsjob;


public
class
States
...
{
public static final short INIT = 0;
public static final short BRESENHAM = 1;
}