calling repaint on jpanel causes its children to shift - java

Im currently working on a game and i hava a jpanel that has a custom button and a jlabel, and when pressing the button the game starts.
i am trying to make that panel fade in with a timer animation by using paintComponent and paintChildren but when i call repaint the button and jlabel shift to the side:
this is the code for my jpanel:
public class PlayAgainScreen extends JPanel implements ActionListener {
private final int UNIT_SIZE;
private final Timer timer;
private int actPer = 0;
public PlayAgainScreen(SnakePanel snakePanel, boolean justLaunched) {
this.UNIT_SIZE = SnakePanel.UNIT_SIZE;
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
this.setPreferredSize(new Dimension(UNIT_SIZE * 20, UNIT_SIZE * 15));
this.setBackground(new Color(0, 0, 0, 0));
PlayButton playButton = new PlayButton(snakePanel, justLaunched);
Image image = new ImageIcon(Objects.requireNonNull(getClass().getClassLoader().getResource("snake_logo.png"))).getImage();
image = image.getScaledInstance((int) (this.getPreferredSize().height * 0.5), (int) (this.getPreferredSize().height * 0.5), Image.SCALE_SMOOTH);
JLabel label = new JLabel(new ImageIcon(image));
gbc.insets = new Insets(0, UNIT_SIZE, 0, 0);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(playButton, gbc);
gbc.gridx = 1;
this.add(label, gbc);
timer = new Timer(17, this);
timer.start();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(new Color(0x543200));
g2D.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), UNIT_SIZE, UNIT_SIZE);
}
#Override
protected void paintChildren(Graphics g) {
super.paintChildren(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(new Color(0, 0, 0, (int) (255 - actPer * 255 / 30F)));
g2D.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), UNIT_SIZE, UNIT_SIZE);
}
#Override
public void actionPerformed(ActionEvent e) {
actPer++;
repaint();
if (actPer == 30) {
timer.stop();
}
}
}

Related

Placing JButtons at exact locations and creating grid of JLabels?

public class SudukoGUI extends JPanel{
private static int WIDTH;
private static int HEIGHT;
private static int PANEL_HEIGHT;
public SudukoGUI(int width, int height, String title) {
this.WIDTH = width;
this.HEIGHT = height;
this.PANEL_HEIGHT = height - width;
SwingUtilities.invokeLater(() -> initGUI(WIDTH, HEIGHT, title));
}
public int getWidth() {
return WIDTH;
}
public int getHeight() {
return HEIGHT;
}
public void initGUI(int width, int height, String title) {
JFrame frame = new JFrame(title);
JButton b1 = new JButton("Solve");
JButton b2 = new JButton("Clear");
// JPanel bp = new JPanel();
// bp.setLayout(new BorderLayout());
// bp.setPreferredSize(new Dimension(WIDTH, PANEL_HEIGHT));
// frame.add(bp);
this.setPreferredSize(new Dimension(width, height));
this.setLayout(new BorderLayout());
this.add(b1, BorderLayout.SOUTH);
this.add(b2, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setSize(width + 16, height);
frame.setVisible(true);
frame.add(this);
//frame.pack();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(1.0f, 1.0f, 1.0f));
int cellWidth = WIDTH / Suduko.getNumRows();
int cellHeight = (HEIGHT - PANEL_HEIGHT) / Suduko.getNumCols();
int gridHeight = HEIGHT - PANEL_HEIGHT;
int regionWidth = 3, regionHeight = 3;
g2d.fillRect(0, 0, WIDTH, HEIGHT);
g2d.setColor(new Color(0,0,0));
for(int i = 0; i <= WIDTH; i += cellWidth) {
if((i / cellWidth) % regionWidth == 0) {
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(i, 0, i, gridHeight);
}else {
g2d.setStroke(new BasicStroke(1));
g2d.drawLine(i, 0, i, gridHeight);
}
}
for(int i = 0; i <= gridHeight; i += cellHeight) {
if((i / cellHeight) % regionHeight == 0) {
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(0, i, WIDTH, i);
}else {
g2d.setStroke(new BasicStroke(1));
g2d.drawLine(0, i, WIDTH, i);
}
}
}
}
As of now both buttons are at BorderLayout.SOUTH. I don't know how to place them at the bottom of this window and in smaller sizes. I tried creating a separate JPanel with setPreferredSize(new Dimension(WIDTH, PANEL_HEIGHT)); So it would be the blank area at the bottom, but that doesn't work of course, when changing the border layout the buttons take up the entire height. I want to place the buttons at the bottom in the blank area.
I also want to create JLabel components for the grid so I can enter numbers with KeyListener (and having MouseListener for selecting which cell). I'm not sure here how to proceed how to create labels in a grid format and how to set exact sizes to them etc. Any tips?
You can use a 3x3 grid of 3x3 grid layouts to create the effect of a Soduko board. The 'outer' grid layout can be assigned more space between each of the panels (each with its own grid layout with less space) for the effect.
Here is how that screenshot was created, adjust numbers (colors etc) to suit the exact requirement.
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.image.BufferedImage;
public class SudokuBoard {
public SudokuBoard() {
initComponents();
}
public final void initComponents() {
JPanel panel = new JPanel(new GridLayout(3,3,6,6));
panel.setBackground(Color.ORANGE);
panel.setBorder(new EmptyBorder(3,3,3,3));
int s = 20;
BufferedImage image = new BufferedImage(s,s,BufferedImage.TYPE_INT_RGB);
ImageIcon icon = new ImageIcon(image);
for (int ii=0; ii<9; ii++) {
JPanel p = new JPanel(new GridLayout(3,3,2,2));
p.setBackground(Color.CYAN);
for (int jj=0; jj<9; jj++) {
JLabel l = new JLabel(icon);
p.add(l);
}
panel.add(p);
}
JFrame f = new JFrame("Sudoku");
f.add(panel);
f.setResizable(false);
f.pack();
f.setLocationByPlatform(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new SudokuBoard();
}
};
SwingUtilities.invokeLater(r);
}
}

