我有一个使用drawLine()绘制网格的applet,然后允许用户通过单击网格来放置和删除.png图像。现在我想在绘图空间的右侧添加一些界面按钮,我该怎么做?目前一个按钮拒绝移动到我想要的坐标,当然有正确的方法可以做到吗?
package test2;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
public class MainClass extends Applet implements MouseListener{
final int columnNumber = 10; //Number of columns
final int rowNumber = 8; //Number of rows
final int gridSize = 51;
private URL base;
//element images
private Image whiteSquare, resistanceHor, resistanceVer;
//creating a GridElement array
GridElement[][] gridElement = new GridElement[columnNumber][rowNumber];
public void init() {
setSize(columnNumber*gridSize + 100, rowNumber*gridSize);
//Here I try to add a button
Button resistanceButton = new Button("Button1");
resistanceButton.setLocation(columnNumber*gridSize, 20);
this.add(resistanceButton);
try {
base = getDocumentBase();
} catch (Exception e) {
// TODO: handle exception
}
resistanceHor = getImage(base, "data/resistance_hor.png");
resistanceVer = getImage(base, "data/resistance_ver.png");
whiteSquare = getImage(base, "data/white_square.png");
// Add the MouseListener to applet
addMouseListener(this);
for(int xcount=0; xcount<columnNumber;xcount++){
for(int ycount=0; ycount<rowNumber;ycount++){
//set top left corner coordinates for all grid elements and declare them empty and non-vertical
gridElement[xcount][ycount] = new GridElement(gridSize*xcount, gridSize*ycount,true,false);
}
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g){
//set grid color
Color gridGrey = new Color(208,195,195);
g.setColor(gridGrey);
//draw grid
for(int xcount=0; xcount<columnNumber;xcount++){
g.drawLine(xcount*gridSize, 0, xcount*gridSize, rowNumber*gridSize);
}
for(int ycount=0; ycount<rowNumber;ycount++){
g.drawLine(0, ycount*gridSize, columnNumber*gridSize, ycount*gridSize);
}
//draw png images in non-empty squares
for(int xcount=0; xcount<columnNumber;xcount++){
for(int ycount=0; ycount<rowNumber;ycount++){
if(gridElement[xcount][ycount].isEmpty() == false){
g.drawImage(resistanceHor, gridElement[xcount][ycount].getxTopLeftCorner(), gridElement[xcount][ycount].getyTopLeftCorner(), this);
}
else{
g.setColor( getBackground() );
g.fillRect(gridElement[xcount][ycount].getxTopLeftCorner()+1, gridElement[xcount][ycount].getyTopLeftCorner()+1, gridSize-1, gridSize-1);
}
}
}
}
public void mouseClicked (MouseEvent me) {
int col = me.getX() / gridSize; // Column where user clicked.
int row = me.getY() / gridSize; // Row where user clicked.
//if a square is empty, make it not empty, if it is not empty, make it empty
if(gridElement[col][row].isEmpty() == true){
gridElement[col][row].setEmpty(false);
}
else{
gridElement[col][row].setEmpty(true);
}
repaint();
}
public void mousePressed (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
public void mouseEntered (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
}
package test2;
public class GridElement {
private int xTopLeftCorner;
private int yTopLeftCorner;
private boolean vertical;
private boolean empty;
private boolean isResistor;
GridElement(int xTopLeftCorner, int yTopLeftCorner, boolean empty, boolean vertical){
this.xTopLeftCorner = xTopLeftCorner;
this.yTopLeftCorner = yTopLeftCorner;
this.empty = empty;
this.vertical = vertical;
}
public boolean isResistor() {
return isResistor;
}
public void setResistor(boolean isResistor) {
this.isResistor = isResistor;
}
public boolean isVertical() {
return vertical;
}
public boolean isEmpty() {
return empty;
}
public void setVertical(boolean vertical) {
this.vertical = vertical;
}
public void setEmpty(boolean empty) {
this.empty = empty;
}
public int getxTopLeftCorner() {
return xTopLeftCorner;
}
public int getyTopLeftCorner() {
return yTopLeftCorner;
}
public void setxTopLeftCorner(int xTopLeftCorner) {
this.xTopLeftCorner = xTopLeftCorner;
}
public void setyTopLeftCorner(int yTopLeftCorner) {
this.yTopLeftCorner = yTopLeftCorner;
}
}
答案 0 :(得分:0)
You need to learn about layout managers.
基本上是顶级容器,如Applet,JApplet,Frame,JFrame,Dialog,JDialog等。拥有BorderLayout的默认布局管理器。当您add(component)
到具有BorderLayout的容器时,该组件会被添加到BorderLayout.CENTER
位置。不仅如此,BorderLayout还不尊重组件的首选大小,这意味着组件将伸展以适应容器。在您的情况下,它将占用整个框架。
解。
不要在applet组件上绘制。换句话说是Panel
。将面板添加到applet容器中。 (中心还可以)
创建另一个Panel
并将按钮添加到那个面板,然后将面板添加到BorderLayout.LINE_END
,例如add(buttonPanel, BorderLayout.LINE_END);
其他重要说明:
除非这是一个课堂作业,教授明确表示要使用AWT组件。不要。请改用Swing组件。 Swing组件与AWT组件非常相似。它们通常在AWT组件前加J
作为前缀,例如JPanel
。请参阅more about Swing
请勿像paint()
方法那样明确调用update()
。而是致电repaint()
如果您不打算自己绘制背景,则应在super.paint(g)
方法中调用paint
。如果你不这样做,你会看到剩余的油漆文物。