gpt4 book ai didi

java - JApplet 绘制形状

转载 作者:行者123 更新时间:2023-12-01 05:51:33 26 4
gpt4 key购买 nike

    import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class ShapeApplet extends JApplet{
//contains all shapes that created
private ShapeContainer shapeContaiter = new ShapeContainer();

//main window panels
private Panel leftPanel = new Panel();
private Panel rightPanel = new Panel();
private Panel buttomPanel = new Panel();

//the panel inside right panel
private Panel shapesPanel = new Panel();

private String [] shapeNames = {"Cicle","Rect","Polygon","Line","Cylinder"};
private Choice shapeChoice = new Choice();


//defines the colors to choose from
JColorChooser colorChooser = new JColorChooser(Color.BLACK);

//item that will be use for painting
private Shape shapeToDraw;

//creating shapesButtons
ImageIcon iconCircle = new ImageIcon("circle.png", "Circle");
JButton buttonCircle = new JButton(iconCircle);

ImageIcon iconLine = new ImageIcon("line.png","Line");
JButton buttonLine = new JButton(iconLine);

ImageIcon iconRect = new ImageIcon("rect.png","Line");
JButton buttonRect = new JButton(iconRect);

ImageIcon iconPolygon = new ImageIcon("polygon.png","Line");
JButton buttonPolygon = new JButton(iconPolygon);

//creating the other tool buttons
JButton buttonClear = new JButton("Undo");

Checkbox checkBoxIsTheShapeFilled = new Checkbox("Filled");
//defines the new canvas
PaintingCanvas paintingCanvas = new PaintingCanvas();
//defines the class for buttons actions
ButtonAction buttonAction = new ButtonAction();

//save the shape that selected
private int currentTool = 4;
//integers to keep the mouse location
private int xPoint1, xPoint2 = 0, yPoint1,yPoint2 = 0;

@Override
public void init() {
System.out.println("points" + xPoint1+yPoint2);
//setting location , size and properties
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = (int) dim.getWidth();
int screenHeight = (int) dim.getHeight();
this.setSize(screenWidth,screenHeight-400);
//setting layout
this.setLayout(new BorderLayout());

//creating the choices for shapes
for(int i = 0 ; i < shapeNames.length ; i++ )
shapeChoice.add(shapeNames[i]);

//defines left Panel and right Panel properties
this.leftPanel.setBackground(Color.LIGHT_GRAY);
this.leftPanel.setLayout(new GridLayout(5, 0));

this.rightPanel.setBackground(Color.LIGHT_GRAY);
this.rightPanel.setLayout(new BorderLayout());

//adding items to the right panel
this.rightPanel.add(colorChooser, BorderLayout.NORTH);

//setting the shapes panel
this.shapesPanel.setLayout(new GridLayout(3,3));
this.shapesPanel.add(buttonCircle);
this.shapesPanel.add(buttonLine);
this.shapesPanel.add(buttonRect);
this.shapesPanel.add(buttonPolygon);
this.shapesPanel.add(checkBoxIsTheShapeFilled);

//adding the shapes panel to the right Panel
this.rightPanel.add(shapesPanel, BorderLayout.SOUTH);

//adding items to the left panel
this.leftPanel.add(buttonClear);

//defines the location of each item on the main window
this.add(leftPanel, BorderLayout.WEST);
this.add(rightPanel, BorderLayout.EAST);
this.add(buttomPanel,BorderLayout.SOUTH);
this.add(paintingCanvas,BorderLayout.CENTER);

//setting button actions
this.buttonCircle.addActionListener(buttonAction);
this.buttonLine.addActionListener(buttonAction);
this.buttonPolygon.addActionListener(buttonAction);
this.buttonRect.addActionListener(buttonAction);
this.buttonClear.addActionListener(buttonAction);


}

@Override
public void start() {

super.start();
}

@Override
public void stop() {

super.stop();
}

@Override
public void destroy() {

super.destroy();
}

public class PaintingCanvas extends Canvas implements MouseListener, MouseMotionListener
{
public PaintingCanvas()
{
addMouseListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
}

@Override
public void mouseEntered(MouseEvent e) {

}

@Override
public void mouseExited(MouseEvent e) {

}

@Override
public void mousePressed(MouseEvent e) {

xPoint1 = e.getX();
yPoint1 = e.getY();
xPoint2 = e.getX();
yPoint2 = e.getY();

}//end of mousePressed


@Override
public void mouseReleased(MouseEvent e) {

xPoint2 = e.getX();
yPoint2 = e.getY();



shapeContaiter.add(shapeToDraw);

System.out.println("Release");
repaint();

}


@Override
public void update(Graphics g) {
paint(g);
}//end of update

@Override
public void paint(Graphics g) {

switch (currentTool)
{
case 0:
//System.out.println("Circle pointsxy: " + xPoint1+","+yPoint1);
//System.out.println("Circle pointsx2y2: " + xPoint2+","+yPoint2);
shapeToDraw = new Circle(xPoint1 , yPoint1, new Point(xPoint2, yPoint2),colorChooser.getColor() , checkBoxIsTheShapeFilled.getState());
//System.out.println( "Circle pointsxy: " + shapeToDraw.getLocation());
shapeToDraw.draw(g);

break;
case 1:
shapeToDraw = new Line(xPoint1, yPoint1, new Point(xPoint2,yPoint2), colorChooser.getColor(), checkBoxIsTheShapeFilled.getState());
shapeToDraw.draw(g);

break;
case 2:

break;
case 3:
shapeToDraw = new Rect(xPoint1, yPoint1, new Point(xPoint2, yPoint2), colorChooser.getColor(), checkBoxIsTheShapeFilled.getState());
shapeToDraw.draw(g);

break;
case 4:
break;
default:
}//end of switch

System.out.println("Size of shapeContianer: "+shapeContaiter.size());
System.out.println("pointsxy: " + xPoint1+","+yPoint1);
System.out.println("pointsx2y2: " + xPoint2+","+yPoint2);


}//end of paint
@Override
public void mouseDragged(MouseEvent e) {


}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub

}

}//end of canvas