Java Rendering Artifacts When Adding Buttons To Semi-Transparent JPanel

I have a problem. I am trying to create "alert" component that I can display in a JFrame. This alert has a semi-transparent background (70% opacity, white) with any trivial amount of JButtons on it. The background itself has rounded corners, and thus I made a custom component.
When rendering this alert, artifacts appear: no alert-background is rendered on the button that is initially selected. When moving my mouse across the buttons, it looks like the text of the buttons is being drawn in two places for no reason.
Screenshots of the artifacts (top = initial render, bottom = moving mouse around):
Code that causes these problems:
public class Test {
public static void main(String[] args) {
MyRoundedTransparentBackground background = new MyRoundedTransparentBackground();
background.setSize(200, 200);
background.setLocation(100, 100);
background.setLayout(new GridBagLayout());
JPanel spacer = new JPanel();
spacer.setBackground(new Color(0, 0, 0, 0));
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 1;
c.gridheight = 1;
c.weighty = 1;
background.add(spacer, c);
JButton button1 = new JButton("Hi there");
JButton button2 = new JButton("Bye ...");
button1.setBackground(new Color(0, 0, 0, 0));
button1.setFocusPainted(false);
button1.setBorderPainted(false);
button2.setBackground(new Color(0, 0, 0, 0));
button2.setFocusPainted(false);
button2.setBorderPainted(false);
c.weighty = 0;
c.gridy = 1;
background.add(button1, c);
c.gridx = 1;
background.add(button2, c);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocation(2000, 100);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.red);
frame.add(background);
frame.setVisible(true);
}
private static class MyRoundedTransparentBackground extends JComponent {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(new Color(1f, 1f, 1f, 0.7f));
g2.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 15, 15);
}
}
Can someone help me find out what the issue is, and give a potential solution to achieve the desired behavior?

JButton query in Belisha Beacon Program

