ubuntu java swing jtextfield caret invisible since java8 (setXORMode bug ?) - java

Why does caret becomes invisible (after key pressed, or left-arrow) under java8 (oracle and openjdk) and higher ? (it works under java7). I am on ubuntu. It works fine on mac-os jdk1.8.0_51 and windows jdk1.8.0_65.
Seems to be linked to this bug. It is spécific to JTextField (does not occur on JTextArea).
from OverwritableTextField
public class OverWriteCaret extends DefaultCaret {
protected static final int MIN_WIDTH = 8;
private static final Logger logger = Logger.getLogger(OverWriteCaret.class.getName());
public static void main(String[] args) {
JFrame f = new JFrame("Big caret");
JTextField tf = new JTextField(20);
tf.setCaret(new OverWriteCaret());
f.getContentPane().add(tf, "North");
f.pack();
f.setVisible(true);
}
#Override
protected synchronized void damage(Rectangle r) {
if (r == null)
return;
try {
JTextComponent comp = getComponent();
TextUI mapper = comp.getUI();
Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
int largeur = r2.x - r.x;
if (largeur == 0)
largeur = MIN_WIDTH;
comp.repaint(r.x, r.y, largeur, r.height);
this.x = r.x;
this.y = r.y;
this.width = largeur;
this.height = r.height;
} catch (Exception e) {
logger.info(e);
}
}
#Override
public void paint(Graphics g) {
if (isVisible())
try {
JTextComponent comp = getComponent();
TextUI mapper = comp.getUI();
Rectangle r1 = mapper.modelToView(comp, getDot());
Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
g = g.create();
g.setColor(comp.getForeground());
g.setXORMode(comp.getBackground());
int largeur = r2.x - r1.x;
if (largeur == 0)
largeur = MIN_WIDTH;
g.fillRect(r1.x, r1.y, largeur, r1.height);
g.dispose();
} catch (Exception e) {
logger.info(e);
}
}
}

found another caret, ok with it
public class FancyCaret extends DefaultCaret {
private static final Logger logger = Logger.getLogger(FancyCaret.class.getName());
protected synchronized void damage(Rectangle r) {
if (r == null)
return;
x = r.x;
y = r.y;
height = r.height;
if (width <= 0)
width = getComponent().getWidth();
repaint(); // calls getComponent().repaint(x, y, width, height)
}
#Override
public void paint(Graphics g) {
JTextComponent comp = getComponent();
if (comp == null)
return;
int dot = getDot();
Rectangle r;
char dotChar;
try {
r = comp.modelToView(dot);
if (r == null)
return;
dotChar = comp.getText(dot, 1).charAt(0);
} catch (Exception e) {
logger.info(e);
return;
}
if ((x != r.x) || (y != r.y)) {
repaint(); // erase previous location of caret
x = r.x; // Update dimensions (width gets set later in this method)
y = r.y;
height = r.height;
}
if (dotChar == '\n') {
width = r.height / 2;
if (isVisible())
g.fillRect(r.x, r.y, width, r.height);
return;
}
g.setColor(comp.getCaretColor());
g.setXORMode(comp.getBackground()); // do this to draw in XOR mode
width = g.getFontMetrics().charWidth(dotChar);
if (isVisible())
g.fillRect(r.x, r.y, width, r.height);
}
}

Related

How to create a looped world map? (Java-Swing)