public class ButtonAction implements ActionListener
{

@Override
public void actionPerformed(ActionEvent e) {

if ( e.getSource() == buttonCircle)
currentTool = 0;
else if ( e.getSource() == buttonLine)
currentTool = 1;
else if ( e.getSource() == buttonPolygon)
currentTool = 2;
else if ( e.getSource() == buttonRect)
currentTool = 3;
else if (e.getSource() == buttonClear){
currentTool=4;
if(shapeContaiter.isEmpty()) JOptionPane.showMessageDialog(buttonClear, "Nothing to delete");
else {

Graphics g = paintingCanvas.getGraphics();

shapeContaiter.get(shapeContaiter.size()-1).setColor(Color.WHITE);
shapeContaiter.get(shapeContaiter.size()-1).draw(g);
shapeContaiter.remove(shapeContaiter.size()-1);

System.out.println("Size of shapeContianer: "+shapeContaiter.size());
}
}


}//end of action performed

}//end of ButtonClicked

}

我正在尝试创建一个撤消按钮,该按钮将从 shapeContainer 数组和 Canvas 中删除最后一个形状。我可以 Handlebars 指放在问题上,但是当我尝试删除其中一个形状时,它会删除​​我在当前形状之前绘制的形状。

我创建了 Circle、Rect、Polygon、Line 类,经过严格检查后似乎没问题。

最佳答案

  1. ShapeContainer 类是什么?相反,为什么不直接使用堆栈 http://download.oracle.com/javase/6/docs/api/java/util/Stack.html
  2. 为什么使用awt Panel和Canvas?我建议只使用 JPanel 和 JComponents。
  3. 您的 Button 监听器看起来非常可怕,其中包含所有 if 和 else 语句。相反,为每个按钮使用单独的监听器。这将简化逻辑,从而防止代码中出现错误并使更改变得更容易。 (在这种情况下,API 中的 Action 类可能对您有用)。

关于java - JApplet 绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4546613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com