Basically my Belisha Beacon has to stay Orange when I click on the Steady button but in my Program when I click the Steady Button, the Beacon stays as Light Grey instead. Can someone please identify where I am going wrong please? Thank you much :). Here is my code:
package Homework;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BelishaBeacon {
private static Timer timer;
public class Drawing extends JPanel {
private int x = 125;
private int y = 80;
private boolean changeColors = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle box1 = new Rectangle(165, 180, 20, 45);
Rectangle box2 = new Rectangle(165, 225, 20, 45);
Rectangle box3 = new Rectangle(165, 270, 20, 45);
Rectangle box4 = new Rectangle(165, 315, 20, 45);
Rectangle box5 = new Rectangle(165, 360, 20, 45);
Rectangle box6 = new Rectangle(165, 405, 20, 45);
Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
g2.draw(ball);
g2.draw(box1);
g2.draw(box2);
g2.draw(box3);
g2.draw(box4);
g2.draw(box5);
g2.draw(box6);
g2.setColor(Color.BLACK);
g2.fill(box1);
g2.fill(box3);
g2.fill(box5);
g2.setColor(Color.ORANGE);
g2.fill(ball);
changeColors = !changeColors;
if (changeColors) {
g2.setColor(Color.lightGray);
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
}
}
public void changeColors() {
changeColors = true;
repaint();
}
}
public BelishaBeacon() {
JFrame frame = new JFrame();
frame.setSize(350, 570);
frame.setTitle("Belisha Beacon");
frame.setLayout(new BorderLayout(0, 0));
final Drawing shapes = new Drawing();
timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
shapes.repaint();
}
});
JButton jbtFlash = new JButton("Flash");
jbtFlash.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Flashing");
if (!timer.isRunning()) {
timer.start();
}
}
});
final JButton jbtSteady = new JButton("Steady");
jbtSteady.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();
}
});
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
controlPanel.add(jbtFlash);
controlPanel.add(jbtSteady);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.add(shapes);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new BelishaBeacon();
timer.start();
}
}
Instead of trying to toggle the color in paintComponent(), give Drawing a Color and use it to render the ball:
private Color color = Color.lightGray;
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
…
g2.setColor(color);
g2.fill(ball);
…
}
Make changeColors() actually change colors:
public void changeColors() {
if (Color.orange.equals(color)) {
color = Color.lightGray;
} else {
color = Color.orange;
}
repaint();
}
And add a makeSteady() method:
public void makeSteady() {
color = Color.orange;
repaint();
}
Now, your timer's action can just do shapes.changeColors(), your Flash button handler can just do timer.restart() and your Steady button handler can just do this:
timer.stop();
shapes.makeSteady();
Also, don't forget to invokeLater().
try this:
public class BelishaBeacon {
private static Timer timer;
public class Drawing extends JPanel {
private int x = 125;
private int y = 80;
private boolean changeColors = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle box1 = new Rectangle(165, 180, 20, 45);
Rectangle box2 = new Rectangle(165, 225, 20, 45);
Rectangle box3 = new Rectangle(165, 270, 20, 45);
Rectangle box4 = new Rectangle(165, 315, 20, 45);
Rectangle box5 = new Rectangle(165, 360, 20, 45);
Rectangle box6 = new Rectangle(165, 405, 20, 45);
Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
g2.draw(ball);
g2.draw(box1);
g2.draw(box2);
g2.draw(box3);
g2.draw(box4);
g2.draw(box5);
g2.draw(box6);
g2.setColor(Color.BLACK);
g2.fill(box1);
g2.fill(box3);
g2.fill(box5);
g2.setColor(Color.ORANGE);
g2.fill(ball);
// changeColors = !changeColors;
if (changeColors) {
g2.setColor(Color.lightGray);
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
}
}
public void changeColors() {
changeColors = !changeColors;
repaint();
}
public boolean getChangeColors() {
return changeColors;
}
}
public BelishaBeacon() {
JFrame frame = new JFrame();
frame.setSize(350, 570);
frame.setTitle("Belisha Beacon");
frame.setLayout(new BorderLayout(0, 0));
final Drawing shapes = new Drawing();
timer = new Timer(500, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//shapes.repaint();
shapes.changeColors();
}
});
JButton jbtFlash = new JButton("Flash");
jbtFlash.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Flashing");
if (!timer.isRunning()) {
timer.start();
}
}
});
final JButton jbtSteady = new JButton("Steady");
jbtSteady.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();
if(shapes.getChangeColors()) {
shapes.changeColors();
}
}
});
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
controlPanel.add(jbtFlash);
controlPanel.add(jbtSteady);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.add(shapes);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new BelishaBeacon();
timer.start();
}
}

What is wrong with my listener?