So, I've started to make a grand strategy game in Java using Swing and I want to create a world map which looped. (So if you reach the west end of the worldmap, the system will start drawing the east side and vice verse, like in HOI4 or EU4.)
I have no idea how to do that.
I tried to create 3 map and if you reach the end the system drop back to the middle, but this method ate my computer.
Or if it's easier to understand, I want to create a cylinder, and draw a part from its wall.
(I'm planning to switch to libgdx, especially if there I can make this much easier.)
Here's my WorldMap class:
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import me.fiveship.waw.objects.Area;
import me.fiveship.waw.objects.Point;
public class WorldMap extends JComponent {
private static final long serialVersionUID = -4823224592445587979L;
public static int WIDTH = 1280;
public static int HEIGHT = 768;
public WorldMap() {
setBounds(0, 0, WIDTH, HEIGHT);
}
public Point location = new Point(0, 0);
public double zoomLevel = 3;
protected java.awt.Point p;
private static boolean settedUp = false;
private static BufferedImage areaMap = null;
private static BufferedImage countryMap = null;
private static BufferedImage regionMap = null;
public static void createPreMaps() {
Point max = Area.max();
areaMap = new BufferedImage(max.X, max.Y, BufferedImage.TYPE_INT_ARGB);
countryMap = new BufferedImage(max.X, max.Y, BufferedImage.TYPE_INT_ARGB);
regionMap = new BufferedImage(max.X, max.Y, BufferedImage.TYPE_INT_ARGB);
// AREA MAP
Graphics g = areaMap.createGraphics();
for (Area area : Area.areas()) {
g.setColor(area.color());
for (Point p : area.points) {
g.fillRect(p.X, p.Y, 1, 1);
}
g.setColor(area.color().darker());
/*
* for (Border b : area.borders) { g.fillRect(b.p.X, b.p.Y, 1, 1); }
*/
}
// COUNTRY MAP
// g = countryMap.createGraphics();
// REGION MAP
// g = regionMap.createGraphics();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (!settedUp) {
settedUp = true;
createPreMaps();
}
Rectangle r = new Rectangle((int) (location.X * zoomLevel), (int) (location.Y * zoomLevel),
(int) (areaMap.getWidth() * zoomLevel), (int) (areaMap.getHeight() * zoomLevel));
g.drawImage(areaMap, r.x, r.y, r.width, r.height, null);
}
}
Okay, so I found out someting. When I had tried the "three map" method, the problem had been that I had wanted to draw two different images (for the base and for the other drawings). Now I made it again and it works fine. (Now I need only two image.)
If anyone need the code:
The paint method of the world map:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (!settedUp) {
settedUp = true;
createPreMaps();
}
Rectangle r = new Rectangle((int) (location.X * zoomLevel), (int) (location.Y * zoomLevel),
(int) (areaMap.getWidth() * zoomLevel), (int) (areaMap.getHeight() * zoomLevel));
g.drawImage(areaMap, r.x, r.y, r.width, r.height, null);
g.drawImage(areaMap, r.x - r.width, r.y, r.width, r.height, null);
}
The mouse listeners (to move and zoom on the map):
addMouseListener(new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
if (map != null) {
if (SwingUtilities.isMiddleMouseButton(e)) {
// System.out.println("Pressed");
Point point = e.getPoint();
int validX = (int) (point.x / map.zoomLevel);
int validY = (int) (point.y / map.zoomLevel);
map.p = new Point((int) (validX), (int) (validY));
// System.out.println(e.getX() + ";" + e.getY());
}
}
}
#Override
public void mouseExited(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseClicked(MouseEvent e) {
}
});
addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseMoved(MouseEvent event) {
}
#Override
public void mouseDragged(MouseEvent event) {
if (map != null) {
if (SwingUtilities.isMiddleMouseButton(event)) {
Point point = event.getPoint();
int validX = (int) (point.x / map.zoomLevel);
int validY = (int) (point.y / map.zoomLevel);
// System.out.println("Dragged");
int thisX = (int) (map.location.X);
int thisY = (int) (map.location.Y);
// System.out.println("Dragged" + e.getX() + ";" + e.getY());
// Determine how much the mouse moved since the initial click
int xMoved = (thisX + validX) - (thisX + map.p.x);
int yMoved = (thisY + validY) - (thisY + map.p.y);
xMoved *= speed;
yMoved *= speed;
// Move picture to this position
int X = thisX + xMoved;
int Y = thisY + yMoved;
map.location = new me.fiveship.waw.objects.Point(X, Y);
if (map.location.Y > 0) {
map.location.Y = 0;
}
double a = ((-Area.max().Y + map.getBounds().getHeight() / map.zoomLevel));
if (a > map.location.Y) {
map.location.Y = (int) a;
}
int w = Area.max().X;
if (map.location.X > w) {
map.location.X = 0;
}
if (map.location.X < -w + map.getWidth()) {
map.location.X = map.getWidth();
}
// System.out.println(map.location.X);
repaint();
}
}
}
});
addMouseWheelListener(new MouseWheelListener() {
#Override
public void mouseWheelMoved(MouseWheelEvent e) {
// System.out.println(map);
if (map != null) {
double delta = 0.05d * e.getPreciseWheelRotation();
map.zoomLevel -= delta;
if (map.zoomLevel <= 1) {
map.zoomLevel = 1;
} else if (map.zoomLevel >= Consts.c().MaxZoom) {
map.zoomLevel = Consts.c().MaxZoom;
}
// System.out.println(map.zoomLevel);
if (map.location.Y > 0) {
map.location.Y = 0;
}
double a = ((-Area.max().Y + map.getBounds().getHeight() / map.zoomLevel));
if (a > map.location.Y) {
map.location.Y = (int) a;
}
int w = Area.max().X;
if (map.location.X > w) {
map.location.X = 0;
}
if (map.location.X < -w + map.getWidth()) {
map.location.X = map.getWidth();
}
map.repaint();
}
}
});

