gpt4 book ai didi

java - 在面板顶部添加面板并且两者都可见

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

我正在制作一个音乐/绘图应用程序,您可以用鼠标自由绘制线条,TrackBar 水平穿过它并读取线条并使用 x 和 y 坐标来修改音乐。

  • 我遇到的问题是 TrackBar 类是一个 JPanel,而 DrawBoard 类是一个 JPanel,我将其添加到 Frame 中。但哪个在最上面,哪个就会显示,我想要的是它们都显示,并且 TrackBar 在绘制的线条之上传递。

下面是这3个类的源码,去掉了一些不必要的代码:

我添加它们的 MainFrame 类(在当前设置下,TrackBar 不会出现,但绘图板会出现,因为它与它重叠):

public class MainFrame extends JFrame {

public static ColourToolbar colourBar;
public static TrackBar tb;

public MainFrame(){

super("VIPE by Prestige WorldWide");


// Top colour toolbar for tones
colourBar = new ColourToolbar();
this.getContentPane().add(colourBar, BorderLayout.NORTH);

// This class ImagePanel is a JPanel which I overite the paintCOmponent to have background
ImagePanel bg = new ImagePanel();
bg.setLayout(new BorderLayout());
Dimension size = getPreferredSize();
size.setSize(1024,800); //w, h
bg.setPreferredSize(size);
this.getContentPane().add(bg, BorderLayout.CENTER);



// I add the TrackBar to the ImagePanel since its the Background
tb = new TrackBar();
bg.add(tb, BorderLayout.CENTER);

// I add the drawing board but It will overlap the Trackbar
DrawBoard dboard = new DrawBoard();
bg.add(dboard, BorderLayout.CENTER);




// The control toolbar where the settings and control buttons are.
Toolbar toolBar = new Toolbar();
this.getContentPane().add(toolBar, BorderLayout.SOUTH);
}

public static void main(String[] args){

MainFrame frame = new MainFrame();

frame.setBackground(Color.WHITE);
frame.setSize(1024,768);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
//frame.pack();
frame.setVisible(true);

}


}

现在是 TrackBar 类:

public class TrackBar extends JPanel{

private TrackBarAction tba = new TrackBarAction(this);
public static int TIME = 9;

public Timer t = new Timer(TIME, tba);
public static double x = 0, y = 0, velX = 1.0, velY = 0;

private int SKIP = 120;


public TrackBar(){

this.setOpaque(false);
}

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);

Graphics2D g2d = (Graphics2D)g;
//g2d.drawRect((int)x, (int)y, 10, 800);

Rectangle2D r2d = new Rectangle2D.Double(x, y, 8.0, 800.0); // x,x, w, h
g2d.setPaint(Color.DARK_GRAY);
g2d.fill(r2d);
g2d.draw(r2d);
}

public void reset(){

t.stop();
x = 0;
y = 0;
velX = 0.5;
velY = 0;
this.repaint();
}

public void skipForward(){

if(x+SKIP <= 1024){
x += SKIP;
} else {
x = 1024 - x;
}

this.repaint();
}

public void skipBackwards(){

if(x-SKIP >= 0){
x -= SKIP;
} else {
x = 0;
}

this.repaint();
}

}

现在是 DrawBoard 类:

public class DrawBoard extends JPanel implements MouseListener, MouseMotionListener{


(..)

public Color currentColor;

public static boolean eraser = false;

private int xX1, yY1;

public DrawBoard(){



Graphics2D g2d = bImage.createGraphics();
g2d.dispose();

Dimension size = getPreferredSize();
size.setSize(1024,800); //w, h
setPreferredSize(size);
//status = new JLabel("default");
//add(status, BorderLayout.SOUTH);
addMouseListener(this);
addMouseMotionListener(this);


imgLabel = new JLabel(new ImageIcon(bImage)) {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
paintInLabel(g);
}
};
imgLabel.setOpaque(false);
setOpaque(false);
add(imgLabel, BorderLayout.CENTER);

}

private void paintInLabel(Graphics g) {

if(!eraser){
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(getColor()); // this colour is when mouse is pressed
g2d.setStroke(new BasicStroke(STROKESIZE));
if (points.size() < 1) {
return;
}
for (int i = 1; i < points.size(); i++) {
int x1 = points.get(i - 1).x;
int y1 = points.get(i - 1).y;
int x2 = points.get(i).x;
int y2 = points.get(i).y;
g2d.drawLine(x1, y1, x2, y2);
}
}

}


// Where the drawing happens
@Override
public void mousePressed(MouseEvent e) {
//status.setText("you pressed down the mouse");
if(!eraser){
xX1 = e.getX();
yY1 = e.getY();
points.add(e.getPoint());
}
}

@Override
public void mouseDragged(MouseEvent e) {
//status.setText("you draged the mouse");
Graphics2D g2d = bImage.createGraphics();

// this is the eraser code
if(eraser){
//Graphics2D g2d = bImage.createGraphics();
System.out.println("eraser = true");
System.out.println("Stroke = " + STROKESIZE);
g2d.setComposite(AlphaComposite.Clear);
g2d.setColor(new Color(0, 0, 0, 0));
g2d.drawRect(e.getX(), e.getY(), STROKESIZE, STROKESIZE);
g2d.fillRect(e.getX(), e.getY(), STROKESIZE, STROKESIZE);
// g2d.setColor(c);

imgLabel.repaint();
}else {
//g2d.setComposite();
points.add(e.getPoint());
System.out.println("point = " + e.getPoint());
imgLabel.repaint();
}

}

@Override
public void mouseReleased(MouseEvent e) {
//status.setText("you release the mouse click");
if(!eraser){
Graphics2D g2d = bImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(getColor()); // this is the final colour
g2d.setStroke(new BasicStroke(STROKESIZE));


if (points.size() >= 2) {
for (int i = 1; i < points.size(); i++) {
int x1 = points.get(i - 1).x;
int y1 = points.get(i - 1).y;
int x2 = points.get(i).x;
int y2 = points.get(i).y;
g2d.drawLine(x1, y1, x2, y2);
}
}
g2d.dispose();

points.clear();
imgLabel.repaint();
}
//imgLabel.repaint();

}
// End of where the drawing happens

public void clearDrawBoard() {

}

private Color getColor() {

return ColourToolbar.selectedColor;
}

private void setColor(Color col){

this.currentColor = col;
}

@Override
public void mouseClicked(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet.");
}

}

最佳答案

BorderLayout 只能有一个组件添加到每个组件的 5 个位置。通过将 DrawPanel 添加到中心位置,您可以有效地删除 TrackPanel。

相反,将 TrackBar 的布局管理器设置为 BorderLayout 并向其中添加 DrawPanel。

您也可以使用 JLayeredPane,但管理会变得更加复杂

关于java - 在面板顶部添加面板并且两者都可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15165189/

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