I have created the interface gui and added the buttons. But now I am stuck with the "Steady" button. When I click it I want to the circle "light bulb" to change color from yellow to orange.
What am I doing wrong in my code and when I press the "Steady" button nothing is happening?
/**
* Created by Metallion on 30/03/2015.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class BelishaBeacon {
public class Drawing extends JPanel {
private int x = 125;
private int y = 80;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//creating the shapes
Rectangle box1 = new Rectangle(165, 180, 20, 45);
Rectangle box2 = new Rectangle(165, 225, 20, 45);
Rectangle box3 = new Rectangle(165, 270, 20, 45);
Rectangle box4 = new Rectangle(165, 315, 20, 45);
Rectangle box5 = new Rectangle(165, 360, 20, 45);
Rectangle box6 = new Rectangle(165, 405, 20, 45);
//drawing the shapes
Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
g2.draw(ball);
g2.draw(box1);
g2.draw(box2);
g2.draw(box3);
g2.draw(box4);
g2.draw(box5);
g2.draw(box6);
//coloring the shapes
g2.setColor(Color.BLACK);
g2.fill(box1);
g2.fill(box3);
g2.fill(box5);
g2.setColor(Color.YELLOW);
g2.fill(ball);
}
}
public class changeColors extends JPanel {
private boolean choseColor = false;
private int x = 125;
private int y = 80;
public void changeColor(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.ORANGE);
if (!choseColor) {
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
}
}
public void changeColor() { choseColor = false; }
}
public BelishaBeacon() {
//Creation of frame
JFrame frame = new JFrame();
frame.setSize(350, 570);
frame.setTitle("Belisha Beacon");
frame.setLayout(new BorderLayout(0, 0));
final Drawing shapes = new Drawing();
final changeColors colorinG = new changeColors();
JButton jbtFlash = new JButton("Flash");
final JButton jbtSteady = new JButton("Steady");
jbtSteady.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
colorinG.changeColor();
colorinG.repaint();
}
});
//Positioning
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
controlPanel.add(jbtFlash);
controlPanel.add(jbtSteady);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.add(shapes);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new BelishaBeacon();
}
}
You need to add handling logic to your Drawing Swing component, e.g.:
add method to your Drawing to change the color
modify the paintComponent(Graphics) according to the first step
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class BelishaBeacon {
public class Drawing extends JPanel {
private int x = 125;
private int y = 80;
private boolean changeColors = false;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//creating the shapes
Rectangle box1 = new Rectangle(165, 180, 20, 45);
Rectangle box2 = new Rectangle(165, 225, 20, 45);
Rectangle box3 = new Rectangle(165, 270, 20, 45);
Rectangle box4 = new Rectangle(165, 315, 20, 45);
Rectangle box5 = new Rectangle(165, 360, 20, 45);
Rectangle box6 = new Rectangle(165, 405, 20, 45);
//drawing the shapes
Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100);
g2.draw(ball);
g2.draw(box1);
g2.draw(box2);
g2.draw(box3);
g2.draw(box4);
g2.draw(box5);
g2.draw(box6);
//coloring the shapes
g2.setColor(Color.BLACK);
g2.fill(box1);
g2.fill(box3);
g2.fill(box5);
g2.setColor(Color.YELLOW);
g2.fill(ball);
if (changeColors) {
g2.setColor(Color.ORANGE);
g2.fill(new Ellipse2D.Double(x, y, 100, 100));
}
changeColors = false;
}
public void changeColors() {
changeColors = true;
repaint();
}
}
public BelishaBeacon() {
//Creation of frame
JFrame frame = new JFrame();
frame.setSize(350, 570);
frame.setTitle("Belisha Beacon");
frame.setLayout(new BorderLayout(0, 0));
final Drawing shapes = new Drawing();
JButton jbtFlash = new JButton("Flash");
final JButton jbtSteady = new JButton("Steady");
jbtSteady.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
shapes.changeColors();
}
});
//Positioning
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout(1, 2, 0, 0));
controlPanel.add(jbtFlash);
controlPanel.add(jbtSteady);
frame.add(controlPanel, BorderLayout.SOUTH);
frame.add(shapes);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new BelishaBeacon();
}
}

2D Transformation (Translation, Rotation, Scaling) Program In Java

I have a problem with 2D transformation program
I have the source code
import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class House extends JPanel {
MyCanvas canvas;
JSlider sliderTransX, sliderTransY, sliderRotateTheta, sliderRotateX,
sliderRotateY, sliderScaleX, sliderScaleY, sliderWidth;
double transX = 0.0;
double transY = 0.0;
double rotateTheta = 0.0;
double rotateX = 150.0;
double rotateY = 150.0;
double scaleX = 1.0;
double scaleY = 1.0;
float width = 1.0f;
public House() {
super(new BorderLayout());
JPanel controlPanel = new JPanel(new GridLayout(3, 3));
add(controlPanel, BorderLayout.NORTH);
controlPanel.add(new JLabel("Translate(dx,dy): "));
sliderTransX = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150,
100, 50);
sliderTransY = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 300, 150,
100, 50);
// To control rotation
controlPanel.add(new JLabel("Rotate(Theta,ox,oy): "));
sliderRotateTheta = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 360,
0, 90, 45);
JPanel subPanel = new JPanel();
subPanel.setLayout(new GridLayout(1, 2));
sliderRotateX = setSlider(subPanel, JSlider.HORIZONTAL, 0, 300, 150,
150, 50);
sliderRotateY = setSlider(subPanel, JSlider.HORIZONTAL, 0, 300, 150,
150, 50);
controlPanel.add(subPanel);
// To control scaling
controlPanel.add(new JLabel("Scale(sx,sy)x10E-2:"));
sliderScaleX = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 200, 100,
100, 10);
sliderScaleY = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 200, 100,
100, 10);
// To control width of line segments
JLabel label4 = new JLabel("Width Control:", JLabel.RIGHT);
sliderWidth = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
sliderWidth.setPaintTicks(true);
sliderWidth.setMajorTickSpacing(5);
sliderWidth.setMinorTickSpacing(1);
sliderWidth.setPaintLabels(true);
sliderWidth.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
width = sliderWidth.getValue();
canvas.repaint();
}
});
JPanel widthPanel = new JPanel();
widthPanel.setLayout(new GridLayout(1, 2));
widthPanel.add(label4);
widthPanel.add(sliderWidth);
add(widthPanel, BorderLayout.SOUTH);
canvas = new MyCanvas();
add(canvas, "Center");
}
public JSlider setSlider(JPanel panel, int orientation, int minimumValue,
int maximumValue, int initValue, int majorTickSpacing,
int minorTickSpacing) {
JSlider slider = new JSlider(orientation, minimumValue, maximumValue,
initValue);
slider.setPaintTicks(true);
slider.setMajorTickSpacing(majorTickSpacing);
slider.setMinorTickSpacing(minorTickSpacing);
slider.setPaintLabels(true);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider tempSlider = (JSlider) e.getSource();
if (tempSlider.equals(sliderTransX)) {
transX = sliderTransX.getValue() - 150.0;
canvas.repaint();
} else if (tempSlider.equals(sliderTransY)) {
transY = sliderTransY.getValue() - 150.0;
canvas.repaint();
} else if (tempSlider.equals(sliderRotateTheta)) {
rotateTheta = sliderRotateTheta.getValue() * Math.PI / 180;
canvas.repaint();
} else if (tempSlider.equals(sliderRotateX)) {
rotateX = sliderRotateX.getValue();
canvas.repaint();
} else if (tempSlider.equals(sliderRotateY)) {
rotateY = sliderRotateY.getValue();
canvas.repaint();
} else if (tempSlider.equals(sliderScaleX)) {
if (sliderScaleX.getValue() != 0.0) {
scaleX = sliderScaleX.getValue() / 100.0;
canvas.repaint();
}
} else if (tempSlider.equals(sliderScaleY)) {
if (sliderScaleY.getValue() != 0.0) {
scaleY = sliderScaleY.getValue() / 100.0;
canvas.repaint();
}
}
}
});
panel.add(slider);
return slider;
}
class MyCanvas extends Canvas {
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.translate(transX, transY);
g2D.rotate(rotateTheta, rotateX, rotateY);
g2D.scale(scaleX, scaleY);
BasicStroke stroke = new BasicStroke(width);
g2D.setStroke(stroke);
drawHome(g2D);
}
public void drawHome(Graphics2D g2D) {
Line2D line1 = new Line2D.Float(100f, 200f, 200f, 200f);
Line2D line2 = new Line2D.Float(100f, 200f, 100f, 100f);
Line2D line3 = new Line2D.Float(100f, 100f, 200f, 100f);
Line2D line5 = new Line2D.Float(200f, 100f, 200f, 200f);
g2D.draw(line1);
g2D.draw(line2);
g2D.draw(line3);
g2D.draw(line5);
}
}
public static void main(String[] a) {
JFrame f = new JFrame();
f.getContentPane().add(new House());
f.setDefaultCloseOperation(1);
f.setSize(700, 550);
f.setVisible(true);
}
}
Please help me.
Don't mix AWT and Swing components needlessly: extend JPanel and override paintComponent(). A call to super.paintComponent(g) will clean up rendering, and RenderingHints will improve rotated drawing.
class MyCanvas extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2D.translate(transX, transY);
g2D.rotate(rotateTheta, rotateX, rotateY);
g2D.scale(scaleX, scaleY);
BasicStroke stroke = new BasicStroke(width);
g2D.setStroke(stroke);
drawHome(g2D);
}
...
}

Categories