我已经创建了一个矩形,现在我必须将一个JLabel放入其中.那我怎么能把JLabel放在矩形里面呢.
代码在这里: -
public class ColoredRect extends JPanel
{
private double x, y, width, height;
public ColoredRect(double x,double y)
{
this.x = x;
this.y = y;
width = 100;
height =40;
rect = new Rectangle2D.Double(this.x , this.y,width,height);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.cyan);
g2.fill(rect);
}
}
Run Code Online (Sandbox Code Playgroud)
请给我一些实现这个的想法.
提前致谢.
我有一个任务,我应该在正方形中绘制正方形,最后得到这样的东西:

我一直在尝试不同的方法来解决这个问题,但由于我有限的编程知识,我似乎无法想出一个简单的方法来做到这一点.我想做的是让Polygon画出这样的正方形:
public void paintComponent(Graphics g){
super.paintComponent(g);
setBackground(Color.DARK_GRAY);
int x1 = 50;
int y1 = 50;
int x2 = 250;
int y2 = 50;
int x3 = 250;
int y3 = 250;
int x4 = 50;
int y4 = 250;
int xPoints[] = {x1,x2,x3,x4};
int yPoints[] = {y1,y2,y3,y4};
int nPoints = 4;
g.setColor(Color.GREEN);
g.drawPolygon(xPoints, yPoints, nPoints);
}
Run Code Online (Sandbox Code Playgroud)
然后只需将几个像素添加到正确的点坐标即可移动方块.问题是我无法找到沿着线的y坐标,使得方形开始越来越向中心开始(对不起,这可能是一个可怕的解释).无论如何,我现在陷入困境,如果有人有更好的方法来解决这个问题(或者可以指出我在正确的方向上)那将会很棒.
谢谢!
我正在尝试创建一个按钮面板,其中单击的按钮变为"不同颜色"; 即显示背景图像.ps我只需要这种方法(有2张图片),而不是其他任何东西.谢谢 !
例如:
public class TestPane extends JPanel {
private BufferedImage imgUnclicked;
private BufferedImage imgClicked;
private Point mousePoint;
public TestPane() {
try {
imgUnclicked = ImageIO.read(new File("C:\\Users\\Me\\Desktop\\tmp\\Uncolored.png"));
imgClicked = ImageIO.read(new File("C:\\Users\\Me\\Desktop\\tmp\\Colored.png"));
} catch (IOException ex) {
Logger.getLogger(Spotlight.class.getName()).log(Level.SEVERE, null, ex);
}
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
mousePoint = e.getPoint();
repaint();
}
});
}
}
@Override
protected void paintComponent(Graphics g) {
//Draw imgClicked
//Draw imgUnclicked with some rectangular area around mouse click subtracted
}
}
Run Code Online (Sandbox Code Playgroud) 我是Java GUI的新手,我试图让这个程序在点击按钮时显示一个方块.没有任何反应,因为repaint()不适用于paintComponent(Graphics g).我搜索过,有些人说使用事件调度线程,但我仍然很困惑
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class Ham extends JFrame implements ActionListener
{
JPanel p1;
JButton b1;
public Ham(){
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1 = new JPanel();
b1 = new JButton("Check");
b1.addActionListener(this);
p1.add(b1);
add(p1, BorderLayout.NORTH);
setVisible(true);
}
public void actionPerformed (ActionEvent e){
if(e.getSource() == b1){
repaint();
}
}
public void paintComponent(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(100,100,50,50);
}
}
Run Code Online (Sandbox Code Playgroud) 所以我有这个小项目让我跳马里奥.但我无法弄清楚如何重新绘制它.如果我在点击后在Main类中执行它,那么整个跳跃将非常不稳定.
我尝试在跳转功能结束时执行此操作,但这也不起作用.
这是我的代码:
主要:
package klassid;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Main extends JComponent implements KeyListener, ActionListener{
static Hero hero;
Timer t = new Timer(500,this);
public static void main(String[] args) {
JFrame aken = new JFrame("Simple jumping simulator");
aken.setSize(600, 600);
aken.getContentPane().setBackground(new Color(255,255,255));
aken.getContentPane().add(new Main());
aken.setVisible(true);
aken.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hero.the_jump();
}
public Main(){
addKeyListener(this);
setFocusable(true);
t.start();
hero = new Hero(0, 320);
}
public void paintComponent(Graphics g){
hero.render(g, this); …Run Code Online (Sandbox Code Playgroud) 有点奇怪的问题.
我有一个"Circles"的ArrayList,我已经在我的绘图板中绘制了但是在它们被绘制之前,它们通过一种方法进行验证,该方法检查它们是否在面板内,这意味着在面板的范围内100.传递此"drawn()"方法的Circles数量由我的circlesDrawn整数变量计算,然后在方法中稍后打印在控制台中.
我在paintComponent方法中打印数据是为了正确同步,因为我在我的驱动程序main方法中遇到了问题.但是,当我的程序运行时,此数据被打印三次,circleDrawn变量的第二次和第三次分别是值的两倍和三倍.
有没有办法防止这种情况发生或任何有用的指针任何人都可以给我如何纠正这个?
我只附加了paintComponent方法,以避免将整个项目转储到这里,但如果需要更多上下文,我可以轻松提供它.
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
for (int i = 0; i < circles.size(); ++i)
{
if (circles.get(i).drawn(DEFAULT_WIDTH, DEFAULT_HEIGHT) == true)
{
circles.get(i).draw(g);
circlesDrawn++;
}
}
System.out.println("Number of circles drawn: " + circlesDrawn);
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助.
我正致力于创造一个有趣的游戏,基本上是一个简单的演化表示.
基本上,当我点击我移动的球时,它会改变颜色.目标是不断改变,直到它与背景颜色相匹配,这意味着球被成功隐藏.最终我会添加更多的球,但我试图弄清楚如何通过鼠标点击改变它的颜色.到目前为止,我已经创建了移动球动画.
当我点击球时如何改变球的颜色?
码:
public class EvolutionColor
{
public static void main( String args[] )
{
JFrame frame = new JFrame( "Bouncing Ball" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
BallPanel bp = new BallPanel();
frame.add( bp );
frame.setSize( 1800, 1100 ); // set frame size
frame.setVisible( true ); // display frame
bp.setBackground(Color.YELLOW);
} // end main
}
class BallPanel extends JPanel implements ActionListener
{
private int delay = 10;
protected Timer timer;
private int x = 0; // x position
private int …Run Code Online (Sandbox Code Playgroud) 我的问题是,当我按下一个按钮时,应该调用paintComponent然后应该在JPanel上绘制一个图形,不幸的是paintComponent在加载程序时绘制图形,在这种情况下按钮是无用的.
我制作了我的程序的一个小版本,以便轻松快速地阅读和检测问题.这里的代码不是原始代码,但它表明了同样的问题.
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class TestPaint extends JPanel implements ActionListener {
private JButton button_1 = new JButton( "Draw Oval" );
public TestPaint() {
add(button_1);
}
@Override
public void actionPerformed(ActionEvent e) {
if ( e.getSource() == button_1 )
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(10, 10, 100, 100);
}
}
Run Code Online (Sandbox Code Playgroud)
运行程序
import javax.swing.JFrame;
public class RunPaint {
public static void main(String[] args) {
TestPaint paint_g = new TestPaint();
JFrame …Run Code Online (Sandbox Code Playgroud) 我目前正在制作一个带GUI的基本连接四游戏,我正在试图弄清楚如何将棋子放在棋盘上.我有一个方法可以找到用户想要放置哪个空间,但我不确定如何绘制它们.因为paintComponent()总是在没有实际方法调用的情况下调用,而默认构造函数只接受Graphics对象,我怎么能重载,paintComponent()以便它可以采取不同的参数(这些参数是该片段的位置),编译器将知道调用我的新paintComponent()方法?
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawLine(-100,0,500,0);
g2.drawLine(141,-500,141,500);
g2.translate(getWidth()/2.0, getHeight()/2.0);
g2.scale(1,-1);
g2.rotate(45*Math.PI/180);
Rectangle2D r = new Rectangle2D.Double(0,0,100,100);
g2.fill(r);
Run Code Online (Sandbox Code Playgroud)