Select buttons with a mouse rather then keyboard

Hi I made a pause menu for my game, and you navigate through it with the arrow keys on the keyboard. My question is how do I make it so I can navigate with my mouse, and click the buttons rather then having to use the arrow keys?
here is the code:
public class InGameMenu implements KeyListener {
private String[] string = { "Resume Game", "Options", "Save Game", "Load Game", "Exit Game" };
private String[] optionStrings = { "Help", "Back" };
public static int selected = 0;
private int space = 25;
public int width = 800;
public int height = 600;
public static boolean isInMenu = true;
public static boolean isInOptions = false;
public static boolean saving = false;
public static boolean loading = false;
public InGameMenu() {
}
public void tick() {
}
public void render(Graphics g) {
g.setColor(new Color(0, 0, 0, 90));
g.fillRect(0, 0, Component.width, Component.height);
if (isInMenu) {
g.setColor(Color.LIGHT_GRAY);
if (saving) {
g.drawString("Saving", Component.width / 2 / Component.pixelSize - (int) (Component.width / 25), 35);
}
if (loading) {
g.drawString("Loading", Component.width / 2 / Component.pixelSize - (int) (Component.width / 25), 35);
}
for (int i = 0; i < string.length; i++) {
if (selected == i) {
g.setColor(Color.RED);
} else {
g.setColor(Color.WHITE);
}
g.drawString(string[i], Component.width / 2 / Component.pixelSize - (int) (Component.width / 17.5), Component.height / 8 + (i * space));
}
} else if (isInOptions) {
for (int i = 0; i < optionStrings.length; i++) {
if (selected == i) {
g.setColor(Color.RED);
} else {
g.setColor(Color.WHITE);
}
g.drawString(optionStrings[i], Component.width / 2 / Component.pixelSize - (int) (Component.width / 17.5), Component.height / 8 + (i * space));
}
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (isInMenu) {
if (key == KeyEvent.VK_UP) {
selected--;
if (selected < 0) {
selected = string.length - 1;
}
}
if (key == KeyEvent.VK_DOWN) {
selected++;
if (selected > string.length - 1) {
selected = 0;
}
}
if (key == KeyEvent.VK_ENTER) {
if (selected == 0) {
Component.isInMenu = false;
} else if (selected == 1) {
isInMenu = false;
isInOptions = true;
selected = 0;
} else if (selected == 2) {
saving = true;
SaveLoad.save();
saving = false;
} else if (selected == 3) {
loading = true;
SaveLoad.load();
loading = false;
} else if (selected == 4) {
System.exit(0);
}
}
} else if (isInOptions) {
if (key == KeyEvent.VK_UP) {
selected--;
if (selected < 0) {
selected = optionStrings.length - 1;
}
}
if (key == KeyEvent.VK_DOWN) {
selected++;
if (selected > optionStrings.length - 1) {
selected = 0;
}
}
if (key == KeyEvent.VK_ENTER) {
if (selected == 0) {
System.out.println("HELP");
} else if (selected == 1) {
isInOptions = false;
isInMenu = true;
}
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
You can implement MouseListener too.
You can add these methods from MouseListener:
public void mousePressed(MouseEvent e) {
if(e.getSource() == button1)
{
isInMenu = false;
isInOptions = true;
selected = 0;
}
if(e.getSource() == button2)
{
saving = true;
SaveLoad.save();
saving = false;
}
if(e.getSource() == button3)
{
loading = true;
SaveLoad.load();
loading = false;
}
if(e.getSource() == button4)
{
System.exit(0);
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
First, you must get the bounds (x, y, width and height) of the text.
You already have the x and y:
g.drawString(string[i], Component.width / 2 / Component.pixelSize - (int) (Component.width / 17.5), Component.height / 8 + (i * space));
// x = Component.width / 2 / Component.pixelSize - (int) (Component.width / 17.5)
// y = Component.height / 8 + (i * space)
You can determine the width and height via Font#getStringBounds(String, FontRenderContext):
FontRenderContext renderContext = new FontRenderContext(null, true, true);
Font font = new Font("Arial", Font.PLAIN, 14);
String labelText = "Start";
Rectangle2D labelBounds = font.getStringBounds(labelText, renderContext);
int labelWidth = (int) labelBounds.getWidth();
int labelHeight = (int) labelBounds.getHeight();
The bounds is needed to determine if the mouse is hovering over the text when the click occurs.
Next, you must maintain the bounds for each menu item.
Right now, you're only maintaining the names in string and optionStrings. You'll need to maintain the x, y, width and height for every menu item.
Since every menu item each has their own name, size and position, it would be easier to create a new type composed of these properties:
public class Label {
private String name;
private Font font;
private int x, y;
private int width, height;
public Label(String name, Font font, int x, int y, int width, int height) {
this.name = name;
this.font = font;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
Instead of a String[], you could have a Label[]. Although it's preferrable to use List for it's higher-level functionality:
public class InGameMenu implements MouseListener {
private List<Label> labels;
public void mousePressed(MouseEvent event) {
int x = event.getX();
int y = event.getY();
labels.forEach(label -> {
// if (mouse hovering over label)
// ...
});
}
}
Then, you must determine if the mouse position is within the label's position.
The formula is pretty simple:
// checks if mouse intersects with label on X axis
intersectsHorizontally := mouseX > labelX && mouseX < labelX + labelWidth;
// checks if mouse intersects with label on Y axis
intersectsVertically := mouseY > labelY && mouseY < labelY + labelHeight;
// if both conditions above are true, mouse is hovering over label
The easiest way to implement this would be to give your Label objects a containsPoint(int x, int y) behavior:
public class Label {
private int x, y;
private int width, height;
//...
public boolean containsPoint(int pointX, int pointY) {
boolean containsHorizontally = pointX > x && pointX < x + width;
boolean containsVertically = pointY > y && pointY < y + height;
return containsHorizontally && containsVertically;
}
}
Now you can easily determine whether the mouse is hovering over a specific label:
public void mousePressed(MouseEvent event) {
int x = event.getX();
int y = event.getY();
labels.forEach(label -> {
if(label.containsPoint(x, y)) {
//...
}
});
}
Finally, you must determine which label was clicked
There are many ways to go about this. Instead of a List, you could maintain the labels independently and check each one:
public class InGameMenu implements MouseListener {
private Label startLabel;
private Label loadLabel;
public InGameMenu(Label startLabel, Label loadLabel) {
this.startLabel = startLabel;
this.loadLabel = loadLabel;
}
public void mousePressed(MouseEvent event) {
int x = event.getX();
int y = event.getY();
if(startLabel.containsPoint(x, y)) {
//start game
} else if(loadLabel.containsPoint(x, y)) {
//load game
}
}
}
But this isn't dynamic, as InGameMenu is forced to know about every label it has, and adding labels would be a pain.
A better approach is to create all the Label objects outside of InGameMenu, as the examples above have been doing:
FontRenderContext renderContext = ...;
Font font = ...;
// start label
String labelText = "Start";
Rectangle2D labelBounds = font.getStringBounds(labelText, renderContext);
int x = ...;
int y = ...;
int width = (int) labelBounds.getWidth();
int height = (int) labelBounds.getHeight();
Label label = new Label(labelText, font, labelX, labelY, labelWidth, labelHeight);
// list of all labels for menu
List<Label> labels = new ArrayList<>();
labels.add(label);
InGameMenu menu = new InGameMenu(labels);
Then have the label object tell us when it has been clicked. We can do this by giving Label a click() method for InGameMenu to trigger when the label has been clicked:
public class InGameMenu implements MouseListener {
private List<Label> labels;
public InGameMenu(List<Label> labels) {
this.labels = labels;
}
public void mousePressed(MouseEvent event) {
int x = event.getX();
int y = event.getY();
labels.forEach(label -> {
if(label.containsPoint(x, y))
label.click();
});
}
}
Then allowing Label to accept a callback function:
public class Label {
private String name;
private Font font;
private int x, y;
private int width, height;
private Runnable onClick;
public Label(String name, Font font, int x, int y, int width, int height, Runnable onClick) {
this.name = name;
this.font = font;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.onClick = onClick;
}
public void click() {
onClick.run();
}
//...
}
So when you create Label objects, you can give them actions that make use of outside data (maybe a GameStateManager or something):
Runnable onClick = () -> System.out.println("Start button was clicked!");
Label label = new Label("Start", ..., onClick);

Cant see Overwrinting Cursor when i create new Line

I'm trying to create an overwriting Cursor. I've got it except when I click an earlier line the caret disappears, then when I hit 'enter' for a new line it appears again.
what should I change in my Code to solve this issue?
here is my Caret Class:
public class Mycaret extends DefaultCaret {
protected static final int MIN_WIDTH = 8;
protected DefaultCaret dc = null;
JTextComponent com = null;
public Mycaret(int rate, DefaultCaret dc) {
this.dc = dc;
super.setBlinkRate(rate);
}
protected synchronized void damage(Rectangle r) {
if (r != null) {
try {
JTextComponent comp = getComponent();
TextUI mapper = comp.getUI();
char dotChar = 0;
if(comp.getText().length()>0){
dotChar = comp.getText().charAt(comp.getText().length()-1);
}
this.com = comp;
Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
int width = r2.x - r.x;
if (width == 0 ) {
width = MIN_WIDTH;
}
comp.repaint(r.x, r.y, width, r.height);
this.x = r.x;
this.y = r.y;
this.width = width;
this.height = r.height;
}
catch (BadLocationException e) {
}
}
}
public void paint(Graphics g) {
char dotChar;
if (isVisible()) {
try {
JTextComponent comp = getComponent();
TextUI mapper = comp.getUI();
Rectangle r1 = mapper.modelToView(comp, getDot());
Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
g = g.create();
g.setColor(comp.getForeground());
g.setXORMode(comp.getBackground());
int width = r2.x - r1.x;
dotChar = comp.getText(getDot(), 1).charAt(0);
if (width == 0 ) {
width = MIN_WIDTH;
}
g.fillRect(r1.x, r1.y, width, r1.height);
g.dispose();
} catch (BadLocationException e) {
}
}
}
}
this is a Sample:
public class MyFrameSample extends JFrame {
DefaultCaret caret=null;
public MyFrameSample() {
JTextArea text = new JTextArea(10,20);
caret = new DefaultCaret();
text.setCaret(new Mycaret(500, caret));
add(text);
pack();
setVisible(true);
}
public static void main(String[] args) {
new MyFrameSample();
}
}
Based on #user1803551 observation I noticed that the width could be negative so I just changed your if condition to "<=" in the paint() and damage() methods:
if (width <= 0 )
{
width = MIN_WIDTH;
}
As I mentioned in the comments, the problem comes from newline characters (\n). When the caret is placed before \n in a non-empty line, it doesn't appear because it tries to take the width of a \n. Hence, I added a check for if a \n is at the same position the caret is.
Mid-write edit: #camickr figured out a better way by finding that somehow the width of \n is negative (anyone, why? See comments).
public class Mycaret extends DefaultCaret {
protected static final int MIN_WIDTH = 8;
public Mycaret(int rate) {
super.setBlinkRate(rate);
}
protected boolean isBeforeNewLine() throws BadLocationException {
PlainDocument doc = (PlainDocument) getComponent().getDocument();
if (doc.getText(getDot(), 1).equals("\n"))
return true;
return false;
}
#Override
protected synchronized void damage(Rectangle r) {
if (r != null) {
try {
JTextComponent comp = getComponent();
Rectangle r2 = comp.getUI().modelToView(comp, getDot() + 1);
int width = r2.x - r.x;
if (width == 0 || isBeforeNewLine()) {
width = MIN_WIDTH;
}
comp.repaint(r.x, r.y, width, r.height);
this.x = r.x;
this.y = r.y;
this.width = width;
this.height = r.height;
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
#Override
public void paint(Graphics g) {
if (isVisible()) {
try {
JTextComponent comp = getComponent();
g.setColor(comp.getForeground());
g.setXORMode(comp.getBackground());
Rectangle r1 = comp.getUI().modelToView(comp, getDot());
Rectangle r2 = comp.getUI().modelToView(comp, getDot() + 1);
int width = r2.x - r1.x;
if (width == 0 || isBeforeNewLine()) {
width = MIN_WIDTH;
}
g.fillRect(r1.x, r1.y, width, r1.height);
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
}
Explanation:
The isBeforeNewLine method get the text at the caret's position from the document. For a JTextArea it is a PlainDocument. If it's a \n then the width is set to MIN_WIDTH. This has to be done for both the paint and damage methods. It is also important to note that \n is the line separator for a JTextArea system-independently, but other text components might have it set differently.
Notes:
When catching an exception, the least you can do is print the stack trace.
I don't see a reason to create a new Graphics object in paint, use the one which is given.
Use #Override when applicable.
I just had to do some cleaning (refactoring), you might want to take something from this. I also changed the constructor since you don't need to pass a DefaultCaret to your class which extends it.

how to move and scale image by dragging it's edges?

I have post this question before also but this time I have added only required and necessary code only although the code is somewhat lengthy.
I want to load image in Jlabel and then after change image when user clicks next button. And when user want to move or scale image he can just easily do it by selecting image edges but it's not working.
All problem are solved except the scaling and moving image.
my code :
public class CopyOfPictureEditor extends JFrame
{
private static final long serialVersionUID = 6676383931562999417L;
String[] validpicturetypes = {"png", "jpg", "jpeg", "gif"};
Stack<File> pictures ;
JLabel label = new JLabel();
BufferedImage a = null;
JPanel panel = new JPanel();
public CopyOfPictureEditor()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
JMenuBar menubar = new JMenuBar();
JMenu toolsmenu = new JMenu(" File ");
final File dir = new File("");
final JFileChooser file;
file = new JFileChooser();
file.setCurrentDirectory(dir);
file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
file.showOpenDialog(panel);
String path = file.getSelectedFile().getAbsolutePath();
System.out.println(path);
pictures= getFilesInFolder(path.toString());
JButton NEXT = new JButton("");
NEXT.setToolTipText("Next Image");
Image imgn = null;
try
{
imgn = ImageIO.read(getClass().getResource("/images/next12.png"));
}catch (IOException e) {
e.printStackTrace();
}
NEXT.setIcon(new ImageIcon(imgn));
JPanel buttonPane = new JPanel();
buttonPane.add(Box.createRigidArea(new Dimension(250,0)));
buttonPane.add(Box.createRigidArea(new Dimension(250,0)));
NEXT.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
nextImage();
}
});
buttonPane.add(NEXT);
getContentPane().add(buttonPane, BorderLayout.SOUTH);
setJMenuBar(menubar);
menubar.add(toolsmenu);
panel.add(label,BorderLayout.CENTER);
add(panel);
setTitle("Aero Picture Editor");
setVisible(true);
setPreferredSize(getPreferredSize());
setLocation(0,0);
label.addMouseListenet(new MouseHandler());
label.addMouseMotionListenet(new MouseHandler());
}
public Stack<File> getFilesInFolder(String startPath){
File startFolder = new File(startPath);
Stack<File> picturestack = new Stack<File>();
String extension;
int dotindex;
for (File file : startFolder.listFiles()) {
extension = "";
dotindex = file.getName().lastIndexOf('.');
if (dotindex > 0) {
extension = file.getName().substring(dotindex + 1);
for (String filetype : validpicturetypes){
if (extension.equals(filetype)) {
picturestack.add(file);
}
}
}
}
return picturestack;
}
public void nextImage() {
String p;
File f;
try{
f= pictures.pop().getAbsoluteFile();
a=ImageIO.read(f);
p = f.getPath();
System.out.println(p);
} catch (IOException e1) {
e1.printStackTrace();
}
ImageIcon image = new ImageIcon(a);
label.setIcon(image);
repaint();
}
protected void paintComponent(Graphics g)
{
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g.create();
int x = (getWidth() - a.getWidth()) / 2;
int y = (getHeight() - a.getHeight()) / 2;
AffineTransform at = new AffineTransform();
at.translate(x, y);
g2d.setTransform(at);
g2d.drawImage(a, 0, 0, this);
g2d.dispose();
}
public enum MouseAction {
Move(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)),
ResizeSouth(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR)),
ResizeNorth(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)),
ResizeEast(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)),
ResizeWest(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)),
ResizeNorthEast(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR)),
ResizeNorthWest(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR)),
ResizeSouthEast(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR)),
ResizeSouthWest(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
private Cursor cursor;
private MouseAction(Cursor cursor) {
this.cursor = cursor;
}
public Cursor getCursor() {
return cursor;
}
}
public class MouseHandler extends MouseAdapter
{
private MouseAction action;
private Point clickPoint;
private boolean ignoreMoves;
protected void updateAction(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int width = getWidth();
int height = getHeight();
if (x < 10 && y < 10) {
action = MouseAction.ResizeNorthWest;
} else if (x > width - 10 && y < 10) {
action = MouseAction.ResizeNorthWest;
} else if (y < 10) {
action = MouseAction.ResizeNorth;
} else if (x < 10 && y > height - 10) {
action = MouseAction.ResizeSouthWest;
} else if (x > width - 10 && y > height - 10) {
action = MouseAction.ResizeSouthEast;
} else if (y > height - 10) {
action = MouseAction.ResizeSouth;
} else if (x < 10) {
action = MouseAction.ResizeWest;
} else if (x > width - 10) {
action = MouseAction.ResizeEast;
} else {
action = MouseAction.Move;
} setCursor(action.getCursor());
}
#Override
public void mouseMoved(MouseEvent e) {
if (!ignoreMoves)
{
updateAction(e);
}
}
#Override
public void mousePressed(MouseEvent e) {
updateAction(e);
ignoreMoves = true;
clickPoint = e.getPoint();
repaint();
System.out.println(e.getX());
System.out.println(e.getY());*/
}
#Override
public void mouseReleased(MouseEvent e) {
clickPoint = null;
ignoreMoves = false;
}
#Override
public void mouseDragged(MouseEvent e) {
switch (action) {
case Move: {
Point p = e.getPoint();
p.x -= clickPoint.x;
p=SwingUtilities.convertPoint(label, p, null);
setLocation(p);
}
break;
case ResizeWest: {
Point p = e.getPoint();
int xDelta = p.x - clickPoint.x;
int width = getWidth() - xDelta;
int x = getX() + xDelta;
setSize(width, getHeight());
setLocation(x, getY());
revalidate();
}
break;
case ResizeEast: {
Point p = e.getPoint();
int xDelta = p.x - clickPoint.x;
int width = getWidth() + xDelta;
setSize(width, getHeight());
revalidate();
clickPoint = p;
}
break;
case ResizeNorth: {
Point p = e.getPoint();
int yDelta = p.y - clickPoint.y;
int height = getHeight() - yDelta;
int y = getY() + yDelta;
setSize(getWidth(), height);
setLocation(getX(), y);
revalidate();
}
break;
case ResizeSouth: {
Point p = e.getPoint();
int yDelta = p.y - clickPoint.y;
int height = getHeight() + yDelta;
setSize(getWidth(), height);
revalidate();
clickPoint = p;
}
break;
}
}
#Override
public void mouseExited(MouseEvent e)
{
}
}
}
Found this one:
label.addMouseListener(new MouseHandler());
label.addMouseMotionListener(new MouseHandler());
Since you set your clickPoint in mousePressed and want to have it in mouseDragged, it should be the same object. You should actually get some nullpointers in mouseDragged?
Hard to see without complete code, but for one, you're calling setSize and setLocation on the Window itself in your MouseDragged method. If you want to get the object the user clicked on you need to get it from e.getSource(), like:
public void mouseDragged(MouseEvent e){
JLabel l = (JLabel)e.getSource();
switch(action){
case ResizeWest:
Point p = e.getPoint();
int xDelta = p.x - clickPoint.x;
int width = getWidth() - xDelta;
int x = getX() + xDelta;
l.setSize(width, getHeight()); // call setSize on JLabel l
l.setLocation(x, getY());
l.revalidate();
break;
}
Incidentally, why are you using a JLabel for this? I would use a Java object that draws itself onto the JFrame using the JFrame's graphics context.

How to Zoom specific area of Image on Canvas in Swing

I want to Zoom specific area on image. which is selected by the user. image display on canvas using Swing. i already done full image zoom on canvas but can't implement specific area zoom.
please help
Canvas is awt not swing. Try JPanel instead. Example.
But if you already done full image zoom, try to make the same on a sub-image.
BufferedImage.getSubimage(x, y, w, h);
EDIT
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class Example {
private Point startPoint = new Point(0, 0);
private Point rectLocale = new Point();
private Dimension rectSize = new Dimension();
private int zoom = 80;
private BufferedImage capture = null;
private BufferedImage raw;
public Example() throws Exception {
raw = new Robot().createScreenCapture(new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
MouseBehavior behavior = new MouseBehavior();
JPanel b = new JPanel() {
private static final long serialVersionUID = 1L;
#Override
public Dimension getMinimumSize() {
return new Dimension(500, 500);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 500);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = ((Graphics2D) g);
g2d.drawImage(raw, 0, 0, null);
if (capture != null) {
int width2 = (int) (rectSize.width + rectSize.width * (zoom / 500d));
int height2 = (int) (rectSize.height + rectSize.height * (zoom / 500d));
int x2 = rectLocale.x - ((width2 - rectSize.width) / 2);
int y2 = rectLocale.y - ((height2 - rectSize.height) / 2);
Image scaledInstance = capture.getScaledInstance(
width2, height2, Image.SCALE_AREA_AVERAGING);
g2d.drawImage(scaledInstance, x2, y2, null);
g2d.drawRect(x2, y2, width2, height2);
} else {
g2d.draw(new Rectangle(rectLocale, rectSize));
}
}
};
b.addMouseMotionListener(behavior);
b.addMouseListener(behavior);
b.addMouseWheelListener(behavior);
JFrame f = new JFrame();
f.setLocation(10, 10);
f.setDefaultCloseOperation(3);
f.add(b);
f.pack();
f.setVisible(true);
}
private class MouseBehavior extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
rectLocale = new Point();
rectSize = new Dimension();
capture = null;
if (e.getSource() instanceof JComponent) {
((JComponent) e.getSource()).repaint();
}
}
#Override
public void mouseDragged(MouseEvent e) {
Point currentPoint = e.getPoint();
rectSize.width = Math.abs(currentPoint.x - startPoint.x);
rectSize.height = Math.abs(currentPoint.y - startPoint.y);
if (e.isShiftDown()) {
rectSize.width = rectSize.height = Math.min(
rectSize.width, rectSize.height);
int dx = startPoint.x - rectSize.width;
int dy = startPoint.y - rectSize.height;
rectLocale.x = startPoint.x < currentPoint.x ? startPoint.x
: Math.max(dx, dy);
rectLocale.y = startPoint.y < currentPoint.y ? startPoint.y
: Math.min(dx, dy);
} else {
rectLocale.x = Math.min(currentPoint.x, startPoint.x);
rectLocale.y = Math.min(currentPoint.y, startPoint.y);
}
if (e.getSource() instanceof JComponent) {
((JComponent) e.getSource()).repaint();
}
}
#Override
public void mouseReleased(MouseEvent e) {
if (rectSize.width <= 0 || rectSize.height <= 0) {
capture = null;
} else {
capture = raw.getSubimage(Math.max(0, rectLocale.x),
Math.max(0, rectLocale.y), rectSize.width, rectSize.height);
}
if (e.getSource() instanceof JComponent) {
((JComponent) e.getSource()).repaint();
}
}
#Override
public void mouseWheelMoved(MouseWheelEvent e) {
zoom = Math.min(2000, Math.max(0, zoom + e.getUnitsToScroll() * 10));
if (e.getSource() instanceof JComponent) {
((JComponent) e.getSource()).repaint();
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
Example example = new Example();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

Categories

Resources